1. Spring concept
(1) Spring isLightweightofOpen source JavaEE frame。
(2) Spring can solve the complexity of enterprise application development.
(3) Spring has two core parts: IOC and Aop.
- IOC: Inversion of control, the process of creating objects is handed over to Spring for management
- Aop: aspect-oriented, without modifying the source code for functional enhancement
(4) Spring features
- Facilitate decoupling and simplify development
- Aop programming support
- Convenient for program testing
- Easy to integrate with other frameworks
- Facilitate transaction operations
- Reduce the difficulty of API development
(5) Now in the course, select Spring version 5.x
2. Introductory case
2.1 Download Spring5
Official website address: Spring official website address https://spring.io/
Download address: Spring download address
Unzip and get the following directory:
2.2 Open the IDEA tool and create a normal Java project
2.3 Import Spring5 related jar packages
Create a lib file and put the basic jar package into
2.4 Create a common class, create a common method in this class
2.5 Create a Spring configuration file and configure the created objects in the configuration file
Spring configuration file uses xml format
bean1.xml
<?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">
<!-- 配置User对象创建 -->
<bean id="user" class="com.mycode.spring5.User"></bean>
</beans>
2.6 Write test code
public class TestSpring5 {
@Test
public void testAdd() {
// 1、加载 Spring 配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
// 2、获取配置创建的对象
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
}