Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

@Autowired

 
Greenhorn
Posts: 19
  • 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
 
Rancher
Posts: 436
2
  • 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
  • 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
 
Ranch Hand
Posts: 101
Spring Flex Java
  • 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.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • 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
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic