本文需要的例子是在“spring 入門例子--spring注入方式”基礎上
spring 入門例子--spring注入方式
Scope作用域
-
創(chuàng)建BeanScope類
package com.younghare.bean;
public class BeanScope {
public void sayHello() {
//hashcode可以區(qū)分實例德召;創(chuàng)建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>
-###創(chuàng)建測試用例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() {
//創(chuàng)建2次查看測試實例的hachcode是否相同,如果相同這標識同一個實例
BeanScope beanScope =super.getBean("beanScope");
beanScope.sayHello();
BeanScope beanScope2 =super.getBean("beanScope");
beanScope2.sayHello();
}
}
-
運行測試用例
image.png
-### 修改Scope ="prototype"
在運行測試用例
image.png
Bean生命周期-配置方式1
-
創(chuàng)建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>
image.png
-
創(chuàng)建單元測試類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");
}
}
測試結果
image.png
Bean生命周期-實現接口方式2
-###BeanLiftCycle實現接口InitializingBean,DisposableBean
接口是
org.springframework.beans.factory.DisposableBean;
org.springframework.beans.factory.InitializingBean;
image.png
-
修改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>
運行測試用例
image.png
Bean生命周期-默認方式3(默認方法省略。肴掷。敬锐。)
默認方法注意在bean中提供對應的接口
image.png
當3上方式方式同時使用時
當3上方式方式同時使用時順序是
接口的方式》配置的init-method 和destory-method
而默認的初始化就不生效
Aware(實現Aware接口可以獲取相應資源)
接口可以獲得ioc容器
org.springframework.context.ApplicationContextAware
可以攔截設置BeanName 的
org.springframework.beans.factory.BeanNameAware
-
創(chuàng)建類BeanApplicationContextAware
public class BeanApplicationContextAware implements ApplicationContextAware{
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("setApplicationContext方法中"
+arg0.getBean("beanApplicationContextAware").hashCode());
}
}
-
創(chuàng)建類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>
-
測試用例
image.png
image.png