Spirng Use In Other Times (补充)
来源:互联网 发布:大量商家退出淘宝 编辑:程序博客网 时间:2024/06/06 20:10
大家都知道web应用在启动时,Spring的listener在启动时会找到Spring的配置文件,然后spring 的bean factory
生产bean,并压入servletConext作用域内,所以在只要能取得servletConext的环境中就很容易取到这些bean,
Spring也提供了WebApplicationContextUtils 工具类。
以下引自:http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/
#############################################################
当 Web 应用集成 Spring 容器后,代表 Spring 容器的 WebApplicationContext 对象将以
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 为键存放在 ServletContext 属性列表中。您
当然可以直接通过以下语句获取 WebApplicationContext:
WebApplicationContext wac = (WebApplicationContext)servletContext.
getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
但通过位于 org.springframework.web.context.support 包中的 WebApplicationContextUtils 工具类获取
WebApplicationContext 更方便:
WebApplicationContext wac =WebApplicationContextUtils.
getWebApplicationContext(servletContext);
#############################################################
所以在jsp, servlet, filter, listener中都能很方便地取到servletConext,当然也可以很方便地取得实例化好的
bean。但是在javabean里就比较麻烦了。
当然可以参见下文:
以下引自:http://wiki.springside.org.cn/display/springside/Spirng+Use+In+Other+Times
#############################################################
Spirng Use In Other Times
Spring再强大,也要面对降临的问题--因为Spring不是Weblogic、Tomcat般的顶层容器,Servlet和EJB对象不由它
创建,所以它必须要降临到Weblogic、Tomcat所在的位面。
初学者一般不用管那么多,照着Spring+hibernate+Struts之类的Sample就做了,但慢慢的,也许就要开始在
jsp+javabean体系,土制框架,singleton类等环境下使用Spring了。
《Professional Java Development with the Spring Framework》第3章有"Managing the Containe"一节讲这个问
题。一般可以分为直接召唤系与IoC fashion两类。
#############################################################
在javabean里直接使用 ApplicationContext ctx = new ClasspathXmlApplicationContext
("ApplicationContext.xml");无疑是最简单的。其它的方法稍显麻烦
在我们的一个应用中,使用到了定时任务,当然也是用spring封装的,直接使用javabean作为任务。在定时任务中
我们想操作数据库,而操作数据库的service都配置在spring中,这样形成了一个死循环。如果使用上面的方法,每
次任务执行时,又要把所有的bean重新“生产”一次,效率肯定不高。
现实中,我们是这样解决的
1、分配置文件
spring-dao.xml
daos services
spring-conext.xml(把spring-dao.xml import进来)
dataSource,事务,等
spring-task.xml
执行任务的javabean,jobDetails triggers, SchedulerFactoryBean
- public interface AutoRunService {
- void doSomething();
- }
2、web.xml的configLocation中写上上面两个文件
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-context.xml,classpath:spring-task.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
3、如果采用直接招唤的方法
直接在javabean中
- ApplicationContext ctx = new ClasspathXmlApplicationContext("spring-conext.xml");
下在就可以操作数据库了。
4、不用第三种方法,我们注意到执行任务的javabean也是在容器中“生产”了,如果知道它所在的容器就好了。
正好发现有个ApplicationContextAware接口,实现此接口的类必须要实现 setApplicationContext(ApplicationContext applicationContext)方法,传入applicationContext,这正是我想要的,当然这个javabean必须也是在这个容器中生产的。于是有:
- public class AutoRunServiceDefaultImpl implements AutoRunService, ApplicationContextAware {
- XxxService xxxService;
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- xxxService = (XxxService) applicationContext.getBean("xxxService");
- }
- public void doSomething() {
- //xxxService.xxx();
- }
虽然,引入了Spring的API,破坏了此JavaBean的“纯洁”,但是还是值得的,比直接召唤的性能上要好很多。
- Spirng Use In Other Times (补充)
- Message 'You cannot use the EurekaLog package in other packages'.
- Use ResultSet repeated (multiple times)
- other use google search
- Delete a file in use - Run an EXE Hidden - Run code in other process' memory)(vb)
- 安装或者卸载Directx SDK时候,出现‘d3d10sdklayers.dll' is currently in use by other applications
- Could not access the hardware (it is possibly in use by other application)
- git use commit times for timestamps
- (16)spirng注解
- Spirng+In+Action(Craig Walls Ryan Breidenbach)
- How to create aligned partitions in Linux for use with NetApp LUNs, VMDKs, VHDs and other virtual di
- spirng整合redis (二)
- use pipe to contact with each other
- Finding a number that repeats even no of times where all the other numbers repeat odd no of times
- Address already in use解决方法(ZZ)
- Address already in use: JVM_Bind(解决方法)
- 7.5.1 Point-in-Time Recovery Using Event Times 使用Event Times 基于时间点恢复
- Spirng实战学习笔记(转载)
- 利用TestDriven.net和NUnit进行单元测试(收)
- JavaScript与C# Windows应用程序交互
- BUGZERO安装配置(一)——TOMCAT5.5安装
- java中常用的匹配正则表达式实例大全
- SQLOLEDB and SQLOLEDB.1
- Spirng Use In Other Times (补充)
- CDirectoryChangeWatcher - ReadDirectoryChangesW all wrapped up
- frameset 的一些使用技巧
- Java正则表达式详解
- 淡淡的友情纯净而又绵长
- C#的插件开发
- 通过存储过程实现报表复杂查询并以Excel格式输出
- 制作可引导的启动软盘
- Classpath和Codebase