1.spring作用域:
其中spring容器作用域中singleton是在bean配置中如下:
<!-- singleton的配置--> <bean id="bean1" class="com.my.test.Bean1" scope="singleton"/> <!-- 或者--> <bean id="bean1" class="com.my.test.Bean1" singleton="true"/> <!--prototype配置--> <bean id="bean1" class="com.my.test.Bean1" scope="prototype"/> <!-- 或者--> <bean id="bean1" class="com.my.test.Bean1" singleton="false"/>
web容器的作用域使用
- 首先配置web.xml如果使用的是Servlet2.4及以上版本只需要在web.xml中增加下述的ContextListener
<listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>
如果使用的是Servlet2.4一下版本则需要配置一个javax.servlet.Filter
<filter> <filter-name>requestContextFilter</filter-name> <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> </filter> <filter-mapping> <filter-name>requestContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
- 配置bean
<bean id="bean1" class="com.my.test.Bean1" scope="request"/>
其他的类似,其中global session
spring默认的bean都是singleton的,如有需要使用非单例可以参考prototype的配置
也可以通过Scope类定义自己的作用域
2.spring支持事件传递机制
- 首先第一事件类型:事件类继承ApplicationEvent类
- 实现监听类:监听类实现ApplicationListener接口
- 通过ApplicationContext的publishEvent()方法发布事件