Spring是轻量级的开源的JavaEE框架
Spring可以解决企业应用开发的复杂性
(1)IOC:控制反转,把创建对象过程交给Spring管理
(2)AOP:面向切面,不修改源代码进行功能增强
(1)方便解耦,简化开发
(2)AOP编程支持
(3)方便程序测试
(4)方便和其他框架进行整合
(5)方便进行事务操作
(6)降低API开发难度
package com.example.spring5.domain;
public class User {
public void add(){
System.out.println("add.....");
}
}
<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="user" class="com.example.spring5.domain.User">bean>
beans>
package com.example.spring5;
import com.example.spring5.domain.User;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean1.xml")
class Spring5ApplicationTests {
@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();
}
}