注意:spring
若是一個類須要獲取ApplicationContext實例時,能夠讓該類實現ApplicationContextAware接口:apache
public class Test implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext context) throws Exception { this.applicationContext = context; } }
BeanNameAware接口 當Bean須要獲取自身在容器中的id/name時,能夠實現BeanNameAware接口app
InitializingBean接口 當須要在Bean的所有屬性設置成功後作些特殊處理,能夠讓該Bean實現InitializingBean接口。效果等同於bean的init-method屬性的使用或者@PostConstruct註解this
執行順序:先執行InitializingBean接口中定義的afterPropertiesSet()方法,後執行init-method或者@PostConstruct註解的方法url
執行順序:先註解,後DisposableBean接口定義的方法,最後執行destroy-method引用的方法。spa
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.className}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
PropertyPlaceholderConfigurer另外一種精簡配置方式(context命名空間)
<context:property-placeholder location="classpath:jdbc.properties, classpath:mails.properties" />