一、IOC简介
IOC含义:
全称:Inverse of control:控制反转,指的是将bean对象的创建及对象关联关系的维护由我们自己去做,反转给了spring的容器来创建对象,维护对象之间的关系
IOC作用:解决了上层建筑严重依赖下层建筑的问题,实现上层建筑对下层建筑的控制
IOC底层原理:xml解析,反射,容器,设计模式
二、获取容器的三种方式(容器指applicationContext.xml)
//获取application的上下文对象,参数是相对路径 // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取application上下文对象,参数是绝对路径 // ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\java\\webproject\\springStudy\\spring_day01\\src\\applicationContext.xml"); // Resource resource = new FileSystemResource("D:\\java\\webproject\\springStudy\\spring_day01\\src\\applicationContext.xml"); //方法已废弃,不需掌握 // XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(resource);
三、从容器中获取bean两种方式
https://blog.csdn.net/lox_swj/article/details/117371073
四、DI(依赖注入)
DI:Dependency Injection:依赖注入,实际上就是给属性赋值
IOC是一种控制反转的思想,具体实现就是DI
案例:
TestService类
package com.atguigu.service; public class TestService { public void success() { System.out.println("方法调用成功"); } }
TestServlet
package com.atguigu.servlet; import com.atguigu.service.TestService; public class TestServlet { private TestService testService; public void success() { testService.success(); } public TestService getTestService() { return testService; } public void setTestService(TestService testService) { this.testService = testService; } }
applicationContext.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"> <bean id="testService" class="com.atguigu.service.TestService"/> <bean class="com.atguigu.servlet.TestServlet"> <property name="testService" ref="testService"/> </bean> </beans>
测试:
@Test public void test02() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); TestServlet bean = context.getBean(TestServlet.class); bean.success(); }
运行结果
五、分模块配置
可使用import标签直接引用其他配置文件,效果和写在本配置文件中是一样的
<import resource="applicationContext_redis.xml"></import>
六、内部bean和外部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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 外部bean --> <bean id="testService" class="com.atguigu.service.TestService"/> <bean id="testServlet01" class="com.atguigu.servlet.TestServlet"> <!-- ref连接外部bean的id,形成引用 --> <property name="testService" ref="testService"/> </bean> <bean id="testServlet02" class="com.atguigu.servlet.TestServlet"> <property name="testService"> <!-- 直接在内部写一个bean --> <bean id="testService01" class="com.atguigu.service.TestService"/> </property> </bean> </beans>
七、容器中bean的创建时机、底层原理、是否单例
创建时机:容器中的bean对象默认是在容器加载时从上到下创建
底层原理:xml解析,反射,容器,设计模式
是否单例:默认单例,可以通过bean标签的属性scope属性更改
singleton:单例
prototype:多例
八、容器中创建对象的三种方式
第一种:构造器(无参构造器,有参构造器)
<!--无参--> <bean id="actor01" class="com.atguigu.beans.Actor"/> <!--有参--> <bean id="actor02" class="com.atguigu.beans.Actor"> <constructor-arg name="id" value="1001"/> <constructor-arg name="name" value="成龙"/> <constructor-arg name="age" value="60"/> <constructor-arg name="sales" value="100000.0"/> </bean>
第二种:静态工厂(通过类的静态方法得到的对象)
静态工厂类
package com.atguigu.factory; import com.atguigu.beans.Actor; public static class StaticFactory { public Actor getActor() { return new Actor(1001, "成龙", 60, 100000.0); } }
配置文件
<!--静态工厂--> <bean id="actor03" class="com.atguigu.factory.StaticFactory" factory-method="getActor"/>
实例 工厂类
package com.atguigu.factory; import com.atguigu.beans.Actor; public class InstanceFactory { public Actor getActor() { return new Actor(1001, "成龙", 60, 100000.0); } }
配置文件
<!--静态工厂--> <bean id="actor03" class="com.atguigu.factory.StaticFactory" factory-method="getActor"/> <!--实例工厂--> <bean id="instanceFactory" class="com.atguigu.factory.InstanceFactory"/> <bean id="actorCheng" factory-bean="instanceFactory" factory-method="getActor"/>
九、给对象赋值的三种方式
第一种:通过有参构造器
使用<constructor-arg>标签
name:构造器参数名字
value:参数的值
index:当前参数在参数列表的位置
type:指定当前参数的类型
<bean id="actor02" class="com.atguigu.beans.Actor"> <constructor-arg name="id" value="1001" index="0" type="java.lang.Integer"/> <constructor-arg name="name" value="成龙" index="1" type="java.lang.String"/> <constructor-arg name="age" value="60" index="2" type="java.lang.Integer"/> <constructor-arg name="sales" value="100000.0" index="3" type="java.lang.Double"/> </bean>
第二种:通过set方法
可使用标签<property>和p名称空间
<!--<property标签--> <bean id="actor04" class="com.atguigu.beans.Actor"> <property name="id" value="1001"/> <property name="name" value="成龙"/> <property name="age" value="60"/> <property name="sales" value="100000.0"/> </bean>
<!--p名称空间--> <bean id="actor05" class="com.atguigu.beans.Actor" p:id="1001" p:name="成龙" p:age="60" p:sales="100000.0"/>
第三种:使用注解