<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">In Spring 2.0 introduced support for various annotations for configuration purposes, such as <a href="https://dineshonjava.com/transaction-management-in-spring/"><b>@Transactional</b></a>, <b><a href="https://dineshonjava.com/spring-required-annotation/">@Required</a> </b>and <b>@PersistenceContext /@PersistenceUnit. </b>Spring 2.5 introduces support for a complete set of configuration annotations: <a href="https://dineshonjava.com/spring-autowired-annotation/"><b>@Autowired</b></a> in combination with support for the <b><a href="https://dineshonjava.com/spring-jsr-250-annotations/">JSR-250 annotations</a> <a href="https://dineshonjava.com/spring-resource-annotation/">@Resource</a>, <a href="https://dineshonjava.com/spring-postconstruct-predestroy/">@PostConstruct</a> and <a href="https://dineshonjava.com/spring-postconstruct-predestroy/">@PreDestroy</a></b> .</p>
<div id="ads-id" align="center"></div>
<p>As of Spring 2.5, it is now possible to follow that same general approach to drive Spring&#8217;s dependency injection.Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.</p>
<p>Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>Annotation wiring is not turned on in the Spring container by default. So, before we can use annotation-based wiring, we will need to enable it in our Spring configuration file. So consider to have following configuration file in case you want to use any annotation in your Spring application.</p>
</div>
<pre class="highlight"><;beans xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">; 
 
 <;context:annotation-config>;<;/context:annotation-config>; 
 <;context:component-scan base-package="com.dineshonjava.sdnext">; 
 <;/context:component-scan>; 
 <;aop:aspectj-autoproxy>;<;/aop:aspectj-autoproxy>; 
 <;context:property-placeholder location="classpath:jdbcConfig.properties">; 
 <;/context:property-placeholder>; 
