普通的依赖注入
属性是类
-》引用外部bean,将value标签改为ref
<bean id="dep" class="com.test.ioc.Dept">
<property name="dname" value="测试部"></property>
</bean>
<bean id="empl" class="com.test.ioc.Emp">
<property name="ename" value="John"></property>
<property name="dept" ref="dep"></property>
</bean>
-》内部bean,直接写 bean标签
<bean id="empl" class="com.test.ioc.Emp">
<property name="ename" value="John"></property>
<property name="dept">
<bean id="dep" class="com.test.ioc.Dept">
<property name="dname" value="测试部"></property>
</bean>
</property>
</bean>
-》级联赋值
<bean id="dep" class="com.test.ioc.Dept">
<property name="dname" value="测试部"></property>
</bean>
<bean id="empl" class="com.test.ioc.Emp">
<property name="ename" value="John"></property>
<property name="dept" ref="dep"></property>
<!--会改变dep标签实体的值-->
<property name="dept.dname" value="开发部"></property>
</bean>
属性是数组,使用array标签
<bean id="dep" class="com.test.ioc.Dept">
<property name="dname" value="测试部"></property>
</bean>
<bean id="empl" class="com.test.ioc.Emp">
<property name="ename" value="John"></property>
<property name="dept" ref="dep"></property>
<property name="hobbys">
<array>
<value>篮球</value>
<value>看书</value>
<value>写字</value>
</array>
</property>
</bean>
属性是集合
-》List,使用list标签
<bean id="empl1" class="com.test.ioc.Emp">
<property name="ename" value="John"></property>
</bean>
<bean id="empl2" class="com.test.ioc.Emp">
<property name="ename" value="Amy"></property>
</bean>
<bean id="dep" class="com.test.ioc.Dept">
<property name="dname" value="测试部"></property>
<property name="empls">
<list>
<ref bean="empl1"></ref>
<ref bean="empl2"></ref>
</list>
</property>
</bean>
-》属性是Map,使用map 和 entry 标签
<bean id="empl1" class="com.test.ioc.Emp">
<property name="ename" value="John"></property>
</bean>
<bean id="empl2" class="com.test.ioc.Emp">
<property name="ename" value="Amy"></property>
</bean>
<bean id="dep" class="com.test.ioc.Dept">
<property name="dname" value="测试部"></property>
<property name="empls">
<map>
<entry>
<key><value>S1</value></key>
<ref bean="empl1"></ref>
</entry>
<entry>
<key><value>S2</value></key>
<ref bean="empl2"></ref>
</entry>
</map>
</property>
</bean>
-》统一使用util标签
<bean id="empl1" class="com.test.ioc.Emp">
<property name="ename" value="John"></property>
</bean>
<bean id="empl2" class="com.test.ioc.Emp">
<property name="ename" value="Amy"></property>
</bean>
<bean id="dep" class="com.test.ioc.Dept">
<property name="dname" value="测试部"></property>
<property name="emplMap" ref="mapTest"></property>
</bean>
<util:map id="mapTest">
<entry>
<key><value>S1</value></key>
<ref bean="empl1"></ref>
</entry>
<entry>
<key><value>S2</value></key>
<ref bean="empl2"></ref>
</entry>
</util:map>
<util:list id="listTest">
<ref bean="empl1"></ref>
<ref bean="empl2"></ref>
</util:list>
PS:注意,使用util需要修改XML约束为
<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
http://www.springframework.org/schema/util/spring-util.xsd">
P命名空间
<bean id="empl1" class="com.test.ioc.Emp">
<property name="ename" value="John"></property>
</bean>
<bean id="empl2" class="com.test.ioc.Emp">
<property name="ename" value="Amy"></property>
</bean>
<bean id="dep" class="com.test.ioc.Dept"
p:dname="开发部" p:emplList-ref="listTest" p:emplMap-ref="mapTest">
</bean>
<util:map id="mapTest">
<entry>
<key><value>S1</value></key>
<ref bean="empl1"></ref>
</entry>
<entry>
<key><value>S2</value></key>
<ref bean="empl2"></ref>
</entry>
</util:map>
<util:list id="listTest">
<ref bean="empl1"></ref>
<ref bean="empl2"></ref>
</util:list>
PS:注意,使用p需要增加XML约束:
xmlns:p="http://www.springframework.org/schema/p"
<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"
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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
引入外部文件与使用
有一个文件:
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--引入外部文件-->
<context:property-placeholder location="classpath:out.properties"></context:property-placeholder>
<!--定义bean-->
<bean id="empl1" class="com.test.ioc.Emp">
<property name="ename" value="${test-name1}"></property>
</bean>
<bean id="empl2" class="com.test.ioc.Emp">
<property name="ename" value="${test-name2}"></property>
</bean>
</beans>
测试结果:
bean的作用域
scope属性
singleton | 单实例 | 在IOC容器初始化的时候创建,为默认值 |
prototype | 多实例 | 获取bean的时候创建 |
多实例:
单实例:
生命周期
- 调用无参构造函数创建对象
- 设置相关属性
- 后置处理器(初始化之前)
- 调用指定初始化方法进行初始化
- 后置处理器(初始化之后)
- 创建完成,可以进行使用
- 对象销毁(配置指定销毁方法)
- Ioc容器关闭
测试类:
public class User {
public User()
{
System.out.println("1.调用无参构造函数创建对象....");
}
public void init(){
System.out.println("4.初始化.....");
}
public void destory(){
System.out.println("7.对象销毁.....");
}
private String name;
public String getName() {
System.out.println("获取");
return name;
}
public void setName(String name) {
System.out.println("2.设置相关属性....");
this.name = name;
}
}
public class MyBeanPost implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("3.后置处理器(初始化之前).....");
return null;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("5.后置处理器(初始化之后).....");
return null;
}
}
测试结果:
FactoryBean
- FactoryBean是一个可生产Bean的工厂Bean,动态生成某一个类型的Bean的实例。如果一个Bean实现了FactoryBean接口,那么这个Bean就变成了一个工厂,根据Bean的名称获取到的实际上是工厂调用getObject()返回的对象,而不是Bean本身,如果要获取工厂Bean自身的实例,那么需要在名称前面加上'&'符号。
- FactoryBean在以下情况下常常被使用:
-》创建复杂的Bean对象:FactoryBean可以用于创建复杂的对象,如代理对象、动态代理等。
-》对象的定制化创建:FactoryBean可以用于创建需要特定定制过程的对象,例如需要一些额外的配置或者初始化过程。
-》对象的包装和增强:FactoryBean可以用于包装和增强对象,通过在getObject()方法中返回增强后的对象。
测试类:
public class User {
public User()
{
System.out.println("1.调用无参构造函数创建对象....");
}
public void init(){
System.out.println("4.初始化.....");
}
public void destory(){
System.out.println("7.对象销毁.....");
}
private String name;
public String getName() {
System.out.println("获取");
return name;
}
public void setName(String name) {
System.out.println("2.设置相关属性....");
this.name = name;
}
}
public class MyFactoryBean implements FactoryBean<User> {
public User getObject() throws Exception {
return new User();
}
public Class<?> getObjectType() {
return User.class;
}
}
测试结果:
自动装配
根据指定的策略,在IOC容器里匹配某一个bean,自动为指定的bean中所依赖的类类型或接口类型属性赋值