1. 创建Bean
1.1 使用默认构造函数创建(最常用)
在spring的配置文件中使用bean标签,配以class和id属性之后,且没有其他属性和标签时,采用的是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建
<bean id="accountService" class="com.dudu.service.impl.AccountServiceImpl" ></bean>
public class AccountServiceImpl implements IAccountService {
//当缺少默认构造函数时,上面Bean会创建失败
public AccountServiceImpl(){
System.out.println("默认构造函数对象创建对象……");
}
public AccountServiceImpl(String name){
System.out.println("对象创建……");
}
}
1.2 使用普通工厂中的方法
使用某个类中的方法创建对象并存入Spring容器
<!-- 创建工厂Bean -->
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<!-- factory-bean指定使用的工厂Bean,传值为工厂Bean的id;
factory-method指定使用的创建Bean的方法,传值为方法名 -->
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
如果需要向方法中传入参数,则使用<constructor-arg></constructor-arg>
标签
public class InstanceFactory {
public IAccountService getAccountService() {
return new AccountServiceImpl();
}
}
1.3 使用静态工厂中的静态方法创建对象
使用某个类中的静态方法创建对象并存入Spring容器
<!-- class指定使用的静态工厂类,传值为静态工厂类的全限定类名;
factory-method指定使用的创建Bean的方法,传值为方法名 -->
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
如果需要向方法中传入参数,则使用<constructor-arg></constructor-arg>
标签
public class StaticFactory {
public static IAccountService getAccountService() {
return new AccountServiceImpl();
}
}
1.4 使用FactoryBean
//自定义FactoryBean
public class CarFactoryBean implements FactoryBean<Car> {
private String brand;
public void setBrand(String brand) {
this.brand = brand;
}
@Override
public Car getObject() throws Exception {
return new Car(brand, 400000.0);
}
@Override
public Class<?> getObjectType() {
return Car.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
<bean id="car" class="com.dudu.beans.CarFactoryBean">
<property name="brand" value="BMW"/>
</bean>
2. 调整Bean的作用范围(scope属性)
- singleton:单例的(默认值)
- prototype:多例的
- request:作用于web应用的请求范围
- session:作用于web应用的会话范围
- global-session:作用于集群环境的会话范围(全局会话范围,当不是集群环境时,它等同于session)
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="singleton"></bean>
3. Bean对象的生命周期
单例对象
出生:当容器创建时,对象就已经创建
活着:只要容器还在,对象一直活着
死亡:容器销毁,对象消亡
总结:单例对象的生命周期和容器相同
多例对象
出生:当使用对象时,spring框架才创建
活着:对象在使用过程中一直活着
死亡:当对象长时间不用且没有别的对象引用时,由Java的垃圾回收器回收
<!-- init-method指定创建Bean时执行的方法,传值为方法名
destroy-method指定销毁Bean时执行的方法,传值为方法名-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="singleton"
init-method="init" destroy-method="destory">
<property name="name" value="费渡"/>
</bean>
测试打印:
Constructor…
setName…
init…
com.itheima.service.impl.AccountServiceImpl@6fd02e5
destroy…
4. 创建Bean的后置处理器
自定义后置处理器(对所有的Bean都生效)
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization..." + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization..." + beanName);
return bean;
}
}
配置后置处理器
<!-- 无需指定id,容器可以直接识别 -->
<bean class="com.itheima.service.MyBeanPostProcessor"></bean>
测试打印:
Constructor…
setName…
postProcessBeforeInitialization…accountService
init…
postProcessAfterInitialization…accountService
com.itheima.service.impl.AccountServiceImpl@6fd02e5
destroy…