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
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:
- package com.kaishengit.web;
- import com.kaishengit.service.ProjectService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- @Controller
- public class FolderController {
- private ProjectService projectService;
- //set
- @Autowired
- public void setProjectService(ProjectService projectService) {
- this.projectService = projectService;
- }
- }
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:
- package com.kaishengit.web;
- import com.kaishengit.service.ProjectService;
- import org.springframework.stereotype.Controller;
- import javax.inject.Inject;
- @Controller
- public class FolderController {
- @Inject
- private ProjectService projectService;
- }
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:
- @Inject
- public House(Person owner) {
- System.out.println("---This is the building constructor.---");
- this.owner = owner;
- }