Spring bean 通過實現 InitializingBean ,DisposableBean 介面實現初始化方法和銷燬前操作
阿新 • • 發佈:2019-01-01
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- <context:component-scan base-package="com.myapp.core.jsr330"/> --> <!-- <context:annotation-config /> --> <!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="personService" class="com.myapp.core.annotation.init.PersonService"> <property name="message" value="123"></property> </bean> --> <bean id="personService" class="com.myapp.core.annotation.init.PersonService"> <property name="message" value="123"></property> </bean> </beans>
3:測試類:
package com.myapp.core.annotation.init; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml"); PersonService personService = (PersonService)context.getBean("personService"); context.registerShutdownHook(); } }
4:輸出測試結果:
I'm init method using implements DisposableBean interface....123 三月 16, 2013 5:06:34 下午 org.springframework.context.support.AbstractApplicationContext doClose INFO: Closing org[email protected]205756: startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy I'm init method using implements InitializingBean interface....123
over