File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Autowired

 
bagaria kushal
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
Flex Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic