注意: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" />