<;/beans>; 
</pre>
</div>
<p> ;</p>
<div style="color: #274e13;">
<p>Now let me explain the new configurations in the above Spring context file(<b>spring.xml</b>). The <b><;context:annotation-config/>; </b> element is used to automatically register all of Spring&#8217;s standard post-processors for annotation-based configuration.</p>
<p><b>Element : annotation-config</b><br />
Activates various annotations to be detected in bean classes: Spring&#8217;s <b>@Required and @Autowired</b>, as <b>well as JSR </b> <b>250&#8217;s @PostConstruct, @PreDestroy and @Resource (if available), JAX-WS&#8217;s @WebServiceRef (if available), EJB3&#8217;s @EJB (if available), and JPA&#8217;s @PersistenceContext and @PersistenceUnit (if available)</b>. Alternatively, you may choose to activate the individual <b>BeanPostProcessors </b>for those annotations.</p>
<p><b><i style="color: blue;">Note:</i> This tag does not activate processing of Spring&#8217;s @Transactional or EJB3&#8217;s @TransactionAttribute annotation. Consider the use of the <;tx:annotation-driven>; tag for that purpose. </b></p>
</div>
</div>
<p> ;</p>
<div style="color: #274e13;">The <b> <;context:component-scan base-package=&#8221;com.dineshonjava.sdnext&#8221;/>;</b> element is used to enable autodetection of the stereotyped classes in the package <b>&#8220;com.dineshonjava.sdnext&#8221;</b>. <b>AutowiredAnnotationBeanPostProcessor</b> and <b> CommonAnnotationBeanPostProcessor</b> are both included implicitly when using the component-scan element, the <b><;context:annotation-config/>;</b> element can be omitted.</div>
<p> ;</p>
<div style="color: #274e13;">The properties for the data source are taken from the <b>jdbcConfig.properties </b> file in the <b>classpath</b>. The property placeholders are configured with the <b><;context:property-placeholder/>;</b> element.</div>
<p> ;</p>
<div style="color: #274e13;">The <b><;aop:aspectj-autoproxy/>;</b> element is used to enable <b>@AspectJ</b> support in Spring.</div>
<p> ;</p>
<div style="color: #274e13;">Element<b><;context:load-time-weaver/>;</b> Activates a <b>Spring LoadTimeWeaver</b> for this application context, available as a bean with the name &#8220;<b>loadTimeWeaver</b>&#8220;. Any bean that implements the <b>LoadTimeWeaverAware </b>interface will then receive the <b>LoadTimeWeaver </b>reference automatically; for example, Spring&#8217;s JPA bootstrap support. The default weaver is determined automatically. As of Spring 2.5: detecting Sun&#8217;s GlassFish, Oracle&#8217;s OC4J, Spring&#8217;s VM agent and any ClassLoader supported by pring&#8217;s ReflectiveLoadTimeWeaver (for example, the TomcatInstrumentableClassLoader). The activation of AspectJ load-time weaving is specified via a simple flag (the &#8216;aspectj-weaving&#8217; attribute), with the AspectJ class transformer registered through Spring&#8217;s LoadTimeWeaver. AspectJ weaving will be activated by default if a &#8220;META-INF/aop.xml&#8221; resource is present in the classpath. This also activates the current application context for applying dependency injection to non-managed classes that are instantiated outside of the Spring bean factory (typically classes annotated with the @Configurable annotation). This will only happen if the AnnotationBeanConfigurerAspect is on the classpath (i.e. spring-aspects.jar), effectively activating &#8220;spring-configured&#8221; by default.</div>
</div>
<p> ;</p>
<div style="color: #274e13;">Let us see few important annotations to understand how they work:</div>
<h3 style="color: #274e13; text-align: left;"><b><a href="";https://dineshonjava.com/spring-required-annotation/">Annotation @Required</a>:</b></h3>
<h4 style="color: #274e13; text-align: left;">Target:</h4>
<div style="color: #274e13;"> <b>bean property setter methods</b></div>
<div style="color: #274e13;"><b>Desrcription- </b></div>
<div style="color: #274e13;">The <b><a href="https://dineshonjava.com/spring-required-annotation/">@Required</a> </b>annotation is used to specify that the value of a bean property is required to be dependency injected. That means, an error is caused if a value is not specified for that property.</div>
<p> ;</p>
<h3 style="color: #274e13; text-align: left;"><b><a href="https://dineshonjava.com/spring-autowired-annotation/">Annotation @Autowired</a>:</b></h3>
<h4 style="color: #274e13; text-align: left;">Target:</h4>
<div style="color: #274e13;"> <b>bean property setter methods, non-setter methods, constructor and properties</b></div>
<div style="color: #274e13;"><b>Desrcription- </b></div>
<div style="color: #274e13;">
<p><a href="https://dineshonjava.com/spring-autowired-annotation/"><b>@Autowired</b></a> may also be used for well-known &#8220;<b>resolvable dependencies</b>&#8220;: the <b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/">BeanFactory </a></b>interface, the <b><a href="https://dineshonjava.com/application-context-in-spring/">ApplicationContext </a></b>interface, the <b>ResourceLoader </b>interface, the <b>ApplicationEventPublisher </b>interface and the <b>MessageSource </b>interface.</p>
<h3 style="color: #274e13; text-align: left;"><b><a href="https://dineshonjava.com/spring-qualifier-annotation/">Annotation @Qualifier</a>:</b></h3>
<h4 style="color: #274e13; text-align: left;">Target:</h4>
<div style="color: #274e13;"> <b>bean property setter methods</b></div>
<div style="color: #274e13;"><b>Desrcription- </b></div>
<div style="color: #274e13;">There may be a situation when you create more than one bean of the same type and want to wire only one of them with a property, in such case you can use <a href="https://dineshonjava.com/spring-qualifier-annotation/"><b>@Qualifier</b></a> annotation along with <a href="https://dineshonjava.com/spring-autowired-annotation/"><b>@Autowired</b></a> to remove the confusion by specifying which exact bean will be wired. Below is an example to show the use of <a href="https://dineshonjava.com/spring-qualifier-annotation/"><b>@Qualifier</b></a> annotation.</div>
<p> ;</p>
<h3 style="color: #274e13; text-align: left;"><b><a href="";https://dineshonjava.com/spring-aspect-annotation/">Annotation @Aspect</a>:</b></h3>
<h4 style="color: #274e13; text-align: left;">Target:</h4>
<div style="color: #274e13;"> <b>on class marks</b></div>
<div style="color: #274e13;"><b>Desrcription- </b></div>
<div style="color: #274e13;">To use <a href="https://dineshonjava.com/spring-aspect-annotation/"><b>@AspectJ</b></a> aspects in a Spring configuration you need to enable Spring support for configuring Spring <b>AOP </b>based on <a href="https://dineshonjava.com/spring-aspect-annotation/"><b>@AspectJ</b></a> aspects, and autoproxying beans based on whether or not they are advised by those aspects. By autoproxying we mean that if Spring determines that a bean is advised by one or more aspects, it will automatically generate a proxy for that bean to intercept method invocations and ensure that advice is executed as needed.</div>
<h3 style="color: #274e13; text-align: left;"><b><a href="https://dineshonjava.com/spring-jsr-250-annotations/">JSR-250 Annotations</a>:</b></h3>
<h4 style="color: #274e13; text-align: left;">Target:</h4>
<div style="color: #274e13;"> <b>setter and non setter methods</b></div>
<div style="color: #274e13;"><b>Desrcription- </b></div>
<div style="color: #274e13;">Spring supports <a href="https://dineshonjava.com/spring-jsr-250-annotations"><b>JSR-250</b></a> based annotations which include <b>@Resource, @PostConstruct and @PreDestroy</b> annotations. <a href="https://dineshonjava.com/spring-jsr-250-annotations/"><b>JSR 250</b></a>, as a <b>Java Specification Request</b>, has the objective to define a set of annotations that address common semantic concepts and therefore can be used by many Java EE and Java SE components. This is to avoid redundant annotations across those components. <a href="https://dineshonjava.com/spring-jsr-250-annotations/"><b>JSR 250</b></a> was released on the 11th 05 2006. As Declarative annotation-driven configuration is more and more used in Java frameworks and applications, e.g. Spring makes more components of its framework configurable via annotations, the importance of <a href="https://dineshonjava.com/spring-jsr-250-annotations/"><b>JSR 250</b></a> is likely to increase in the future.</div>
<p> ;</p>
<h3 style="color: #274e13; text-align: left;"><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/">Stereotype Annotations</a>:</b></h3>
<h4 style="color: #274e13; text-align: left;">Target:</h4>
<div style="color: #274e13;"> <b>Classes marked with stereotype annotations</b></div>
<div style="color: #274e13;"><b>Desrcription- </b></div>
<div style="color: #274e13;">
<p>Classes marked with stereotype annotations are candidates for auto-detection by Spring when using <b>annotation-based configuration and classpath</b> scanning.</p>
<p>The <b>@Component</b> annotation is the main stereotype that indicates that an annotated class is a &#8220;component&#8221;.</p>
<p>The <b>@Service</b> stereotype annotation used to decorate the Service implementation related class is a specialized form of the <b>@Component</b> annotation. It is appropriate to annotate the <b>service-layer classes</b> with <b>@Service</b> to facilitate processing by tools or anticipating any future <b>service-specific </b> capabilities that may be added to this annotation.</p>
<p>The <b>@Repository</b> annotation is yet another stereotype that was introduced in Spring 2.0 itself. A class that serves in the persistence layer of the application as a data access object (DAO), otherwise known as a repository in some other technologies.</p>
<p><b>@Controller</b> A controller component in the presentation layer of the application, as it relates to a MVC-designed application</p>
<p> ;</p>
<div style="color: #274e13;"><b>In <a href="https://dineshonjava.com/spring-autowired-annotation/">Next Chapter</a> we will discuss about the <a href="https://dineshonjava.com/spring-autowired-annotation/">Using Autowired Annotation in Spring.</a></b></div>
<div style="color: #274e13;"></div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring Related Topics you may like</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/interview-questions-on-spring/"><span style="color: red;">Spring Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/"><span style="color: red;">Spring AOP Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/"><span style="color: red;">Spring Security Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-rest-web-services-interview-questions-and-answers/"><span style="color: red;">Spring REST Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-interview-questions-and-answers/"><span style="color: red;">Spring Boot Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-microservices-interview-questions-answers/"><span style="color: red;">Spring Boot Microservices Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/dependency-injection-in-spring/"><span style="color: red;">Dependency Injection (DI) in Spring </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-ioc-container/"><span style="color: red;">Spring IoC Container</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/"><span style="color: red;">What is Bean Factory in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/application-context-in-spring/"><span style="color: red;">ApplicationContext in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-autowiring-in-spring/"><span style="color: red;">Bean Autowiring in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-bean-scopes/"><span style="color: red;">Spring Bean Scopes</span></a></b></li>
<li><b><a href="https://dineshonjava.com/create-custom-bean-scope-in-spring-example/"><span style="color: red;">Create Custom Bean Scope in Spring Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/using-applicationcontextaware-in-spring/"><span style="color: red;">Using ApplicationContextAware in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-lifecycle-and-callbacks/"><span style="color: red;">Spring Bean Life Cycle and Callbacks</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanpostprocessor-in-spring/"><span style="color: red;">BeanPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanfactorypostprocessor-in/"><span style="color: red;">BeanFactoryPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-jsr-250-annotations/"><span style="color: red;">Spring JSR-250 Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/jsr-330-annotations-in-spring/"><span style="color: red;">JSR 330 Annotations in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring @Component, @Repository, @Service and @Controller Stereotype Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/method-injection-with-spring-using-lookup-method-property/"><span style="color: red;">Method injection with Spring using Lookup method property</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-aop-in-spring/"><span style="color: red;">Spring AOP-Introduction to Aspect Oriented Programming</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aspect-annotation/"><span style="color: red;">@Aspect Annotation in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-before-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Before Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @After Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-returning-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-returning-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After-Returning Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-throwing-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Throwing Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Around Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Around Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-aop-proxies-chapter-31/"><span style="color: red;">Spring AOP Proxies in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-transaction-management-in/"><span style="color: red;"> Spring AOP Transaction Management in Hibernate</span></a></b></li>
<li><b><a href="https://dineshonjava.com/transaction-management-in-spring/"><span style="color: red;">Spring Transaction Management</span></a></b></li>
<li><b><a href="https://dineshonjava.com/declarative-transaction-management/"><span style="color: red;">Spring Declarative Transaction Management Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-ordering-of-aspects-with-example/"><span style="color: red;">Spring AOP-Ordering of Aspects with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-java-based-configuration-with-example/"><span style="color: red;">Spring Security Java Based Configuration with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-xml-namespace-configuration-example/"><span style="color: red;">Spring Security XML Namespace Configuration Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
</div>
</div>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/coding-to-interfaces-in-spring/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-autowired-annotation/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '659'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…