@Autowired Annotation was introduced in Spring 2.5. You may have read here [autowiring] how dependencies can be automatically injected into the beans by using 'autowire' attribute of <bean> tag.
The @Autowired annotation does the same thing but with little difference. Being an annotation, it is applied directly into your bean's code instead of xml file. It helps in removing <property> and <constructor-arg> tags from xml configuration file thus reducing the configuration code in xml file.
@Autowired annotation can be applied at following levels:
1) On Setter methods (in fact, on any method)
2) On Fields
3) On Constructors
Before using @Autowired we have to enable Spring in order to use it. For this we have to add following lines in our xml file.
The @Autowired annotation does the same thing but with little difference. Being an annotation, it is applied directly into your bean's code instead of xml file. It helps in removing <property> and <constructor-arg> tags from xml configuration file thus reducing the configuration code in xml file.
@Autowired annotation can be applied at following levels:
1) On Setter methods (in fact, on any method)
2) On Fields
3) On Constructors
Before using @Autowired we have to enable Spring in order to use it. For this we have to add following lines in our xml file.
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- Enabling Spring to use @Autowired-->
<context:annotation-config/>
. . .
. . .
</beans>
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- Enabling Spring to use @Autowired-->
<context:annotation-config/>
. . .
. . .
</beans>
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment