1. Spring 是什么?
2. IOC 是什么
2.1 正向控制
2.2 反向控制
3. DI 概念说明
4. Spring 的工作模式可以理解成一个买卖对象的市场
5. 以一个小程序来理解 IOC 和 DI
@Component
public class Car {
@Autowired
private Framework framework;
@Override
public String toString() {
return "Car{" +
"framework=" + framework +
'}';
}
}
@Component
public class Framework {
@Autowired
private Bottom bottom;
@Override
public String toString() {
return "Framework{" +
"bottom=" + bottom +
'}';
}
}
@Component
public class Bottom {
@Autowired
private Tire t1;
@Autowired
private Tire t2;
@Autowired
private Tire t3;
@Autowired
private Tire t4;
@Override
public String toString() {
return "Bottom{" +
"t1=" + t1 +
", t2=" + t2 +
", t3=" + t3 +
", t4=" + t4 +
'}';
}
}
@Component
public class Tire {
@Override
public String toString() {
return "轮子";
}
}
@SpringBootApplication
public class IocApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(IocApplication.class, args);
Car car = context.getBean(Car.class);
// 得到库中所有的 bean(对象)
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
System.out.println(car);
}
}
6. 以 XML 文件的方式将对象注入到我们自己创建的库中
6.1 新建一个 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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
6.2 在 Main 中操作这个库
public class Main {
public static void main(String[] args) {
//1. 以 XML 配置文件手动创建 Spring 容器
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("spring-config.xml");
// 2. 打印容器中所有对象的 bean-id
String[] names = context.getBeanDefinitionNames();
System.out.println("所有对象陈列:");
for (String name : names) {
Object bean = context.getBean(name);
// 得到此类的全名,即包含包名的名称
String canonicalName = bean.getClass().getCanonicalName();
System.out.println(name + " " + canonicalName);
}
int count = context.getBeanDefinitionCount();
System.out.println("一共有" + count + "个");
}
}
6.2 XML 文件的方式注入 bean(对象)
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- xml的方式注入类,new 的过程也交给 Spring-->
<bean id="empty-class" class="com.hsq.ioc2.EmptyClass"/>
<!-- 构造方法注入类-->
<bean id="second-class" class="com.hsq.ioc2.SecondClass">
<!-- 依赖 EmptyClass 注入 SecondClass-->
<constructor-arg ref="empty-class"/>
</bean>
<!-- set 方法注入类-->
<bean id="third-class" class="com.hsq.ioc2.ThirdClass">
<property name="EmptyClass" ref="empty-class"/>
<property name="SecondClass" ref="second-class"/>
</bean>
</beans>
public class EmptyClass {
public EmptyClass() {
System.out.println("被调用");
}
}
public class SecondClass {
public SecondClass(EmptyClass emptyClass) {
System.out.println("second 被调用");
}
}
public class ThirdClass {
public EmptyClass emptyClass;
public SecondClass secondClass;
public void setEmptyClass(EmptyClass emptyClass) {
System.out.println("setEmptyClass() " + emptyClass);
this.emptyClass = emptyClass;
}
public void setSecondClass(SecondClass secondClass) {
System.out.println("setSecondClass() " + secondClass);
this.secondClass = secondClass;
}
public ThirdClass() {
System.out.println("third 被调用");
}
}