IOC (applicationContext.xml) 使用

现在applicationContext.xml定义bean,接下来

        //初始化容器
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		//获取对象
        ScrenDriver screnDriver = context.getBean("screnDriver", ScrenDriver.class);
        //使用对象
        screnDriver.user();

Bean有效范围

  • singleton (单例(只有一个实例))
 <bean id="screnDriver" class="cn.test.controller.ScrenDriver" scope="singleton"></bean>

-prototype (每次引用都创建一个新的实例)

  <bean id="screnDriver" class="cn.test.controller.ScrenDriver" scope="prototype"></bean>

Bean生命周期回调

-创建

public interface InitializingBean{
	void afterPropertiesSet() throws Exception;
}

<bean id="screnDriver" class="cn.test.controller.ScrenDriver" init-method="init">
 
public class init {
    public void init() {
        System.out.println("init");
    }
}

-销毁

public interface DisposableBean{
	void destroy() throws Exception;
}

<bean id="screnDriver" class="cn.test.controller.ScrenDriver" destroy-method="cleanup">

public class cleanup {
    public void cleanup() {
        System.out.println("clean");
    }
}

容器关闭

((ConfiggurableApplicationContext)context).close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值