DI 是IOC的一种具体实现
一、Spring 的两个核心部分AOC 和AOP
1、IOC:控制反转,把对象的创建过程交给Spring进行管理。
1)底层原理xml解析、工厂模式、反射
2)IOC 思想基于IOC 容器实现,底层是对象工厂
3)Spring提供IOC 容器实现的两种方式:(两个接口)
BeanFactory:加载配置文件时不会创建对象,在对象获取的时候才会去创建。
ApplicationContext:BeanFactory接口的子接口,提供更强大的功能,在加载配置文件时就会把在配置文件对象进行创建。
2、AOP :面向切面,不修改源代码进行功能增强
3、Spring特点:解决耦合、简化开发、AOP 编程支持、方便与其他框架整合、方便事物操作、降低API 开发难度
二、下载Spring5的jar包
1、地址:Spring-jar包地址
Commons-logging下载地址
2、demo入门案例
导入相关jar包 core、context、beans、expression
1)配置
<?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.haoxiansheng.spring5.User"></bean>
</beans>
2、类
package com.haoxiansheng.spring5;
/**
* @author haoxiansheng
* @date 2020-06-01
*/
public class User {
public void add() {
System.out.println("add ..haoxiansheng");
}
}
3、测试
package com.haoxiansheng.spring5.testdemo;
import com.haoxiansheng.spring5.Book;
import com.haoxiansheng.spring5.Order;
import com.haoxiansheng.spring5.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author haoxiansheng
* @date 2020-06-01
*/
public class TestSpring5 {
@Test
public void testAdd() {
// 1、加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
// 2、获取配置创建的对象
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
}
三、以两个类之间的调用说明 解决耦合
1、UserService 调用 UserDAO
- 不用Spring 普通方法
// 伪代码
class UserService {
UserDAO userDAO = new UserDAO();
userDAO.insert(UserDO userDO);
}
class UserDAO {
public int insert(UserDO userDO) {
// 做操作
}
}
2)工厂模式
// 伪代码
class UserService {
UserDAO userDAO = new UserFactory.getUserDAO();
userDAO.insert(UserDO userDO);
}
class UserFactory {
public static UserDAO getUserDAO() {
return new UserDAO();
}
}
class UserDAO {
public int insert(UserDO userDO) {
// 做操作
}
}
- Spring IOC 过程
<!--xml配置文件、配置创建的对象-->
<bean id="userDAO" class="com.haoxiansheng.dao.UserDAO"></bean>
// 有service类和dao类 创建工厂
class UserFactory {
public static UserDAO getUserDAO() {
// xml解析
String classValue = class属性值;
// 通过反射创建对象
Class clazz = Class.forName(classValue);
return (UserDAO)clazz.newInstance();
}
}
- xml和注解的方式原理一样,两种不同的实现。
四、Bean管理
1、管理指的两个操作
1)Spring创建对象
2)Spring注入属性
2、实现方式
1)基于注解的方式
2)基于xml配置文件方式
3、DI 依赖注入、注入属性
1) 配置文件
<?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">
<!--完成对象创建和属性赋值
name="name" 属性名称
value="java 属性值
-->
<bean id="book" class="com.haoxiansheng.spring5.Book">
<property name="name" value="java"></property>
</bean>
<!--使用有参数构造完成对象创建和属性注入-->
<bean id="order" class="com.haoxiansheng.spring5.Order">
<constructor-arg name="name" value="上衣"></constructor-arg>
<constructor-arg name="address" value="上海"></constructor-arg>
</bean>
</beans>
2)类
package com.haoxiansheng.spring5;
/**
* @author haoxiansheng
* @date 2020-06-01
*/
public class Order {
private String name;
private String address;
// 有参数构造
public Order(String name, String address) {
this.name = name;
this.address = address;
}
@Override
public String toString() {
return "Order{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
public void print() {
System.out.println(name + " " + address);
}
}
package com.haoxiansheng.spring5;
/**
* @author haoxiansheng
* @date 2020-06-01
*/
public class Book {
private String name;
private String athor;
public Book() {
}
// 2、有参构造注入
public Book(String name) {
this.name = name;
}
// 1、set方法注入
public void setName(String name) {
this.name = name;
}
public void setAthor(String athor) {
this.athor = athor;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", athor='" + athor + '\'' +
'}';
}
public void print() {
System.out.println(name + "==" + athor);
}
public static void main(String[] args) {
Book book = new Book();
book.setName("第一种方式");
}
}
3)测试
package com.haoxiansheng.spring5.testdemo;
import com.haoxiansheng.spring5.Book;
import com.haoxiansheng.spring5.Order;
import com.haoxiansheng.spring5.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author haoxiansheng
* @date 2020-06-01
*/
public class TestSpring5 {
/**
* 测试属性注入与赋值
*/
@Test
public void testBook() {
// 1、加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
// 2、获取配置创建的对象
Book book = context.getBean("book", Book.class);
System.out.println(book);
book.print();
}
/**
* 测试属性注入与赋值 用构造方法
*/
@Test
public void testOrder() {
// 1、加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
// 2、获取配置创建的对象
Order order = context.getBean("order", Order.class);
System.out.println(order);
order.print();
}
}