- IOC具体操作,Bean管理(概念)
- 基于xml方式注入属性
- 1:set方法注入属性,先通过xml的bean标签创建对象,在通过xml中的property标签对该对象进行属性值注入;
- 2:DI注入第二中方式:有参构造
- 拓展 P名称空间注入
- 注入空值和特殊符号
- 方式1:通过&..转义符号实现<>作为name值
- 方式2:把特殊符号的内容写入到CDATA中,例如:
- 注入属性——外部bean
- 注入属性—内部bean和级联赋值
- 注入属性—级联赋值
- xml注入集合属性
- spring中有俩种bean
- 1:普通bean:在配置文件中定义的bean类型就是返回的类型
- 2:工厂bean:在配置文件中定义的bean类型可以和返回类型不一样
- bean作用域
- 1:在spring里面,设置创建bean可以设置是单实例和多实例;
- 2:spring默认情况下,创建的是单实例对象
- 3:如何设置单实例,多实例
- Bean生命周期
- XML自动装配
- 引入外部属性文件
IOC具体操作,Bean管理(概念)
1.什么是Bean管理?=》》》》
(0)Bean管理指的是俩个操作
(1)Spring 创建对象
(2)Spring注入属性
2、Bean管理操作有俩种方式 基于XML和注解的Spring Bean管理
xml配置,注解,扫描?
Spring中Bean管理操作基于XML配置文件方法实现
(1)基于xml配置文件实现
1、基于xml方式创建对象
1:在spring配置文件中,使用bean标签,标签中添加对应属性,就可实现利用xml创建对象。
2:在bean标签中有很多属性,常用属性有
id属性,给需要创建的对象起一个唯一标识
class属性,需要创建对象类的全路径,包名(package)+类名(Class Name)(com.baidu.User)
*name属性,与id属性功能相同,区别在与name中可以加特殊符号(早期框架struts)
3:在创建对象时候,默认也是执行无参构造方法,完成创建对象。
基于xml方式注入属性
(1)DI:依赖注入,就是注入属性。DI为IOC中的一种具体实现,DI需要在创建对象的基础之上来进行DI属性注入。
依赖注入方法:set,有参,接口
1:set方法注入属性,先通过xml的bean标签创建对象,在通过xml中的property标签对该对象进行属性值注入;
2:DI注入第二中方式:有参构造
1.创建订单实体
package com.junjay.spring5;
public class Orders {
// 订单名称
private String oname;
// 订单地址
private String address;
// 有参构造
public Orders(String oname, String address) {
super();
this.oname = oname;
this.address = address;
}
}
2,在spring文件中配置DI有参注入,
<!-- 有参数构造注入属性 -->
<bean id="orders" class="com.junjay.spring5.Orders">
<constructor-arg name="oname" value="单号211"></constructor-arg>
<constructor-arg name="address" value="洛阳"></constructor-arg>
<!-- 对应有参注入中的第一个参数,即oname -->
<!-- <constructor-arg index="0" value="可以通过有参构造的参数序号注入"></constructor-arg> -->
</bean>
在订单实体中加入打印信息方法
package com.junjay.spring5;
public class Orders {
// 订单名称
private String oname;
// 订单地址
private String address;
// 有参构造
public Orders(String oname, String address) {
super();
this.oname = oname;
this.address = address;
}
public void orderText() {
System.err.println("oname="+oname);
System.err.println("address="+address);
}
}
拓展 P名称空间注入
使用p名称空间注入,可以简化基于xml配置的方式
1:通过修改xml配置文件中头部里的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"
xmlns:aop="http://www.springframework.org/schema/aop"
<!--区分命名空间,使用到xml中的约束 -->
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
2:通过xml 中p命名方式注入值
<!-- set 注入属性 -->
<bean id="book" class="com.junjay.spring5.Book" p:bname="三毛">
</bean>
注入空值和特殊符号
1,字面量(设置对象默认值,固定值)
// 订单名称
private String oname="我是字面量设置的值";
// 订单地址
private String address;
<bean id="book" class="com.junjay.spring5.Book" p:bname="三毛是字面量">
</bean>
1:字面量设置null值:
private String bname;
private String zname;
// set方法
public void setBname(String bname) {
this.bname = bname;
}
public void setZname(String zname) {
this.zname = zname;
}
<bean id="book" class="com.junjay.spring5.Book">
<!-- 使用property 完成属性注入 name:属性名称,value:注入属性的值 -->
<property name="bname" value="小王子"></property>
<!-- 设置zname空值 null -->
<property name="zname">
<null></null>
</property>
</bean>
方式1:通过&..转义符号实现<>作为name值
<bean id="book" class="com.junjay.spring5.Book">
<!-- 使用property 完成属性注入 name:属性名称,value:注入属性的值 -->
<property name="bname" value="小王子"></property>
<!-- 属性值中包含特殊符号 -->
<property name="zname" value="<左右尖括号被实别为标签符号>">
</property>
</bean>
方式2:把特殊符号的内容写入到CDATA中,例如:
<bean id="book" class="com.junjay.spring5.Book">
<!-- 使用property 完成属性注入 name:属性名称,value:注入属性的值 -->
<property name="bname" value="小王子"></property>
<!-- 属性值中包含特殊符号 -->
<property name="zname">
<value><![CDATA[<被CDATA包裹的内容原样输出,不会被实别为标签>]]></value>
</property>
</bean>
注入属性——外部bean
1:创建俩个类service 类和 dao类
2:在service中调用dao方法
public class UserService {
// 原始方法
public void add() {
System.err.println("service -------- add ");
// 创建UserDao 对象
// 创建实现类UserDaoImpl以UserDao接受,这个过程就是多态
// 接口 new 接口实现类
UserDao userdao = new UserDaoImpl();
userdao.add();
}
}
/**
* interface 接口关键字
* dao 接口
* @author My
*
*/
public interface UserDao {
/**
* 多外接口
*/
void add();
void update();
}
/**
* implements 实现接口关键字
* dao接口实现类
* @author My
*
*/
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.err.println("UserDaoImpl -------- add ");
}
@Override
public void update() {
System.err.println("UserDaoImpl -------- update ");
}
}
3:在spring配置文件中实现以上方法,首先通过配置文件创建俩个对象,然后在通过DI注入的方式把UserDao注入到UserServive中(注入方式可以为set注入,或有参注入)
<!-- 1,service 和 dao 创建对象 -->
<bean id="userService" class="com.junjay.spring5.service.UserService">
<!-- set 方式注入userDao,
name:为UserService的属性名称
ref:为创建userDao对象的bean标签的id的值(外部bean注入)
-->
<property name="userDao" ref="userDaoImpl">
</property>
</bean>
<!-- UserDao是被interface修饰的接口,接口无法实例化创建对象,所以需要创建接口实现类的对象 -->
<bean id="userDaoImpl" class="com.junjay.spring5.dao.UserDaoImpl"></bean>
警告: Exception encountered during context initialization
- cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userService' defined in class path resource [bean2.xml]:
Error setting property values;
nested exception is org.springframework.beans.NotWritablePropertyException:
Invalid property 'userDao' of bean class [com.junjay.spring5.service.UserService]:
Bean property 'userDao' is not writable or has an invalid setter method. Did you mean 'userdao'?
注入属性—内部bean和级联赋值
1:一对多关系 :部门与员工(一个部门有多个员工,一个员工属于一个部门)
2:在实体类之间表示一对多关系
/**
* @author 部门实体类
*
*/
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept [dname=" + dname + "]";
}
}
/**
* @author 员工实体
*
*/
public class Emp {
private String ename;
private String gender;
// 员工属于某一部门,使用对象表示
private Dept dept;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void add() {
System.out.println("ename=" + ename);
System.out.println("gender=" + gender);
System.out.println(dept.toString());
}
}
3:在spring配置文件中,配置相关实体内部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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 内部bean -->
<!-- 1:创建员工实体对象 -->
<bean id="emp" class="com.junjay.spring5.bean.Emp">
<!-- 设置俩个普通属性 -->
<property name="ename" value="张三"></property>
<property name="gender" value="男"></property>
<!-- 设置dept对象属性 -->
<property name="dept">
<!-- 嵌套dept对象并且给dept内的属性赋值 -->
<!-- 这种内部嵌套的方式就是内部bean -->
<bean id="dept" class="com.junjay.spring5.bean.Dept">
<property name="dname" value="人事部"></property>
</bean>
</property>
</bean>
</beans>
注入属性—级联赋值
方式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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 内部bean -->
<!-- 1:创建员工实体对象 -->
<bean id="emp" class="com.junjay.spring5.bean.Emp">
<!-- 设置俩个普通属性 -->
<property name="ename" value="张三"></property>
<property name="gender" value="男"></property>
<!-- 级联外部赋值,dept对象属性 -->
<property name="dept" ref="dept">
</property>
</bean>
<bean id="dept" class="com.junjay.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>
</beans>
方式2:级联内部赋值
<!-- 内部bean -->
<!-- 1:创建员工实体对象 -->
<bean id="emp" class="com.junjay.spring5.bean.Emp">
<!-- 设置俩个普通属性 -->
<property name="ename" value="张三"></property>
<property name="gender" value="男"></property>
<!-- 级联内部赋值,dept对象属性 -->
<property name="dept" ref="dept"></property>
<!-- 如果emp实体类中没有dept的get方法则允许报错,因为没有get方法就获取不到dept.dname -->
<property name="dept.dname" value="财务部"></property>
</bean>
在emp对象中添加get方法以获取dept对象中的dname,供xml中的dept.dname进行值注入
// Dept实体上面已经写过,在实体内添加get方法即可
// 生成dept的get方法才能获取到spring配置文件中dept对象的dept.dname
public Dept getDept() {
return dept;
}
xml注入集合属性
1:注入数组类型属性
1:创建包含个集合类型的对象实体并生成sett方法
数组,Set, 注入List集合属性3:注入Map集合属性
package com.junjay.spring5.collectionbean;
import java.sql.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
// 数组类型 属性
private String[] sarr;
// List集合类型属性
private List slist;
// Map集合类型属性
private Map<String, String> smap;
// Set集合属性
private Set<String> set;
public void setSarr(String[] sarr) {
this.sarr = sarr;
}
public void setSlist(List slist) {
this.slist = slist;
}
public void setSmap(Map<String, String> smap) {
this.smap = smap;
}
public void setSet(Set<String> set) {
this.set = set;
}
/**
* 数组打印
*/
public void getArr() {
System.out.println(Arrays.toString(sarr));
}
public void getList() {
System.out.println(slist);
}
public void getMap() {
System.out.println(smap);
}
public void getSet() {
System.out.println(set);
}
}
配置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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 集合类型属性注入 -->
<bean id="stu" class="com.junjay.spring5.collectionbean.Stu">
<!-- 数组类型属性注入 -->
<property name="sarr">
<!-- 数组类型可多内容,用array或list标签,注入多个值 -->
<array>
<value>数组1</value>
<value>数组2</value>
</array>
</property>
<!-- List类型属性注入 -->
<property name="slist">
<!-- List类型可多内容,用list标签,注入多个值 -->
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<!-- Map类型属性注入 -->
<property name="smap">
<!-- Map类型可多内容,用entry标签,以key,value注入 -->
<map>
<entry key="Java" value="java"></entry>
<entry key="Spring" value="spring"></entry>
</map>
</property>
<!-- Set类型属性注入 -->
<property name="set">
<!-- Set类型可多内容,用entry标签,以key,value注入 -->
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
</bean>
</beans>
测试
在集合里面设置对象类型值
package com.junjay.spring5.collectionbean;
// 集合中的对象
public class Cos {
private String cname;
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Cos [cname=" + cname + "]";
}
}
// 在上述Stu中加入集合对象属性 并生成set方法
// 集合对象
private List<Cos> cosList;
public void setCosList(List<Cos> cosList) {
this.cosList = cosList;
}
<!-- 集合类型属性注入 -->
<bean id="stu" class="com.junjay.spring5.collectionbean.Stu">
<!-- List集合类型,值为对象 类型属性注入 -->
<property name="cosList">
<list>
<ref bean="cos1"></ref>
<ref bean="cos2"></ref>
</list>
</property>
</bean>
<!-- 在上述xml配置文件中添加多对象值,注入stu 集合对象中 -->
<!-- 创建多个cos对象通过ref注入到stu list对象中 -->
<bean id="cos1" class="com.junjay.spring5.collectionbean.Cos">
<property name="cname" value="cos1"></property>
</bean>
<bean id="cos2" class="com.junjay.spring5.collectionbean.Cos">
<property name="cname" value="cos2"></property>
</bean>
集合注入部分抽取提取出来
1:创建对象
package com.junjay.spring5.collectionbean;
import java.util.List;
public class Boss {
private List<String> blist;
public void setBlist(List<String> blist) {
this.blist = blist;
}
public void test() {
System.out.println(blist);
}
}
在xml配置文件中添加命名空间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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
// 加命名空间
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
// 加util工具类
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<!-- 1:提取list集合类型属性注入 -->
<util:list id="blist">
<!-- 如果list为对象则用ref -->
<value>1</value>
<value>2</value>
<value>3</value>
</util:list>
<!-- 2:提取list集合类型属性注入使用 -->
<bean id="boss" class="com.junjay.spring5.collectionbean.Boss">
<!-- List类型属性注入 -->
<property name="slist" ref="blist">
</property>
</bean>
</beans>
spring中有俩种bean
一种是普通的bean,我们自己创建的bean
一个是工厂bean(FactoryBean),spring内置bean
1:普通bean:在配置文件中定义的bean类型就是返回的类型
eg:class=”..User>” 则自能返回Userbean对象
2:工厂bean:在配置文件中定义的bean类型可以和返回类型不一样
1:创建类,让类做为工厂bean,实现FactoryBean接口
package com.junjay.spring5.factorybean;
import org.springframework.beans.factory.FactoryBean;
/**
* mybean 普通类先实现FactoryBean接口
* 实现接口后需要重写接口内方法
* @author My
*
*/
public class MyBean implements FactoryBean{
/**
* 设置返回类型
*/
@Override
public Cou getObject() throws Exception {
Cou cou = new Cou();
cou.setCname("222");
return cou;
}
@Override
public Class getObjectType() {
return null;
}
}
package com.junjay.spring5.factorybean;
public class Cou {
private String cname;
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Cou [cname=" + cname + "]";
}
}
2:实现接口里的方法,在实现方法中定义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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<bean id="mybean" class="com.junjay.spring5.factorybean.MyBean">
</bean>
</beans>
bean作用域
1:在spring里面,设置创建bean可以设置是单实例和多实例;
2:spring默认情况下,创建的是单实例对象
3:如何设置单实例,多实例
(1):在spring配置文件中,bean标签内有属性(socpe),用于设置单例还是多例
(2):socpe属性值
2.1 singleton 默认值,单例
2.2 prototype 多例,每次获取重新实例化
2.3 request 每次请求重新实例化
2.4 session 每个会话对象内,对象是单例
2.5 application 在application对象内是单例
2.6 global session 是spring推出的对象,它依赖于spring-webmvc-portlet ,类似于session
配置文件中设置多实例,获取bean对象地址则不相同,也证明了返回对象不是一个对象
(3):singleton和prototype 区别
singleton,表示单实例,prototype ,表示多实例
设置scope值是singleton时,加载spring配置文件时候已经创建单实例对象(饿汉式)
设置prototype 值时,不是在加载spring文件时候创建对象,而是在调用getBean时候进行多实例对象创建(懒汉式)
Bean生命周期
1:生命周期:
(1):从对象的创建到对象销毁的过程
2: bean生命周期
(1):通过构造器创建bean实例(无参构造)
(2):为bean的属性设置值和对其他bean引用(调用set方法)
(3):调用bean的初始化方法(需要进行配置)
(4):bean实例创建成功得到bean对象
(5):当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁方法)
3:演示bean生命周期
【java】this()与super()使用详解
(1):创建对象
package com.junjay.spring5.bean;
public class Ord {
private String oname;
public void setOname(String oname) {
this.oname = oname;
System.out.println("(2):为bean的属性设置值和对其他bean引用(调用set方法)");
}
/**
* 生命周期 (1):通过构造器创建bean实例(无参构造)
*/
public Ord() {
super();
System.out.println("(1):通过构造器创建bean实例(无参构造)");
}
/**
* 调用bean的初始化方法
*/
public void init() {
System.out.println("(3):调用bean的初始化方法(需要进行配置)");
}
/**
* 对象销毁方法,需要在配置文件中配置,在配置文件中配置destroy-method,但不能使其销毁,
* 需要手动让bean实例销毁
*/
public void destroy() {
System.out.println("(5):当容器关闭的时候,调用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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<!--1: 创建bean实例 -->
<bean id="ord" class="com.junjay.spring5.bean.Ord"
init-method="init" destroy-method="destroy"><!-- init-method调用初始化方法 -->
<!-- 2:调用set方法 -->
<property name="oname" value="阿斯蒂"></property>
</bean>
</beans>
@Test测试
使用ApplicationContext接受时,没有close方法,因为ApplicationContext没有close,我们需要用他的实现类,或子接口进行bean销毁。
4:bean后置处理器,bean生命周期变成7步
(1):通过构造器创建bean实例(无参构造)
(2):为bean的属性设置值和对其他bean引用(调用set方法)
(3):在bean初始化之前会调用的一个方法(把bean实例传递bean后置处理器的方法,即BeanPostProcessor接口中的postProcessBeforeInitialization 为初始化之前的方法)
(4):调用bean的初始化方法(需要进行配置)
(5):在bean初始化之后会调用的一个方法把bean实例传递bean后置处理器的方法,即BeanPostProcessor接口中的postProcessAfterInitialization为初始化之前的方法)
(6):bean实例创建成功得到bean对象
(7):当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁方法)
5:演示添加bean后置处理器之后效果
(1):创建类,实现BeanPostProcessor接口,传教后置处理器
package com.junjay.spring5.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* 改类实现BeanPostProcessor接口
* 只要实现改接口在创建所有对象时候都会调用
*
* @author My
*
*/
public class MyBeanPost implements BeanPostProcessor {
/**
* bean是容器调用构造器创建的实例 beanName是实例的id 在所有初始化方法之前调用
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("(2.5)在bean初始化之前会调用的一个方法");
return bean;
}
/**
* bean是容器调用构造器创建的实例 beanName是实例的id 在所有初始化方法之后调用
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("(3.5)在bean初始化之后会调用的一个方法");
return 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<!--1: 创建bean实例 -->
<bean id="ord" class="com.junjay.spring5.bean.Ord"
init-method="init" destroy-method="destroy"><!-- init-method调用初始化方法 -->
<!-- 2:调用set方法 -->
<property name="oname" value="阿斯蒂"></property>
</bean>
<!--配置后置处理器,配置后bean无需引用自动调用 -->
<bean id="myBeanPost" class="com.junjay.spring5.bean.MyBeanPost"></bean>
</beans>
XML自动装配
1:什么是自动装配
(1):根据指定装配规则(属性名称或属性类型),spring自动将匹配的属性值进行注入;
2:演示自动装配过程
创建类
package com.junjay.spring5.autowire;
public class Em {
private Dep dep;
public void setDep(Dep dep) {
this.dep = dep;
}
@Override
public String toString() {
return "Em [dep=" + dep + "]";
}
public void test() {
System.out.println(dep);
}
}
package com.junjay.spring5.bean;
/**
* @author 员工实体
*
*/
public class Emp {
private String ename;
private String gender;
// 员工属于某一部门,使用对象表示
private Dept dept;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDept(Dept dept) {
this.dept = dept;
}
// 生成dept的get方法才能获取到spring配置文件中dept对象的dept.dname
public Dept getDept() {
return dept;
}
public void add() {
System.out.println("ename=" + ename);
System.out.println("gender=" + gender);
System.out.println(dept.toString());
}
}
XML配置文件配置自动装配:autowire标签
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<!-- 实现自动装配
bean标签属性中autowire,配置自动装配
autowire属性常用俩个值:
byname根据属性名称注入; 注入值的bean的id值要和类属性名称一致
bytype根据属性类型注入;
-->
<!-- 根据类型注入 <bean id="em" class="com.junjay.spring5.autowire.Em" autowire="byType"> -->
<bean id="em" class="com.junjay.spring5.autowire.Em" autowire="byName">
<!-- <property name="dep" ref="dep"></property> -->
</bean>
<bean id="dep" class="com.junjay.spring5.autowire.Dep"></bean>
</beans>
引入外部属性文件
1:直接配置数据库信息
(1):配置德鲁伊连接池
(2):引入德鲁伊连接池的jar
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<!-- 直接通过druidjar配置数据库链接 -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<!-- 直接配置 *********************************************************** -->
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> -->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/user"></property> -->
<!-- <property name="username" value="root"></property> -->
<!-- <property name="password" value="root"></property> -->
<!-- 外部配置 ************************************************************** -->
<!-- 获取properties文件中内容,根据key获取,使用spring表达式获取值,进行数据库链接配置 -->
<!-- 驱动名称 com.mysql.jdbc.Driver -->
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<!-- 地址 jdbc:mysql://localhost:3306/user -->
<property name="url" value="${jdbc.url}"></property>
<!-- 链接数据库用户名 -->
<property name="username" value="${jdbc.username}"></property>
<!-- 链接数据库用户密码 -->
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>
2:引入外部属性文件配置数据库连接池
(1):创建外部属性文件,propreties格式文件,写入数据库链接信息
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/test_demo
prop.username=root
prop.password=root
(2):xml配置文件中添加context命名空间,解析上下文
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
">
<!-- 使用context标签,引入外部文件,外部配置 -->
<context:property-placeholder
location="classpath:jdbc.properties" />
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<!-- 获取properties文件中内容,根据key获取,使用spring表达式"${}"获取值,进行数据库链接配置 -->
<!-- 驱动名称 com.mysql.jdbc.Driver -->
<property name="driverClassName" value="${prop.driverClass}"></property>
<!-- 地址 jdbc:mysql://localhost:3306/user -->
<property name="url" value="${prop.url}"></property>
<!-- 链接数据库用户名 -->
<property name="username" value="${prop.username}"></property>
<!-- 链接数据库用户密码 -->
<property name="password" value="${prop.password}"></property>
</bean>
</beans>
单元测试: github:https://github.com/My-Jun/spring5_demo1/tree/master