preface
The last article introduced some understanding and concepts, this article combined with the code to talk about, this is suitable for xiaobai
The code is in the Github repository
- preface
- IoC – The first Spring program
- Three approaches to Spring dependency injection
- Property setter injection
- Constructor injection
- Injection by annotation
IoC – The first Spring program
The code is built based on Maven. If you are not familiar with Maven, you can check the public account JavaPub directory to learn.
- Create a project
Create a new Maven project in Idea. The directory structure is shown in the figure below
- Import dependence
pom.xml
<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 the 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> Copy the code
- In the project
src
Create a directory namedjavapub.rodert
And then create a package namedPersonDao
And add one to the interfaceadd()
methods
package javapub.rodert;
/ * * * @author wangshiyu rodert
* @date2020/7/2 were * @description * / public interface PersonDao { public void add(a); } Copy the code
- Create the interface implementation class PersonDaoImpl
Create the PersonDao implementation class PersonDaoImpl under the Javapub. Rodert package
package javapub.rodert;
/ * * * @author wangshiyu rodert
* @date2020/7/2 yours * @description * / public class PersonDaoImpl implements PersonDao { public void add(a) { System.out.println("Execution successful!!"); } } Copy the code
- Create the Spring configuration file
The Spring configuration file is the heart of integrating Spring
<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> Copy the code
- Now that a Spring program has been built, test it
Creating a 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(a){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); PersonDao personDao = (PersonDao) applicationContext.getBean("personDao"); personDao.add(); } } Copy the code
Return result:
Execution successful!!Copy the code
Run the test method using JUnit tests and it runs successfully. At program execution, object creation is not done by new a class, but by Spring container management. This is how the Spring IoC(Inversion of Control) container idea works.
Three approaches to Spring dependency injection
Dependency Injection (DI) and inversion of control have the same meaning. They are the same concept described from two perspectives.
When a Java instance needs another Java instance, the traditional way is for the caller to create the instance of the called (for example, using the new keyword to get the instance of the called). With the Spring framework, the instance of the called is created by the Spring container instead of the caller. This is called inversion of control.
Property setter injection
Means that the IoC container uses setter methods to inject dependent instances. Setter-based DI is implemented by calling setter methods of the bean after it has been instantiated by calling either the no-argument constructor or the no-argument static factory method.
Constructor injection
Means that the IoC container uses constructors to inject dependent instances. Constructor-based DI is implemented by calling constructors with parameters, each representing a dependency.
Injection by annotation
@Autowired
Statement: reference source Internet, any dispute can leave a message. Standing on the shoulders of predecessors, we can see further.
This tutorial pure hand play, committed to the most practical tutorial, hope to forward more support, is really important to me. Welcome to my public account, I hope to get to know you, more original PDF, wechat search: JavaPub, reply: [666], can also urge more.
Come and talk about any questions you have!
The next part is SSM integration