wood burning stoves 2.0*
The moose likes Spring and the fly likes @Autowired Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "@Autowired" Watch "@Autowired" New topic
Author

@Autowired

bagaria kushal
Greenhorn

Joined: Mar 29, 2011
Posts: 19
If we anotate a class reference with @Autowired then is it mandatory to define a bean of that class reference in the spring configuration xml
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
No. A bean that fits must be in the application context, by whatever method.

If you are using the @Autowired annotation you certainly don't want to use XML files for configuration?
bagaria kushal
Greenhorn

Joined: Mar 29, 2011
Posts: 19
My question is that if i use @autowired annotaion for a object reference then do i need to make an entry for bean of that reference in spring xml for eg


@Autowired
Employee emp;

Spring config xml

<bean id="emp" class="com.bean.Employee"/>

do we need to do this
Shasi Mitra
Ranch Hand

Joined: Nov 27, 2008
Posts: 101

You can give this <context:component-scan base-package="org.example"/> . Now, all your beans in the package will be instantiated.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Shasi Mitra wrote:You can give this <context:component-scan base-package="org.example"/> . Now, all your beans in the package will be instantiated.


As long as you are using @Component or one of its stereotype annotations.

@Component replaces the need for <bean> tags

@Autowired replaces the need for <property> or <constructor-arg> tags.

Usually you use both, but you don't have to.

You can use just @Autowired without any @Component, then in that scenario you will need <bean> tags for all the classes. Also, with this scenario you don't use component-scan, or need it, you use <context:annotation-config> instead.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: @Autowired