【Spring編】二.スプリングビームのライフサイクルとAwareインターフェース
12347 ワード
1.ライフサイクル
1.1定義
3.1定義類はこのインターフェースを実現する
1.1定義
<bean id="student" class="com.wpj.bean.Student">bean>
1.2初期化spring ,
: org.springframework.beans.factory.InitializingBean , afterPropertiesSet
, afterPropertiesSet
: xml init-method="bean " ( )
spring bean init-metod
<bean id="student" class="com.wpj.bean.Student" init-method="init" />
public class Student{
public void init(){
//...
}
}
1.3使用 bean , 。
@Test
public void showSpringBean(){
// spring
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
// id bean
Student student = (Student) ac.getBean("student");
System.out.println(student);
}
1.4廃棄 spring bean
: org.springframework.beans.factory.DisposableBean , destroy
, destroy
: xml destroy-method="bean "
spring bean destroy-metod
<bean id="student" class="com.wpj.bean.Student" destroy-method="init" />
public class Student{
public void destroy(){
//...
}
}
1.5グローバルデフォルト初期化と廃棄方法を設定する bean init , destroy 。( )
<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.xsd"
default-init-method="init" default-destroy-method="destroy">
bean>
:
,
2.AwareSpring Aware , Aware bean , 。
, spring ( )
spring 。
spring
2.1 ApplicationContextAware
bean ApplicationContext , bean spring spring 。
2.1 BeanNameAware
bean BeanName , bean spring spring 。
インターフェースApplicationContextAware
ApplicationEventPublisherAware
BeanClassLoaderAware
BeanFatoryAware
BeanNameAware
BootstrapContextAware
LoadTimeWeaverAware
MessageSourceAware
NotificationPublisherAware
PortletConfigAware
PortletContextAware
ResourceLoaderAware
ServletConfigAware
ServletContextAware
3.判例:Application Contect Aware3.1定義類はこのインターフェースを実現する
public class TestApplicationContextAware implements ApplicationContextAware{
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("applicationContext = " + applicationContext.getBean("testInter"));
System.out.println("applicationContext = " + applicationContext.hashCode());
}
}
3.2 spring-aware.xmlにあります. <bean id="testApplicationContextAware" class="com.wpj.aware.TestApplicationContextAware">bean>
3.3 Testpublic class AwareTest {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-aware.xml");
}
}
現在はこのインターフェースを理解していませんので、簡単にデモンストレーションします.後は補充します.....