• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

@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: 17344
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
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!