IOC操作Bean管理(概念)
- 什么是Bean管理
(1)Bean管理指的是两个操作
(2)Spring创建对象
(3)Speing注入属性 - Bean管理操作有两种方式
(1)基于xml配置文件实现方式
(2)基于注解方式实现
IOC操作Bean管理(基于xml配置文件实现方式)
- 基于xml方式创建对象
(1)在Spring配置文件中,使用bean标签,标签里面添加相应属性,就可以实现对象的创建
(2)在bean标签里面有很多属性,介绍常用的属性
(1)id属性:唯一标识
(2)class属性:类全路径(包类路径)
(3)name属性:与id相似(可加特殊符号 已弃用)
(3)创建对象时,默认使用无参构造完成对象创建
- 基于xml方式注入属性
(1)DI:依赖注入,就是注入属性 - 第一种注入的方式:使用set方法进行注入
(1) 创建类,定义属性和set方法
/*
演示使用set方法进行注入
*/
public class Book {
//创建属性
private String bname;
private String bauther;
private String address;
//创建属性对应set方法
public void setBauther(String bauther) {
this.bauther = bauther;
}
public void setBname(String bname) {
this.bname = bname;
}
public void setAddress(String address) {
this.address = address;
}
public void testDemo(){
System.out.println("书名《" + bname + "》作者" + bauther + ";" +address);
}
}
(2) 在Spring配置文件中配置对象创建,配置属性注入
<!-- set方法注入属性-->
<bean id="book" class="com.spring5.Book">
<!-- 使用property完成属性注入
name:类里面属性的名称
value:向属性注入值
-->
<property name="bname" value="全频段阻塞干扰"></property>
<property name="bauther" value="刘慈欣"></property>
</bean>
- 第二种注入方式:使用有参构造进行注入
(1)创建类,定义属性,创建属性对应有参构造方法
/*
有参数的构造注入
*/
public class Orders {
//属性
private String oname;
private String address;
//有参数的构造
public Orders(String oname,String address){
this.address = address;
this.oname = oname;
}
public void OrdersTest(){
System.out.println("订购方" + oname + "地址" + address);
}
}
(2)在Spring配置文件中进行配置
<bean id="Orders" class="com.spring5.Orders">
<constructor-arg name="oname" value="图书馆"></constructor-arg>
<constructor-arg name="address" value="邮电大学"></constructor-arg>
</bean>
- p名称空间注入(了解)
(1)使用p名称空间注入,可以简化基于xml配置方式
第一步 添加p名称空间在配置文件中
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
第二步 进行属性注入,在bean标签进行操作
<bean id = "book" class = "com.spring5.Book" p:bname="全频段阻塞干扰" p:bauther="刘慈欣"></bean>
IOC操作Bean管理(xml注入其他类型属性)
- 字面量
(1)null值
<!--null值-->
<property name="address">
<null/>
</property>
(2)属性值包含特殊符号
<bean name="Book" class="com.spring5.Book">
<property name="bname" value="全频段阻塞干扰"></property>
<property name="bauther" value="刘慈欣"></property>
<!-- 属性值包含特殊符号-->
<!-- 1,把<>进行转义< >-->
<!-- 2,把带特殊符号内容写道CDATA-->
<property name="address">
<value><![CDATA[图书馆]]></value>
</property>
- 注入属性-外部bean
(1)创建两个类service类和dao类
(2)在server调用dao里面的方法
(3)在Spring配置文件中进行配置
public class UserService {
//创建UserDao类型属性,生成对应set方法
public class UserService {
//创建UserDao类型属性,生成对应set方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("server add...");
userDao.updata();
//原始方式:创建UserDao对象
//UserDao userDao = new UserDaoImpl();
//userDao.updata();
}
<!-- service和dao的对象进行创建-->
<bean id="userService" class="service.UserService">
<!-- 注入useDao的对象
name属性值:类里面属性名称
ref属性:创建userDao对象bean标签id值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="dao.UserDaoImpl"></bean>
- 注入属性-内部bean
(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,属于对象类型属性进行表示
//员工类
public class Emp {
private String ename;
private String gender;
//员工属于某个部门,使用对象形式表示
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
}
//部门类
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
(3)在Spring配置文件中进行配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--内部bean-->
<bean id="emp" class="bean.Emp">
<!-- 先设置两个普通的属性-->
<property name="ename" value="刘蓬"></property>
<property name="gender" value="男"></property>
<!-- 对象类型的属性-->
<property name="dept">
<bean id="dempt" class="bean.Dept">
<property name="dname" value="清华"></property>
</bean>
</property>
</bean>
</beans>
- 注入属性-级联赋值
(1)第一种写法
<bean id="emp" class="bean.Emp">
<!-- 先设置两个普通的属性-->
<property name="ename" value="小蓬"></property>
<property name="gender" value="男"></property>
<!-- 对象类型的属性-->
<!-- 级联赋值-->
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="bean.Dept">
<property name="dname" value="邮电大学"></property>
</bean>
(2)第二种写法: 需要属性的get方法
<!--级联赋值-->
<bean id="emp" class="bean.Emp">
<!-- 先设置两个普通的属性-->
<property name="ename" value="小蓬"></property>
<property name="gender" value="男"></property>
<!-- 对象类型的属性-->
<!-- 级联赋值-->
<property name="dept" ref="dept"></property>
<!-- 需要属性的get方法-->
<property name="dept.dname" value="南邮"></property>
</bean>
<bean id="dept" class="bean.Dept">
<property name="dname" value="北邮"></property>
</bean>
IOC操作Bean管理(xml注入集合属性)
- 注入数组类型属性
- 注入List集合类型属性
- 注入Map集合类型属性
(1)创建类,定义数组,list,map,set类型属性,生成对应set方法
public class Stu {
//1,数组类型的属性
private String[] courses;
//2,List集合类型的属性
private List<String> list;
//3,map集合类型属性
private Map<String,String> maps;
//4,set集合类型属性
private Set<String> sets;
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
}
(2)在Spring配置文件进行配置
<!-- 集合类型属性的注入-->
<bean id="stu" class="com.chen.spring5.collectiontype.Stu">
<!-- 数组类型属性注入-->
<property name="courses">
<array>
<value>java</value>
<value>数据库</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="PHP" value="php"></entry>
</map>
</property>
<!-- set类型属性注入-->
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
</bean>
- 在集合里设置对象类型的值
<!-- 创建多个course对象-->
<bean id="course1" class="com.chen.spring5.collectiontype.Course">
<property name="cname" value="Spring5框架课程"></property>
</bean>
<bean id="course2" class="com.chen.spring5.collectiontype.Course">
<property name="cname" value="MyBatis框架课程"></property>
</bean>
<!-- 注入list集合形式,值是对象-->
<property name="corseList">
<list>
<ref bean="cours1"></ref>
<ref bean="cours2"></ref>
</list>
</property>
- 把集合注入部分提取出来
(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-beans.xsd">
</beans>
(2)使用util标签完成list集合注入提取
<!-- 1 提取list集合类型属性注入-->
<util:list id="bookList">
<value>时间简史</value>
<value>白夜行</value>
<value>边城</value>
</util:list>
<!-- 2 提取list集合类型属性注入使用-->
<bean id="book" class="com.chen.spring5.collectiontype.Book">
<property name="list" ref="bookList"></property>
</bean>
IOC操作bean管理(FactoryBean)
- Spring有两种类型的bean,一种普通bean,另外一种工厂bean(FactoryBean)
- 普通bean:在spring配置文件定义中bean类型就是返回值类型
- 工厂bean:在配置文件定义bean类型可以和返回的类型不一样
第一步 创建类,让这个类为工厂bean,实现FactoryBean接口
第二步 实现接口里面的方法,在实现的方法中定义返回的bean
public class MyBean implements FactoryBean <Course>{
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("spring5框架");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
<bean id="myBean" class="com.chen.spring5.factorybean.MyBean"></bean>
IOC操作bean管理(bean作用域)
- 在Spring里面,设置创建bean实例是单实例还是多实例
- Spring里面,默认情况下,bean是单实例对象
- 如何设置但实例还是多实例
(1)在spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例
(2) scope属性值,
第一个值 默认值,singleton表示单实例对象
第二个值prototype,表示多实例对象
(3)sigleton和prototype区别
第一:siglton表示单实例,prototype表示多实例
第二:设置scope值是sigleton时候,加载spring的配置文件的时候就会创建单实例对象
设置scope值是prototype时候,不是在加载spring配置文件时候创建对象,在调用getBean方法时候创建一个多实例对象
IOC操作bean的生命周期
-
生命周期
(1)从对象创建到对象销毁的过程 -
bean的生命周期
(1)通过构造器创建bean实例(无参构造)
(2)为bean的属性设置值和其他bean的引用(调用set方法)
(3)调用bean的初始化的方法(需要进行配置初始化的方法)
(4)bean可以使用了(对象获取到了)
(5)当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁方法) -
演示bean的生命周期
public class Orders {
private String oname;
//无参构造
public Orders(){
System.out.println("第一步 执行无参构造创建bean实例");
}
public void setOname(String oname) {
this.oname = oname;
System.out.println("第二步 调用set方法设置属性的值");
}
//创建执行的初始化的方法
public void initMethod(){
System.out.println("第三步 执行初始化方法");
}
public void destoryMethod(){
System.out.println("第五步 执行销毁的方法");
}
}
<bean id="orders" class="com.chen.spring5.bean.Orders" init-method="initMethod" destroy-method="destoryMethod">
<property name="oname" value="手机" ></property>
</bean>
@Test
public void testBean(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean4.xml");
Orders orders = context.getBean("orders",Orders.class);
System.out.println("第四步 获取创建bean实例对象");
((ClassPathXmlApplicationContext)context).close();
}
- bean的后置处理器,bean的生命周期有七步
(1)通过构造器创建bean实例(无参构造)
(2)为bean的属性设置值和其他bean的引用(调用set方法)
(3)把bean的实例传递给后置处理器的postProcessBeforeInitialization方法
(4)调用bean的初始化的方法(需要进行配置初始化的方法)
(5)把bean的实例传递给后置处理器的postProcessAfterInitialization方法
(6)bean可以使用了(对象获取到了)
(7)当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁方法) - 演示添加后置处理器的效果
(1)创建类,实现接口BeanPostProcessor,创建后置处理器
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
<bean id="mybeanpost" class="com.chen.spring5.bean.MyBeanPost" ></bean>
IOC操作Bean管理(xml自动装配)
- 什么是自动装配
(1)根据指定的装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入 - 演示自动装配的过程
(1)根据属性名称自动注入
<!--实现自动装配
bean标签属性autowire,配置自动装配
autowire属性两个值:
byName根据属性名称注入,注入值bean的id值和类属性名称一样
byType根据属性类型注入的id值
-->
<bean id="emp" class="com.chen.spring5.autowire.Emp" autowire="byNmme">
<!-- <property name="dept" ref="dept"></property>-->
</bean>
<bean id="dept" class="com.chen.spring5.autowire.Dept"></bean>
(2)根据属性类型自动注入
<!--实现自动装配
bean标签属性autowire,配置自动装配
autowire属性两个值:
byName根据属性名称注入,注入值bean的id值和类属性名称一样
byType根据属性类型注入的id值
-->
<bean id="emp" class="com.chen.spring5.autowire.Emp" autowire="byType">
<!-- <property name="dept" ref="dept"></property>-->
</bean>
<bean id="dept" class="com.chen.spring5.autowire.Dept"></bean>
IOC操作Bean管理(外部属性文件)
- 直接配置数据库信息
(1)配置德鲁伊连接池
(2)引入德鲁伊连接池依赖jar包 - 通过引入外部属性文件配置数据库连接池
(1)创建外部属性文件,properties格式文件,写数据库信息
(2)把外部properties属性文件引入到spring文件中
*引入context名称空间
在Spring配置文件中使用标签引入外部属性文件