Spring属性载器的用法
1、在配置文件里配置类以及properties的位置如下
<!-- 属性加载器的使用 -->
<bean id="propertyConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>SpringConfig.parameter.properties</value>
</property>
</bean>
2、在Bean里使用美元符获取获取的参数
<bean id="initializingBean" class="com.cvicse.steve.springframework.inaction.context.StartLoadBean" init-method="init">
<property name="name">
<value>${ProjectName}</value>
</property>
</bean>
InitializingBean接口与init-method方法的用法与区别
1、InitializingBean接口
实现InitializingBean接口为Bean提供初始化方法,在Bean初始始化的时候,调用afterPropertiesSet()方法,在xml当中不需要做额外的配置。
2、init-method
通过xml配置Bean的,init-method,同样是为Bean提供初始化方法,在Bean初始化之后,首先执行该方法。与实现InitializingBean接口相比,init-metod是与spring松耦合;同时,底层是使用反射写的,固较实现InitializingBean接口效率低。
ApplicationContext的事件
使Bean具有处理容器发布事件的能力,同时应用系统也可以发布自己的事件,是通过Spring的ApplicationListener来实现处理。
1、自定义事件
实现一个事件类,继承ApplicationEvent
2、实现监听器
public interface ApplicationListener<E extends ApplicationEvent>的onApplicationEvent(E event)方法。(注:这里可以学习到一点泛式的用法)
3、发布事件
当调用 ApplicationContext的publishEvent(ApplicationEvent event)方法时,监听器将会执行onApplicationEvent方法。
ContextLoader用法
手动编写一个监听器,用来启动Spring Web服务
1、ServletContextListener接口
实现 ServletContextListener接口的contextInitialized方法
2、ContextLoader用法
this.contextLoader=new ContextLoader(); this.contextLoader.initWebApplicationContext(event.getServletContext());