IOC
1.IOC
控制反转。降低代码之间耦合度(目的)。最常见的方式叫依赖注入。
2.IOC底层原理
(1)xml解析,工厂模式,反射
工厂模式:
IOC过程:
(1)xml配置文件。配置创建的对象
(2)有service类和dao类,创建工厂类
class UserFactory{
public static UserDao getDao(){
String classValue = class属性值; //xml解析
Class clazz = Class.forName(classValue);// 通过反射创建对象
return (UserDao)clazz.newInstance();
}
}
进一步降低耦合度。
IOC思想:
(1)基于IOC容器完成,IOC容器底层就是对象工厂。
(2)Spring提供IOC容器实现两种方式,两个接口
a. BeanFactory:IOC容器基本实现,是Spring内部使用的接口,不提供开发人员进行使用。
加载配置文件时不会创建对象
b. ApplicationContext:BeanFactory接口的子接口,提供更多更强大的功能,一般由开发人员进行使用。
加载配置文件时就会在配置文件对象进行创建
IOC操作Bean管理:
(0)Bean管理指的是两个操作
- Spring创建对象
- Spring注入属性
Bean管理操作两种方式:
(1)基于xml配置文件方式实现
(2)基于注解
基于xml方式创建对象:
(1)
(2)bean标签有很多属性。
-
id:唯一属性
-
class:类全路径(包类路径)
-
name
(3)创建对象时候,默认也是执行无参构造方法完成对象创建
基于xml方式注入属性:
(1)DI:依赖注入,就是注入属性。
第一种注入方式:使用set方法注入
package com.demo.spring5.testdemo;
public class Book {
private String bname;
public void setBname(String bname) {
this.bname = bname;
}
public static void main(String[] args) {
Book book = new Book();
book.setBname("abc");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--peizhiUser-->
<bean id="user" class="com.demo.spring5.User"/>
<bean id="book" class="com.demo.spring5.Book">
<!--使用property注入-->
<!--name:类型名称-->
<!--value:注入的值-->
<property name = "bname" value="三国演义"/>
<property name = "bauthor" value="罗贯中"/>
</bean>
</beans>
第二种方式:使用有参数构造进行注入
public class Orders {
private String oname;
private String address;
public Orders(String oname, String address) {
this.oname = oname;
this.address = address;
}
}
bean.xml配置
<bean id="orders" class="com.demo.spring5.Orders">
<constructor-arg name="oname" value="abc"/>
<constructor-arg name="address" value="beijing"/>
</bean>
也可以用索引
<constructor-arg index="0" value="beijing"/>
测试:
@Test
public void testOrder(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Orders order = context.getBean("orders", Orders.class);
System.out.println(order);
order.test();
}
p名称空间注入:
(1)添加p名称空间在配置文件
xmlns:p="http://www.springframework.org/schema/p"
<bean id="book" class="com.demo.spring5.Book" p:bname="三国演义" p:bauthor="罗贯中">
</bean>
IOC操作Bean管理(xml注入其他类型属性)
(1)字面量
-
空值:
<property name="address"><null/>
</property>
-
属性值包含特殊符号:
注入属性-外部bean:
(1)创建两个类service类和dao类
(2)在service调用dao里面的方法
(3)在spring配置文件内找到
package com.demo.spring5.dao;
public class userDaoImpl implements userDao{
@Override
public void update() {
System.out.println("dao update...");
}
}
package com.demo.spring5.service;
import com.demo.spring5.dao.userDao;
import com.demo.spring5.dao.userDaoImpl;
@SuppressWarnings("all")
public class userService {
//创建userDao类型属性,生成set方法
private userDao userDao;
public void setUserDao(userDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add...");
userDao.update();
// 创建对象
// userDaoImpl userDao = new userDaoImpl();
// userDao.update();
}
}
测试:
public class TestBean {
@Test
public void testAdd(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
userService userService = context.getBean("userService", userService.class);
System.out.println(userService);
userService.add();
}
}
<!--service对象和dao对象创建-->
<bean id="userService" class="com.demo.spring5.service.userService">
<!--注入userDao对象
name属性值:类里面属性名称
ref属性:创建userDao对象bean标签id值
-->
<property name="userDao" ref="userDaoImpl"/>
</bean>
<bean id="userDaoImpl" class="com.demo.spring5.dao.userDaoImpl"/>
注入属性-内部bean和级联赋值:
(1)一对多:部门和员工
(2)在实体类之间表示一对多关系,员工表示所属部门。适用对象类型属性进行表示。
package com.demo.spring5.bean;
public class employee {
private String ename;
private String gender;
private department dept;
public void setDept(department dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
}
package com.demo.spring5.bean;
public class department {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
(3)在spring配置文件中配置
<bean id="emp" class="com.demo.spring5.bean.employee">
<!--先设置两个普通属性-->
<property name="ename" value="lucy"/>
<property name="gender" value="女"/>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.demo.spring5.bean.department">
<property name="dname" value="安保部"/>
</bean>
</property>
</bean>
注入属性-级联赋值
(1)第一种写法
<bean id="emp" class="com.demo.spring5.bean.employee">
<!--先设置两个普通属性-->
<property name="ename" value="lucy"/>
<property name="gender" value="女"/>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.demo.spring5.bean.department">
<property name="dname" value="财务部"></property>
</bean>
(2)第二种写法
需要生成dept的get方法
<bean id="emp" class="com.demo.spring5.bean.employee">
<!--先设置两个普通属性-->
<property name="ename" value="lucy"/>
<property name="gender" value="女"/>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术部"></property>
</bean>
<bean id="dept" class="com.demo.spring5.bean.department">
<property name="dname" value="财务部"></property>
</bean>
IOC操作Bean管理(xml管理注入集合属性)
1.注入数组类型属性:
2.注入List集合类型属性:
3.注入Map集合类型属性:
(1)创建类
package com.demo.collectiontype;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
private String[] courses;
private List<String> list;
private Map<String,String> maps;
private Set<String> set;
public void setSet(Set<String> set) {
this.set = set;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
}
(2)spring中配置文件
<!--集合类型属性注入-->
<bean id="stu" class="com.demo.collectiontype.Stu">
<!--数组类型注入-->
<property name="courses">
<array>
<value>java</value>
<value>mysql</value>
</array>
</property>
<!--list-->
<property name="list">
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<!--map-->
<property name="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PYTHON" value="python"></entry>
</map>
</property>
<!--set-->
<property name="set">
<set>
<value>111</value>
<value>222</value>
</set>
</property>
</bean>
</beans>
4.在集合中设置对象类型值
<!--注入list集合类型,值是对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="com.demo.collectiontype.Course">
<property name="cname" value="test1"></property>
</bean>
<bean id="course2" class="com.demo.collectiontype.Course">
<property name="cname" value="test2"></property>
</bean>
5.把集合注入部分提取出来
(1)在spring配置文件中引入名称空间util
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
IOC操作Bean管理(FactoryBean):
1.Spring有两种类型的bean,一种普通bean,还有一种工厂bean。
2.普通bean:在配置文件中bean类型就是返回类型
3.工厂bean:在配置文件定义bean类型可以和返回类型不一样
第一步:创建类,让这个类作为工厂bean,实现接口FactoryBean
第二步:实现接口里面的方法,在实现的方法中定义返回的bean类型
<bean id="myBean" class="com.demo.factorybean.Mybean">
</bean>
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Course course = context.getBean("myBean", Course.class);
System.out.println(course);
}
IOC操作Bean管理(bean作用域)
1.在Spring里面,设置创建bean实例是单实例还是多实例
2.在Spring李敏啊,默认情况下,bean是单实例对象
3.如何设置单实例还是多实例
(1)在spring配置文件bean标签里面有属性(scope)设置单实例还是多实例
(2)scope属性值
第一个值 默认值, singleton, 表示单实例对象
第二个值 prototype 多实例
(3)singleton 和 prototype区别
设置scope=singleton,加载spring配置文件时就会创建一个单实例对象
设置scope=prototype,不是在加载spring配置文件时候创建对象,在调用getBean方法时候创建多实例对象。
request
session
IOC操作Bean管理(bean生命周期)
1.生命周期
(1)从对象创建到对象销毁的过程
2.bean生命周期
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean饮用(调用set方法)
把bean实例传递bean后置处理器的方法
(3)调用bean的初始化方法(需要进行配置初始化方法)
把bean实例传递bean后置处理器的方法
(4)bean可以使用了(对象获取到了)
(5)当容器在关闭时候,调用bean的销毁方法(需要进行配置)
IOC操作Bean管理(xml自动装配)
根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入。
<!--实现自动装配
bean标签属性autowire,配置自动装配
autowire属性常用两个值:
byName根据属性名称注入,注入值的bean的id值和类属性名称一样
byType根据属性类型注入-->
<bean id="emp" class="com.demo.autowire.Emp" autowire="byName">
IOC操作Bean管理(外部属性文件)
1.直接配置数据库信息
(1)配置druid连接池
(2)引入druid连接池依赖jar包
2.引入外部属性文件配置数据库连接池
(1)创建外部属性文件,properties格式文件,数据库信息
(2)把外部properties属性文件引入到spring配置文件中
引入context名称空间