Compilation software: IntelliJ IDEA 2019.2.4 x64
Operating system: win10 x64-bit Home Edition
Maven version: apache-maven-3.6.3
Mybatis version: 3.5.6
Spring version: 5.3.1
Article directory
Catalog of Spring Series Columns
1. What is Spring?
Spring is an open source Java application framework created in 2003 by Rod Johnson. It provides comprehensive infrastructure support and rich functional features for the development of enterprise-level applications, including dependency injection, aspect-oriented programming, transaction management, data access, Web applications, message passing, etc.
The Spring framework has many advantages such as light weight, strong scalability, easy use and excellent documentation , and is widely used in various types of Java projects. It is one of the most popular Java development frameworks and has become one of the standard technologies in the Java community.
In short, Spring is an IOC (DI) and AOP container framework, an open source framework for simplifying enterprise-level development .
What are IOCs?
IOC , the English full name is Inversion of Control [ Inversion of Control ], which means that the control of the object [everything is an object] is handed over to the entire Spring
What is DI?
DI, the English full name is Dependency Injection, that is, dependency injection
What is AOP?
AOP , the English full name is Aspect Oriented Programming, that is, aspect-oriented programming
Spring official website address: https://spring.io/
Use spring to get the comparison before and after the object:
2. How to build the Spring framework? (entry case)
step:
- Import spring related jar packages
- Write the configuration file (fit the object into the IOC)
- Use the core class library
Introductory case: Create a Student class, assemble the object stuTest of this class into the IOC in the configuration file, set the corresponding properties, and finally use the core class library of the Spring framework to obtain and print the information of the object stuTest.
Preparation: Create the Student class
①Import spring related jar packages
The code example is as follows:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
② Write the configuration file (assemble the object into the IOC container)
配置文件命名
: Generally named applicationContext.xml [beans.xml or spring.xml]
位置
: under the src/main/resources directory
The code example is as follows:
<?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">
<!-- 将对象装配到IOC容器中-->
<bean id="stuTest" class="spring.pojo.Student">
<!-- 给对象stuTest添加属性 -->
<property name="id" value="101"></property>
<property name="name" value="jack"></property>
</bean>
</beans>
③ Use the core class library
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.pojo.Student;
public class TestSpring {
@Test
public void test01(){
//创建容器对象(spring是一个容器)
ApplicationContext iocObj=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器对象获取需要的对象
Student stuTest = (Student) iocObj.getBean("stuTest");
System.out.println(stuTest);
}
}
What are the characteristics of three Spring?
-
非侵入式
: Objects in applications developed based on Spring may not depend on Spring APIHow to understand?
Compared with Servlet, Servlet is intrusive. When we want to use Servlet, we can create a Servlet class, which inherits HttpServlet. It seems that it does not implement the Servlet interface, but the parent class HttpServle it inherits implements the Servlet interface itself. Therefore, this kind of implementation function must inherit or implement an interface, which is called intrusive; using Spring development is different, it directly assembles the object into the IOC, and only needs to create the container object when the test is running, and then obtain the required Object.
-
容器
: Spring is a container because it contains and manages the life cycle of application objects.How to understand?
During application development, we only need to load the object into the spring container, and take it directly from it when needed
-
组件化
: Spring implements the combination of simple component configurations into a complex application. These objects can be used in Spring using a combination of XML and java annotations -
一站式
: On the basis of IOC and AOP, open source frameworks and excellent third-party class libraries that can integrate various enterprise applicationsps: In fact, Spring itself also provides the SpringMVC of the presentation layer and the DBCTemplate of the persistence layer
4. How to get the objects in the container in Spring? (getBean() method)
The getBean() method has the following five overloaded forms, as shown in the figure below.
ps: Only the first three are discussed here
Way:
-
getBean(String beanId)
: Get the object through beanldInsufficient: requires mandatory type conversion, inflexible
The sample code is as follows:
public void test01(){ //创建容器对象(spring是一个容器) ApplicationContext iocObj=new ClassPathXmlApplicationContext("applicationContext.xml"); //通过容器对象获取需要的对象 Student stuTest = (Student) iocObj.getBean("stuTest"); System.out.println(stuTest); }
-
getBean(Class aClass)
: Get the object by ClassInsufficient: When there are multiple beans of the same type in the container , the following error will be reported:
expected single matching bean but found 2:stuZhenzhong,stuZhouxu
Case demonstration: assemble two objects of the same type in the configuration file into the spring container, and then test the effect
The sample code is as follows:
<?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"> <!-- 将对象装配到IOC容器中--> <bean id="stuTest" class="spring.pojo.Student"> <!-- 给对象stuTest添加属性 --> <property name="id" value="101"></property> <property name="name" value="jack"></property> </bean> <bean id="stulisi" class="spring.pojo.Student"> <!-- 给对象stuTest添加属性 --> <property name="id" value="102"></property> <property name="name" value="李四"></property> </bean> </beans>
②Test run
@Test public void test01(){ //创建容器对象(spring是一个容器) ApplicationContext iocObj=new ClassPathXmlApplicationContext("applicationContext.xml"); //通过容器对象获取需要的对象 /* //方式1 Student stuTest = (Student) iocObj.getBean("stuTest"); System.out.println(stuTest); */ //方式2 Student bean = iocObj.getBean(Student.class); System.out.println(bean); }
-
getBean(String beanld,Class aClass)
: Get objects through beanld and ClassAdvantages: Using this method can avoid the need to force type conversion and avoid error reporting problems caused by multiple objects of the same type in the container
非常推荐使用!!!
The sample code is as follows:
@Test public void test01(){ //创建容器对象(spring是一个容器) ApplicationContext iocObj=new ClassPathXmlApplicationContext("applicationContext.xml"); //通过容器对象获取需要的对象 /* //方式1 Student stuTest = (Student) iocObj.getBean("stuTest"); System.out.println(stuTest); */ //方式2 /* Student bean = iocObj.getBean(Student.class); System.out.println(bean);*/ //方式3 Student stuTest = iocObj.getBean("stuTest", Student.class); System.out.println(stuTest); }
Notice:
框架默认都是通过无参构造器帮助我们创建对象。所以如提供对象的构造器时,一定要添加无参构造器
Five. Explain the bean tags in the configuration file in detail
Attributes:
id
: The unique identifier of the beanclass
: Define the type of bean [class full class name]
sub-tab:
property属性
: Assign values to properties in the object [set injection]name属性
: Set property namevalue属性
: set attribute value
The sample code is as follows:
<!-- 将对象装配到IOC容器中-->
<bean id="stuTest" class="spring.pojo.Student">
<!-- 给对象stuTest添加属性 -->
<property name="id" value="101"></property>
<property name="name" value="jack"></property>
</bean>
The details are as follows:
The XML configuration defines a Spring Bean named "stuTest" and sets its implementation class to "spring.pojo.Student". The bean has two properties: "id" and "name", which are set to the integer value 101 and the string value "jack", respectively.
What is used here is the set method to inject property values, that is, to pass certain values to the object by calling the setter method. Specifically, the property element is used here to configure property injection, where the name attribute specifies the name of the corresponding property in the Bean class, and the value attribute specifies the property value to be injected.