catalogue
1 what is dependency injection
2.1 test the function of @ Bean component injection:
3 @Autowired auto assembly (wiring)
four set method injection (Bean attribute injection)
One dependency injection
1 what is dependency injection
2 @Bean injection
The Spring IOC container provides the dependency injection function, which can inject a component dependent object into the appropriate location before use. For example, the following relationship simulates the dependency chainsaw when a bald head forcibly cuts a tree
Spring IOC solves dependency injection by adding parameters to the Bean component initialization method in the configuration class Config. During initialization, the spring IOC container will automatically inject Bean component objects according to the type to solve the problem of "dependency injection":
2.1 test the function of @ Bean component injection:
Step 1: create a new spring2 project and add dependencies to the pom file
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.2.RELEASE</version> </dependency> <!--JUnit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!----> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency> </dependencies>
Step 2: create tool classes and workers under the demo package respectively,
package cn.tedu.demo; import java.io.Serializable; public class Saw implements Serializable { private String name = "Ice saw"; @Override public String toString() { return name; } }
package cn.tedu.demo; import java.io.Serializable; public class Worker implements Serializable { private String name = "Bald head strength"; public Saw saw; public void work(){ System.out.println(name+"use"+saw+"Cut down trees"); } }
Step 3: edit the configuration class and use @ Bean dependency injection to connect the two
package cn.tedu.context; import cn.tedu.demo.Saw; import cn.tedu.demo.Worker; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Config { @Bean public Saw saw(){ return new Saw(); } /*Spring The type of Bean component is automatically matched according to the variable type * If the match is successful, the Bean component is injected into the method parameters*/ @Bean public Worker worker(Saw s){//The parameter types should match correctly Worker worker = new Worker(); worker.saw=s; return worker; } }
Step 4: create a test class under the test package and view the results on the console
package cn.tedu.test; import cn.tedu.context.Config; import cn.tedu.demo.Worker; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestCase { AnnotationConfigApplicationContext ctx; @Before//initialization public void init(){ ctx =new AnnotationConfigApplicationContext(Config.class); } @After//Destruction method public void destroy(){ ctx.close(); } @Test public void testWorker(){ Worker worker = ctx.getBean("worker", Worker.class); worker.work();//A bald man cuts trees with an ice saw } }
2.2 during spring IOC component injection, the component is automatically matched by type and Bean Id
3 @Autowired auto assembly (wiring)
The component scanning function provided by Spring can also complete dependency injection during scanning, which can reduce coding and improve programming efficiency
How do I use @ Autowired? Inject into the object attribute and set method, depending on the situation!!!
The following exercises are injected into object attributes:
Step 1: create a new project spring03, copy the src package of spring2, edit the tool class and worker respectively, and add the package scanning component to the configuration class
Step 2: the test class remains unchanged and the test results are the same. Therefore, adding component scanning and dependency injection can reduce code and improve programming efficiency
four set method injection (Bean attribute injection)
@Autowired is injected into the set method: it has certain logic
Step 1: edit the worker class without changing anything else
Step 2: test the effect of set method injection: