本文需要的例子是在“spring 入门例子--spring注入方式”基础上
spring 入门例子--spring注入方式
Scope作用域
-
创建BeanScope类
package com.younghare.bean;
public class BeanScope {
public void sayHello() {
//hashcode可以区分实例;创建2次查看测试实例的hachcode是否相同,如果相同这标识同一个实例
System.out.println("BeanScope Sayhello 实例hashcode:"+this.hashCode());
}
}
copy applicationContext.xml ->spring-beanscope.xml
-
配置scopye为单例模式
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="beanScope"
class ="com.younghare.bean.BeanScope"
scope="singleton">
</bean>
</beans>
-###创建测试用例UnitTestBaseScopeTest
package com.younghare;
import static org.junit.Assert.*;
import org.junit.Test;
import com.younghare.bean.BeanScope;
public class UnitTestBaseScopeTest extends UnitTestBase {
public UnitTestBaseScopeTest() {
super("classpath:spring-beanscope.xml");
}
@Test
public void testSayHello() {
//创建2次查看测试实例的hachcode是否相同,如果相同这标识同一个实例
BeanScope beanScope =super.getBean("beanScope");
beanScope.sayHello();
BeanScope beanScope2 =super.getBean("beanScope");
beanScope2.sayHello();
}
}
-
运行测试用例
-### 修改Scope ="prototype"
在运行测试用例
Bean生命周期-配置方式1
-
创建BeanLiftCycle类
package com.younghare.bean;
public class BeanLiftCycle {
public void start() {
System.out.println("Bean start 在xml中已经配置初始化方法init-method");
}
public void stop() {
System.out.println("Bean stop 在xml中已经配置初始化方法destroy-method");
}
}
-
配置文件spring-lifecycle.xml(类似applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!--配置 bean的初始化、销毁方法 -->
<bean id="beanScope"
class ="com.younghare.bean.BeanLiftCycle"
init-method="start"
destroy-method="stop"
>
</bean>
</beans>
-
创建单元测试类TestBeanLiftCycle
package com.younghare;
import static org.junit.Assert.*;
import org.junit.Test;
import com.younghare.bean.BeanLiftCycle;
public class TestBeanLiftCycle extends UnitTestBase {
public TestBeanLiftCycle() {
super("classpath:spring-lifecycle.xml");
}
@Test
public void testLiftCycle() {
BeanLiftCycle beanLiftCycle = super.getBean("beanLiftCycle");
}
}
测试结果
Bean生命周期-实现接口方式2
-###BeanLiftCycle实现接口InitializingBean,DisposableBean
接口是
org.springframework.beans.factory.DisposableBean;
org.springframework.beans.factory.InitializingBean;
-
修改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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!--不去--配置 bean的初始化、销毁方法 -->
<!-- init-method="start" destroy-method="stop" -->
<bean id="beanLiftCycle"
class ="com.younghare.bean.BeanLiftCycle"
>
</bean>
</beans>
运行测试用例
Bean生命周期-默认方式3(默认方法省略。。。)
默认方法注意在bean中提供对应的接口
当3上方式方式同时使用时
当3上方式方式同时使用时顺序是
接口的方式》配置的init-method 和destory-method
而默认的初始化就不生效
Aware(实现Aware接口可以获取相应资源)
接口可以获得ioc容器
org.springframework.context.ApplicationContextAware
可以拦截设置BeanName 的
org.springframework.beans.factory.BeanNameAware
-
创建类BeanApplicationContextAware
public class BeanApplicationContextAware implements ApplicationContextAware{
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("setApplicationContext方法中"
+arg0.getBean("beanApplicationContextAware").hashCode());
}
}
-
创建类BeanNameAware
-
spring 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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="beanApplicationContextAware"
class ="com.younghare.bean.BeanApplicationContextAware"
>
</bean>
</beans>
-
测试用例