preface
The previous article introduced some understandings and concepts. This article will talk about them in combination with the code. This article is suitable for Xiaobai
Code in github warehouse
[toc]
IoC - the first Spring program
Let's have a Demo first. The code is built based on Maven. If you are not familiar with maven, you can check the JavaPub directory of the official account for learning.
- Create project
Create a new Maven project in Idea. The directory structure is shown in the figure below
- Import dependent pom XML
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>javapub.rodert</groupId> <artifactId>firstSpringProject</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.7.RELEASE</version> </dependency> </dependencies> </project>
- Create a javapub Rodert's package, and then create an interface named PersonDao in the package, and add an add() method to the interface
package javapub.rodert; /** * @author wangshiyu rodert * @date 2020/7/2 20:13 * @description */ public interface PersonDao { public void add(); }
- Create interface implementation class PersonDaoImpl
In javapub Create the implementation class PersonDaoImpl of PersonDao under the Rodert package
package javapub.rodert; /** * @author wangshiyu rodert * @date 2020/7/2 20:14 * @description */ public class PersonDaoImpl implements PersonDao { public void add() { System.out.println("Successfully executed!!!"); } }
- Create Spring configuration file
Spring configuration file is the core of integrating spring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personDao" class="javapub.rodert.PersonDaoImpl"/> </beans>
- Up to now, a Spring program has been built and tested
New test class
package javapub.rodert; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author wangshiyu rodert * @date 2020/7/2 20:15 * @description */ public class PersonDaoTest { @Test public void test1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); PersonDao personDao = (PersonDao) applicationContext.getBean("personDao"); personDao.add(); } }
Return result:
Successfully executed!!!
Use JUnit test to run the test method, and the operation is successful. During program execution, the creation of objects is not completed through a new class, but through Spring container management. This is the working mechanism of the Spring IOC (inversion of control) container idea.
Three methods of Spring dependency injection
Dependency Injection (DI) and control inversion have the same meaning. They are the same concept described from two perspectives.
When a Java instance needs another Java instance, the traditional method is to create the instance of the callee by the caller (for example, use the new keyword to obtain the callee instance). After using the Spring framework, the instance of the callee is no longer created by the caller, but by the Spring container. This is called inversion of control.
Property setter injection
The IoC container uses the setter method to inject the dependent instance. After instantiating a bean by calling a parameterless constructor or a parameterless static factory method, call the setter method of the bean to implement setter based DI.
Construction method injection
Refers to the IoC container using the constructor to inject the dependent instance. The constructor based DI is implemented by calling the constructor with parameters, and each parameter represents a dependency.
Inject according to annotation
@Autowired
Statement: refer to the source Internet. You can leave a message if there is any dispute. Standing on the shoulders of our predecessors, we can see further.
This tutorial is purely hand-made and is dedicated to the most practical tutorial. I hope you can forward more support. It is really important to me.
Welcome to my official account. I hope I can get to know you. More original PDF, wechat search: JavaPub, reply: [666], or I can urge you to change.
You can talk about any problems!
Part II SSM integration