Spring入门——(五、Bean(二))

>Bean的自动装配
>Resources&ResourcesLoader
>Bean的配置项
》Id Bean的唯一标识
》*Class 具体实例化的类
》Scope 作用域
》Constructor arguments 构造器的参数
》Properties Bean的属性(构造注入、设值注入用过)
》Autowiring mode 自动装配模式
》lazy_initialization mode 软加载模式
》Initialization/destruction method 初始化/销毁的方法

>Bean的作用域
》singleton:单例,指一个Bean容器中只存在一份
》prototype:每次请求(每次使用)创建新的实例,destory方式不生效
》request:每次http请求创建一个实例且仅在当前request内有效
》session:同上,每次http请求创建,当前session内有效
》global session:基于portlet的web中有效(portlet定义了global session),如果是在web中,同 session

>Bean的生命周期
》定义
》初始化
- 实现org.springframework.beans.factory.InitializingBean接口,覆盖afterPropertiesSet方法
- 配置init - method
》使用




》销毁
- 实现org.springframework.beans.factory.DisposableBean接口,覆盖destory方法
- 配置destory-method

》配置全局默认初始化、销毁方法

示例:

spring-lifecycle.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
	
	<bean id="beanLifeCycle" class="com.imooc.life.BeanLifeCycle" init-method="init" destroy-method="stop"></bean>

</beans>

BeanLifeCycle类:

package com.imooc.life;

public class BeanLifeCycle {
	public void start() {
		
		System.out.println("Bean start");
	}
	
	public void stop() {
		
		System.out.println("Bean stop");
	}
}

测试类:

package com.imooc.test.lifecycle;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import junit.framework.TestCase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanLifeCycle extends UnitTestBase{
	
	public TestBeanLifeCycle() {
		super("classpath:spring-lifecycle.xml");
	}
	@Test
	public void test() {
		super.getBean("beanLifeCycle");
	}
}


输出结果为:


示例2:

BeanLifeCycle类:

package com.imooc.life;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BeanLifeCycle implements InitializingBean,DisposableBean{
	/*public void start() {
		
		System.out.println("Bean start");
	}
	
	public void stop() {
		
		System.out.println("Bean stop");
	}*/

	@Override
	public void destroy() throws Exception {
		System.out.println("Bean destory");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Bean afterPropertiesSet");
	}
}

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
	
	<!-- <bean id="beanLifeCycle" class="com.imooc.life.BeanLifeCycle" init-method="init" destroy-method="stop"></bean> -->
	<bean id="beanLifeCycle" class="com.imooc.life.BeanLifeCycle" ></bean>
</beans>

测试类:

package com.imooc.test.lifecycle;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanLifeCycle extends UnitTestBase{
	
	public TestBeanLifeCycle() {
		super("classpath:spring-lifecycle.xml");
	}
	@Test
	public void test() {
		super.getBean("beanLideCycle");
	}
}

测试结果:

七月 09, 2018 11:04:59 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7dc5e7b4: startup date [Mon Jul 09 23:04:59 GMT+08:00 2018]; root of context hierarchy
七月 09, 2018 11:04:59 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-lifecycle.xml]
Bean afterPropertiesSet
七月 09, 2018 11:05:00 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7dc5e7b4: startup date [Mon Jul 09 23:04:59 GMT+08:00 2018]; root of context hierarchy
Bean destory
如果缺少一个工具类,请看博主里面的内容:有一篇UnitTestBase的工具类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值