Java Techies- Solution for All
JSR 330 Annotations
Spring 3.0 supports standard JSR 330 Dependency Injection for java.
These annotations are scanned the same way as the Spring annotations,
only requirement would be to have the relevant jars in your classpath. You can use the following annotations in Spring 3 applications.
- @Named instead of spring's @Component to declare a bean
- @Inject instead of spring's @Autowired to inject a bean
Student.java File
package com.jtechies; import javax.inject.Inject; import javax.inject.Named; @Named public class Student { private String name; @Inject private Course course; public String getName() { return name; } public void setName(String name) { this.name = name; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } }
Course.java File
package com.jtechies; import javax.inject.Named; @Named 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 .ConfigurableApplicationContext; import org.springframework.context.support .ClassPathXmlApplicationContext; import com.jtechies.Course; import com.jtechies.Student; public class Test { public static void main(String[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Course course = (Course)context.getBean("course"); course.setName("MCA"); Student student = (Student) context.getBean("student"); System.out.println("course name = "+student.getCourse().getName()); context.close(); } }
spring.xml File
You also need to add <context:component-scan base-package=" " /> to scan bean annotated with @Named<?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"> <!-- base-package indicates where your component stored, spring will scan this folder and find out the bean annotated with @Named --> <context:component-scan base-package="com.jtechies" /> </beans>
Output
course name = MCA
Spring v/s JSR 330 Annotations
@Autowired | @Inject | @Inject has no 'required' attribute to make sure the bean is injected successful. |
@Component | @Named | Spring also scan for @Named. |
@Scope | @Scope | JSR 330 Scope for meta-annotation and injection point only. |
@Scope("singleton") | @Singleton | JSR-330 has scope singleton by default, but you can use Spring's @Scope to define others. |
@Qualifier | @Named | - |
@Value | No equivalent | SpEL specific |
@Required | Redundant | JSR 330 has no 'required' attribute. |
@Lazy | No equivalent | Probably a good thing. |