Constructor injection
- The dependent objects are injected when the class is initialized.
Public class A {/** * dependent B */ private B B; public A(B b) { this.b = b; }}Copy the code
2 Setter method injection
- Object B, which depends on the injected object A, can be injected through setter methods and obtained through getter method classes
- Injection timing is more flexible than the constructor injection method.
Public class A {/** * dependent B */ private B B; public void setB(B b) { this.b = b; } public B getB() { return this.b; }}Copy the code
3 Interface Injection
- Define a generic interface that defines the injected set method.
- The injected proprietary and dependent objects need to be specified in the configuration file. Spring is required to identify it.
- Interfaces need to be implemented, which is relatively invasive.
- For details, see the following article. Not very often.
https://wiki.jikexueyuan.com/project/spring-ioc/iocordi-1.html#6e5dfcd838f3a79e9129641785cf736f
Copy the code