Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Spring injection with java annotation?

Reply
Thread Tools

Spring injection with java annotation?

 
 
Alberto Sfolcini
Guest
Posts: n/a
 
      04-24-2008
Hi,

I am wondering if I can use an annotation to inject property and avoid
to write getters and setters for each property.

Let's suppose my spring's bean looks like:

<bean id="test" class="foo.bar.Test">
<property name="debug" value="true"/>
<property name="firstname" value="Bill" />
<property name="lastname" value="Gates" />
</bean>

The Test class should look like:

public class Test{
private boolean debug;
private String firstname;
private String lastname;

// Setters and getters...

public void setFirstname(String furstname) {
this.firstname = firstname;
}

etc etc...

}

What I would like to obtain is to avoid the setters and getters
methods by using an annotation:

public class Test{
@Inject(name="debug", optional="false")
private boolean debug;
@Inject
private String firstname;
@Inject
private String lastname;
}

So, let's see the @Inject interface:

public @interface Inject {
String name() default "";
boolean optional() default false;
}

Now, what I am missing is the injection engine, that should be written
in a class that extends the ApplicationContext I guess.

Can somebody give some help?

thanks
 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      04-24-2008
On Apr 24, 8:46*am, Alberto Sfolcini <(E-Mail Removed)> wrote:
> Hi,
>
> I am wondering if I can use an annotation to inject property and avoid
> to write getters and setters for each property.
>
> Let's suppose my spring's bean looks like:
>
> <bean id="test" class="foo.bar.Test">
> * * <property name="debug" value="true"/>
> * * <property name="firstname" value="Bill" />
> * * <property name="lastname" value="Gates" */>
> </bean>
>
> The Test class should look like:
>
> public class Test{
> * *private boolean debug;
> * *private String firstname;
> * *private String lastname;
>
> * *// Setters and getters...
>
> * *public void setFirstname(String furstname) {
> * * * this.firstname = firstname;
> * *}
>
> * etc etc...
>
> }
>
> What I would like to obtain is to avoid the setters and getters
> methods by using an annotation:
>
> public class Test{
> * *@Inject(name="debug", optional="false")
> * *private boolean debug;
> * *@Inject
> * *private String firstname;
> * *@Inject
> * *private String lastname;
>
> }
>
> So, let's see the @Inject interface:
>
> public @interface Inject {
> * * * * String name() default "";
> * * * * boolean optional() default false;
>
> }
>
> Now, what I am missing is the injection engine, that should be written
> in a class that extends the ApplicationContext I guess.
>
> Can somebody give some help?
>
> thanks


@Autowired.

Spring supports most of the behaviour you want already, at least as of
2.5-series releases (and possibly 2.0-series releases).

Docs link: <http://static.springframework.org/spring/docs/2.5.x/
reference/beans.html#beans-annotation-config>

However, it will still use setFoo injection or constructor arguments -
you can't get away from that without rewriting part of Spring's actual
injection mechanism (which is well-buried). Furthermore, you don't
need to provide set/get pairs for properties you want injected: a set
method is enough. The only reason to add a get method is if you plan
on using the property elsewhere via its get method.

-o
 
Reply With Quote
 
 
 
 
Chris Seidel
Guest
Posts: n/a
 
      04-24-2008
Owen Jacobson wrote:
> @Autowired.


> However, it will still use setFoo injection


You do not need a setter, works with a private field too.


 
Reply With Quote
 
Alberto Sfolcini
Guest
Posts: n/a
 
      04-24-2008
On 24 Apr, 17:07, "Chris Seidel" <(E-Mail Removed)> wrote:
> Owen Jacobson wrote:
> > @Autowired.
> > However, it will still use setFoo injection

>
> You do not need a setter, works with a private field too.



Yes it works fine without a setter method, but it is not exactly what
I wanted.
Let's say that it wires with types, I read it's also possible to wire
by names, I have to look at it deeply.
thanks.
 
Reply With Quote
 
Alberto Sfolcini
Guest
Posts: n/a
 
      04-24-2008
On 24 Apr, 17:07, "Chris Seidel" <(E-Mail Removed)> wrote:
> Owen Jacobson wrote:
> > @Autowired.
> > However, it will still use setFoo injection

>
> You do not need a setter, works with a private field too.


// autowiring by Name
@Autowired
@Qualifier("yourObjectName")


Is there a way to merge these annotation into one? for example
( @Inject(name="yourObjectName", required=true) ?
Any ideas?
 
Reply With Quote
 
Chris Seidel
Guest
Posts: n/a
 
      04-24-2008
Alberto Sfolcini wrote:
> // autowiring by Name
> @Autowired
> @Qualifier("yourObjectName")
>
>
> Is there a way to merge these annotation into one? for example
> ( @Inject(name="yourObjectName", required=true) ?
> Any ideas?


Look into the component-scanner code how the annotations are found.
But I think you have to chance the scanner.
Why not 3 annotations?


 
Reply With Quote
 
Alberto Sfolcini
Guest
Posts: n/a
 
      04-30-2008
On 24 Apr, 18:20, "Chris Seidel"
> Look into the component-scanner code how the annotations are found.
> But I think you have to chance the scanner.
> Why not 3 annotations?


No problem at all even with 3 annotations.
I was thinking if is possible to use annotation even with Struts2 in
order to avoid setters and getters.
Do you know something about it?

bye
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
spring hibernate injection eunever32@yahoo.co.uk Java 7 03-03-2011 05:13 AM
resource injection in java EE webservice gk Java 14 09-29-2010 12:57 PM
Spring Python 0.8.0 has been released/Spring Python goes green Goldfish Python 1 11-06-2008 06:12 PM
differences between Spring WebFlow,Spring MVC,and String Portlet MVC? rmn190 Java 2 01-10-2008 02:27 AM
Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object (ant/tomcat/spring) courtney.machi@gmail.com Java 0 12-08-2006 07:37 PM



Advertisments