Use of @Inject,@Named,@Qualifier and @Provider in dependency injection javax.inject

Keywords: Spring Java xml Google

This is what is in the Java EE 6 specification JSR 330 - Dependency Injection for Java, that is, dependency injection of Java EE.

According to the description on API document, the structure, member fields and methods annotated by @Inject are injectable.

The package can be found at jcp.org and downloaded here:

https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_JCP-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=dependency_injection-1.0-final-oth-JSpec@CDS-CDS_JCP



  • Spring's @Autowire default is equivalent to JSR-330's @Inject annotation;
  • Spring's @Qualifier default is equivalent to JSR-330's @Named annotation based on Bean name injection.
  • Spring's own @Qualifier extension @Qualifier qualified descriptor annotation is equivalent to JSR-330's @Qualifier annotation.

    As we all know from the Spring Framework, whenever we generate dependency injection, we must generate the set method of the corresponding class, and write @Autowired on the set method to achieve dependency injection. The following are:

    Java code
    1. package com.kaishengit.web;  
    2.   
    3. import com.kaishengit.service.ProjectService;  
    4. import org.springframework.beans.factory.annotation.Autowired;  
    5. import org.springframework.stereotype.Controller;  
    6.   
    7. @Controller  
    8. public class FolderController {  
    9.     private ProjectService projectService;  
    10.   
    11.     //set  
    12.     @Autowired  
    13.     public void setProjectService(ProjectService projectService) {  
    14.         this.projectService = projectService;  
    15.     }  
    16. }  

    It's troublesome to generate the corresponding set method every time. Now if we use javax.inject.jar, we just need to add @Inject to the attributes of the corresponding class, as follows:

    Java code
    1. package com.kaishengit.web;  
    2.   
    3. import com.kaishengit.service.ProjectService;  
    4. import org.springframework.stereotype.Controller;  
    5.   
    6. import javax.inject.Inject;  
    7.   
    8. @Controller  
    9. public class FolderController {  
    10.     @Inject  
    11.     private ProjectService projectService;  
    12.   
    13.   
    14. }  

    javax.inject.jar download address: https://code.google.com/p/dependency-shot/downloads/detail?name=javax.inject.jar&can=2&q=


    @Inject

    Inject supports constructors, methods, and field annotations, and may also be used for static instance members. Annotable members can be any modifier (private,package-private,protected,public). Injection order: constructor, field, and then method. The fields and methods of the parent class are injected into the fields and methods that take precedence over the subclasses, and the fields and methods in the same class are not sequential.

    The constructor of the @Inject annotation can be a constructor with no parameters or multiple parameters. Inject annotates at most one constructor per class.

    Note in the field:

    • Annotate with @Inject
    • Fields cannot be final
    • Have a legal name

    Methodological notes:

    • Annotate with @Inject
    • Can't be an abstract method
    • Cannot declare its own parameter type
    • Can return results
    • Have a legal name
    • Can have 0 or more parameters

            @Inject MethodModirers ResultType Identifier(FormalParameterList ) Throws MethodBody

    [above translation: inject doc document, translation is not good, please forgive me]

    Constructor annotations:

    1. @Inject  
    2. public House(Person owner) {  
    3.     System.out.println("---This is the building constructor.---");  
    4.     this.owner = owner;  
    5. }  
  • Field annotations:

    1. @Inject private Person owner;  
    Method Notes:

    1. @Inject  
    2. public void setOwner(Person owner) {  
    3.     this.owner = owner;  
    4. }  
    The @Inject annotation and Spring's @Autoware annotation are automatically assembled according to type.

    SpringUtil class:

    1. public class SpringUtil {  
    2.     private static ApplicationContext context = null;  
    3.     public static ApplicationContext getApplicationContext() {  
    4.         if (context == null) {  
    5.             context = new ClassPathXmlApplicationContext("spring.xml");  
    6.         }  
    7.         return context;  
    8.     }  
    9.   
    10.     public static ApplicationContext getApplicationContext(String path) {  
    11.         return new ClassPathXmlApplicationContext(path);  
    12.     }  
    13.   
    14.     public static ApplicationContext getAnnotationConfigApplicationContext(String basePackages) {  
    15.         return new AnnotationConfigApplicationContext(basePackages);  
    16.     }  
    17. }  
    Class Person:

    1. import javax.inject.Named;  
    2.   
    3. @Named  
    4. public class Person {  
    5.     private String name;  
    6.   
    7.     public Person() {  
    8.         System.out.println("---This is the constructor of human beings.---");  
    9.     }  
    10.   
    11.     public String getName() {  
    12.         return name;  
    13.     }  
    14.   
    15.     public void setName(String name) {  
    16.         this.name = name;  
    17.     }  
    18. }  
    Class House:

    1. @Named  
    2. public class House {  
    3.     @Inject private Person owner;  
    4.     public House() {  
    5.         System.out.println("---This is the building constructor.---");  
    6.     }  
    7.   
    8.     public Person getOwner() {  
    9.         return owner;  
    10.     }  
    11.   
    12.     public void setOwner(Person owner) {  
    13.         this.owner = owner;  
    14.     }  
    15. }  
    Test class:

    1. public class Test {  
    2.     public static void main(String[] args) {  
    3.         ApplicationContext context = SpringUtil.getApplicationContext(  
    4.                 "test/spring/inject/bean-inject.xml");  
    5.         House house = (House)context.getBean("house");  
    6.         Person p = house.getOwner();  
    7.         p.setName("Zhang San");  
    8.         System.out.println(house.getOwner().getName());  
    9.     }  
    10. }  
    Output results:

    This is the building constructor.
    This is the constructor of human beings.
    Zhang San

    In Spring 3.1, each constructor initializes only one time and default singleton form. I feel that if I leave Spring environment, I should instantiate new objects every time. Of course, according to the different jar packages implemented, otherwise the @Singleton annotation under javax.inject will do. It's useless.

    @Named

    The @Named and Spring's @Component functions are the same. Named can have a value, and if there is no value, the default Bean name is the same as the class name.

    For example:

    1. @Named public class Person  
    The name of the bean is person.

    1. @Named("p"public class Person  
    If you specify a name, it is the specified name.

    @Qualifier

    Anyone can define a new modifier, and a qualifier annotation should satisfy the following conditions:

    The following is an example of Qualifier:

    Genre annotation class:

    1. @Documented  
    2. @Retention(RetentionPolicy.RUNTIME)  
    3. @Qualifier  
    4. @Target(value = {ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})  
    5. public @interface Genre {  
    6.     User user() default User.STUDENT;  
    7.     public enum User {STUDENT, TEACHER}  
    8. }  
    User Interface: (counting the number)

    1. public interface IUserDAO {  
    2.     int count();  
    3. }  
    StudentDAO:

    1. @Named  
    2. @Genre(user = User.STUDENT)  
    3. public class StudentDAO implements IUserDAO{  
    4.     @Override  
    5.     public int count() {  
    6.         System.out.println("----StudentDAO----");  
    7.         return 0;  
    8.     }  
    9.   
    10. }  
    TeacherDAO:

    1. @Named  
    2. @Genre(user = User.TEACHER)  
    3. public class TeacherDAO implements IUserDAO {  
    4.   
    5.     @Override  
    6.     public int count() {  
    7.         System.out.println("--TeacherDAO--");  
    8.         return 0;  
    9.     }  
    10. }  
    UserDAOProcessor:

    1. @Named  
    2. public class UserDAOProcessor {  
    3.     /*For TeacherDAO class injection, if the StudentDAO class injection should be: @Genre(user = User.STUDENT) or @Genre, because @Genre defaults to STUDENT.*/  
    4.     @Inject  
    5.     private @Genre(user = User.TEACHER) IUserDAO userDAO;   
    6.   
    7.     public int count() {  
    8.         return userDAO.count();  
    9.     }  
    10.   
    11.     public IUserDAO getUserDAO() {  
    12.         return userDAO;  
    13.     }  
    14.   
    15.     public void setUserDAO(IUserDAO userDAO) {  
    16.         this.userDAO = userDAO;  
    17.     }  
    18. }  


    Test class:

    1. public class Test {  
    2.     public static void main(String[] args) {  
    3.         ApplicationContext context = SpringUtil.getApplicationContext(  
    4.                 "test/spring/inject/bean-inject.xml");  
    5.         UserDAOProcessor processor = (UserDAOProcessor)context.getBean("userDAOProcessor");  
    6.         System.out.println(processor.count());  
    7.     }  
    8. }  
    Output results:

    --TeacherDAO--
    0

    Personal understanding of @Qualifier:

    1. Similar to Spring's @Qualifier
    2. Using @Inject alone can't satisfy the injection of the interface and can't find any specific class, so use @Qualifier to determine the specific class injected.
    3. Value, Value, and Enumeration Types in Annotations with @Qualifier

    @Singleton

    Use this annotation to mark that the class is created only once and cannot be inherited. This annotation is generally used on classes.

  • Spring's @Autowire default is equivalent to JSR-330's @Inject annotation;
  • Spring's @Qualifier default is equivalent to JSR-330's @Named annotation based on Bean name injection.
  • Spring's own @Qualifier extension @Qualifier qualified descriptor annotation is equivalent to JSR-330's @Qualifier annotation.
  • Posted by chrisC on Mon, 10 Dec 2018 12:21:05 -0800