Java Techies- Solution for All
Spring Annotation Based Configuration
Spring 2.5 onward it became possible to configure the dependency injection using annotations. Annotation injection is performed before XML injection.
To use the annotations based wiring, we need to enable it in the spring XML file. The XML file to enable annotation based is given below :
<?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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> </beans>
Spring Annotations
Some of the important annotations are as follows
- @Autowired
- @Qualifier
- @PostConstruct
- @PreDestroy
- @Required
- @Bean
- @Component
- @Service
- @Repository
Spring Annotation Based Example
Student.java File
package com.jtechies; import org.springframework.beans.factory .annotation.Autowired; import org.springframework.beans.factory .annotation.Qualifier; import org.springframework.beans.factory .annotation.Required; public class Student { private String name; @Autowired //This annotation can apply to bean //property setter methods, non-setter //methods, constructor and properties. @Qualifier("mcaRelated") //This annotation along with @Autowired //can be used to remove the confusion by //specifiying which exact bean will be wired. private Course course; public String getName() { return name; } public void setName(String name) { this.name = name; } public Course getCourse() { return course; } @Required //This annotation applies //to bean property setter methods. public void setCourse(Course course) { this.course = course; } }
Course.java File
package com.jtechies; public class Course { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Test.java File
package com.jtechies.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support. ClassPathXmlApplicationContext; import com.jtechies.Student; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Student student = (Student) context.getBean("student"); System.out.println("course name = "+student.getCourse().getName()); } }
spring.xml File
<?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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <bean id="student" class="com.jtechies.Student"> <property name="name" value="John"/> </bean> <bean id="course1" class="com.jtechies.Course"> <!-- qualifier specifiying which exact bean will be wired. --> <qualifier value="mcaRelated"/> <property name="name" value="MCA"/> </bean> <bean id="course2" class="com.jtechies.Course"> <property name="name" value="B.Tech"/> </bean> <bean id="course3" class="com.jtechies.Course"> <property name="name" value="B.Arch"/> </bean> </beans>
Output
course name = MCA