Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to turn on Annotations in Spring Configuration file ?

A simple snippet showing how to turn on Annotations in Spring Configuration file

Annotations came into picture with JDK 1.5 to make life of a developer easier. Annotations are meta-data which provides additional information about something. It is applied to various variables , methods , classes , constructors and other declarations. Spring provides annotations as an alternative way over xml based configuration.

Annotations are turned off by-default by Spring Framework. In order to turn it on we have to provide a entry in Spring Configuration file. Using tag context:annotation-config from Spring's context namespace makes it turn on. Annotation makes development easier and faster. It also takes addition of information closer to source code. Annotation injection is done prior to xml based injection by Spring Framework.


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- In order to turn on Annotations in Spring over xml configuration we 
        need to provide a tag by name <context:annotation-config /> see the tag below 
        .... -->

    <context:annotation-config />

    <bean id="employee" class="com.hubberspot.spring.autowire.Employee"
        
        
        <property name="name" value="Jonty" />
        <property name="age" value="28" />
        <property name="address" ref="address"></property>
    </bean>

    <bean id="address" class="com.hubberspot.spring.autowire.Address">

        <property name="street" value="Town Hall Street" />
        <property name="city" value="Pune" />
        <property name="state" value="Maharashtra"></property>
    </bean>

</beans>


After the annotations in Spring file is turned on, Spring automatically wires many components such as properties , constructors and various stuff to a value.
 
© 2021 Learn Java by Examples Template by Hubberspot