How to implement dependency injection DI by spring
This article mainly introduces spring how to achieve dependency injection DI related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe you will have something to gain after reading this spring how to achieve dependency injection DI article, let's take a look at it.
Spring dependency injection DI1, create a maven project mvn archetype:generate-DarchetypeCatalog=internal2, modify pom.xml
Introduce the required dependencies, first spring-context, then spring-test, and finally junit.
UTF-8 4.3.7.RELEASE junit junit 4.12 test org.springframework spring-context ${springframework.version} org.springframework spring-test ${springframework.version} org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 utf-8 Maven-assembly-plugin 3.0.0 com.xueyoucto.xueyou.App jar-with-dependencies Make-assembly package single 3 、 Add classes Person and Bodypackage com.xueyou.demo Import org.springframework.stereotype.Component;@Componentpublic class Person {public String getName () {return name;} public void setName (String name) {this.name = name;} private String name;} package org.xueyou.demo;import org.springframework.stereotype.Component;@Componentpublic class Body {public int getId () {return id;} public void setId (int id) {this.id = id } private int id;} 4. In the configuration class App, add ComponentScan
It is important to note that the scanned package needs to be specified here.
Package com.xueyou.demo;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Hello world! * / @ Configuration@ComponentScan (basePackages = {"org.xueyou.demo", "com.xueyou.demo"}) public class App {public static void main (String [] args) {System.out.println ("Hello World!");}} 5, create a new test class package com.xueyou.demo;import org.junit.Assert Import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.xueyou.demo.Body;@RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration (classes = App.class) public class Springtest @ Autowired private Body body; @ Autowired private Person person; @ Test public void testBodyIsNull () {Assert.assertNotNull (body) } @ Test public void testPersonIsNull () {Assert.assertNotNull (person);}} 6. Run the test class
The results are as follows:
7. We can see from the running results
The Person class and the study class have been injected into spring, and spring can use them.
The spring-test dependency cannot use the problem org.springframework spring-test 4.3.7.RELEASE test
Remove
This is the end of test's article on "how spring implements dependency injection DI". Thank you for reading! I believe you all have a certain understanding of "how to achieve dependency injection DI in spring". If you want to learn more, you are welcome to follow the industry information channel.