- 浏览: 898218 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (498)
- J2EE (52)
- 数据库 (17)
- java基础 (43)
- web技术 (19)
- 程序设计 (6)
- 操作系统 (18)
- IT资讯 (7)
- 我的IT生活 (12)
- 学习笔记 (9)
- Jquery (25)
- JavaScript (18)
- spring (40)
- Hibernate (12)
- Struts (10)
- YUI (2)
- Extjs (22)
- .net (0)
- Eclipse (10)
- 社会主义 (2)
- 服务器 (9)
- CSS (8)
- 网络安全 (16)
- 版本控制 (9)
- PHP (2)
- Oracle (42)
- SQL server (1)
- Mysql (11)
- 项目管理 (3)
- 开发工具使用 (10)
- SQL语句 (7)
- Perl (0)
- Shell (6)
- 漏洞 (4)
- ibatis (5)
- hacker (2)
- SQL注入 (6)
- Hacker工具 (2)
- 入侵和渗透 (7)
- 插件/组件 (2)
- 最爱开源 (5)
- 常用软件 (2)
- DOS (1)
- HTML (2)
- Android (9)
- CMS (1)
- portal (8)
- Linux (7)
- OSGI (1)
- Mina (5)
- maven (2)
- hadoop (7)
- twitter storm (2)
- sap hana (0)
- OAuth (0)
- RESTful (1)
- Nginx (4)
- flex (1)
- Dubbo (1)
- redis (1)
- springMVC (1)
- node.js (1)
- solr (2)
- Flume (1)
- MongoDB (2)
- ElasticSearch (1)
最新评论
-
M_drm:
请问要怎么设置浏览器才不报没权限呢?
用JS在页面调用本地可执行文件的方法(ACTIVEX) -
Alexniver:
官方文档。When importing data into I ...
mysql导入数据过慢 解决方法 -
camelwoo:
我记得 Criteria 可以做连接查询与子查询,也可以做分页 ...
Hibernate总结篇二 -
zhenglongfei:
楼主如果SubKeyName 这个节点不存在,怎么办??怎么用 ...
Java操作注册表 -
yxx676229549:
用log4j 2 了
logback
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两类。
1.直接召唤系--Singleton的Application Context
最简单的,就像在UnitTest里那样,直接构造Application Context:
在Web环境里,会使用ContextLoader构造ApplicationContext后,压进Servlet Context。
由ContextLoaderListener或ContextLoaderServlet,在Web应用启动时完成。
然后在Jsp/Servelet中,可以通过Servlet Context取得ApplicationContext:
但像singleton类或者EJB中,就没有Servlet Context可用了。
如果全部像UnitTest那样直接构造,速度就会很不堪。自然的,就想到把ApplicationContext做成单例。
Spring提供了ContextSingletonBeanFactoryLocator这样的物体。
先搞一个beanRefFactory.xml,里面写上所有的applcationContext-*.xml文件名,并把Context命名为"default-context":
然后让loactor去找它,但代码有点长:
上面的代码实在是太灵活,太麻烦了。
还不如自己实现一个简单的Singleton,扩展ContextLoaderListener类,在Web系统启动时压入Singleton。
新的ContextLoaderListener类重载如下,ContextUtil中包含一个静态的ApplicationContext变量:
用家可直接取用:
2.IoC fashion
如果所有地方都使用直接召唤系,那就反而是在打Rod的耳光了。因为他一直都反对代码与框架深耦合的。
所以,更好的方法是写一些glue code、base class来完成Spring的降临,而不让应用代码察觉Spring Application Context的存在。
不过,因为各个框架的结构不同,Rod也没办法讲出一个通用的整合方法,所以建议大家尽量学习已整合的各种框架,如Spring MVC、Struts的种种方式,写出自己的简单整合代码来。
只有不确定的调用某些Singleton类,不适合过早ioc的情况,可以使用直接召唤系。
初学者一般不用管那么多,照着Spring+hibernate+Struts之类的Sample就做了,但慢慢的,也许就要开始在jsp+javabean体系,土制框架,singleton类等环境下使用Spring了。
《Professional Java Development with the Spring Framework》第3章有"Managing the Containe"一节讲这个问题。一般可以分为直接召唤系与IoC fashion两类。
1.直接召唤系--Singleton的Application Context
最简单的,就像在UnitTest里那样,直接构造Application Context:
ApplicationContext ctx = new ClasspathXmlApplicationContext("ApplicationContext.xml");
在Web环境里,会使用ContextLoader构造ApplicationContext后,压进Servlet Context。
由ContextLoaderListener或ContextLoaderServlet,在Web应用启动时完成。
然后在Jsp/Servelet中,可以通过Servlet Context取得ApplicationContext:
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);
但像singleton类或者EJB中,就没有Servlet Context可用了。
如果全部像UnitTest那样直接构造,速度就会很不堪。自然的,就想到把ApplicationContext做成单例。
Spring提供了ContextSingletonBeanFactoryLocator这样的物体。
先搞一个beanRefFactory.xml,里面写上所有的applcationContext-*.xml文件名,并把Context命名为"default-context":
<beans> <bean id="default-context" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg> <list> <value>applicationContext.xml</value></list> </constructor-arg> </bean> </beans>
然后让loactor去找它,但代码有点长:
BeanFactoryReference bfr = DefaultLocatorFactory.getInstance().useBeanFactory("default-context"); BeanFactory factory = bfr.getFactory(); MyService myService = factory.getBean("myService"); bfr.release(); // now use myService
上面的代码实在是太灵活,太麻烦了。
还不如自己实现一个简单的Singleton,扩展ContextLoaderListener类,在Web系统启动时压入Singleton。
新的ContextLoaderListener类重载如下,ContextUtil中包含一个静态的ApplicationContext变量:
public void contextInitialized(ServletContextEvent event) { super.contextInitialized(event); ServletContext context = event.getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context); ContextUtil.setContext(ctx); }
用家可直接取用:
ApplicationContext context = ContextUtil.getContext();
2.IoC fashion
如果所有地方都使用直接召唤系,那就反而是在打Rod的耳光了。因为他一直都反对代码与框架深耦合的。
所以,更好的方法是写一些glue code、base class来完成Spring的降临,而不让应用代码察觉Spring Application Context的存在。
不过,因为各个框架的结构不同,Rod也没办法讲出一个通用的整合方法,所以建议大家尽量学习已整合的各种框架,如Spring MVC、Struts的种种方式,写出自己的简单整合代码来。
只有不确定的调用某些Singleton类,不适合过早ioc的情况,可以使用直接召唤系。
发表评论
-
使用EhCache和Spring AOP实现计算结果缓存
2012-08-07 10:47 1035原文:http://kim-miao.iteye.com/bl ... -
wsdl axis2 spring
2012-08-02 17:06 880http://renxiangzyq.iteye.com/ ... -
spring 监听器 IntrospectorCleanupListener简介
2012-08-02 17:00 1033"在服务器运行过程中,Spring不停的运行的计划任 ... -
Quartz的cron时间表达式
2012-04-11 17:33 1280一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素 ... -
Spring 框架的设计理念与设计模式分析
2012-03-06 13:49 965Spring 框架的设计理念与设计模式分析 http://ww ... -
Spring事务配置的五种方式
2012-03-04 10:40 1368前段时间对Spring的事务配置做了比较深入的研究,在此之间对 ... -
Spring技术内幕:深入解析Spring架构与设计原理(转)
2011-07-01 16:58 1139http://jiwenke.iteye.com/blo ... -
AOP 的利器:ASM 3.0 介绍
2011-06-29 13:48 1352AOP 的利器:ASM 3.0 介绍 http://www ... -
Spring LDAP 1.3.1 发布
2010-12-05 21:17 1170http://www.iteye.com/news/18834 ... -
Spring MVC 3.x annotated controller的几点心得体会
2010-12-01 09:53 2447最近拿Spring MVC 3.x做项目,用了最新的系列相关A ... -
Spring开始关注移动应用开发,发布Spring Mobile,Spring Android
2010-11-29 11:01 3345近日,Roy Clarkson发布了S ... -
SSH全注解-annotation详细配置
2010-11-21 18:50 2450如果有点SSH框架的经验,这个很好理解.配置不难. 配置an ... -
spring安全框架应用
2010-10-11 22:09 3225第一步 将spring-security-core-2.0. ... -
spring3.0.4 新增加的注解(mvc:resources)
2010-08-23 22:19 8916从spring3.0.3发布以后一直等待spring3.0.4 ... -
spring2.0与spring2.5的差别
2010-08-22 00:14 1621资料:http://ajava.org/online/spri ... -
利用Spring框架封装的JavaMail现实同步或异步邮件发送
2010-08-21 23:25 5531利用Spring框架封装的JavaMail现实同步或异步邮件发 ... -
领略Spring 3.x 时代的Spring MVC
2010-08-21 23:20 1404鼎鼎大名的Spring框架3.0版在12月5日由其作者之一—— ... -
使用 Spring 2.5 基于注解驱动的 Spring MVC
2010-08-21 23:16 1002概述 继 Spring 2.0 对 Spring MVC ... -
Spring2.0压缩包目录说明
2010-08-21 22:44 5311、 Spring2.0压缩包目录说明 aspectj目录 ... -
Spring的起源和背景 以及你为什么要使用Spring
2010-08-17 09:04 1500Spring的起源和背景 Rod J ...
相关推荐
maven web环境搭建(spring+springmvc+mybatis),非常详细的图文教程
NULL 博文链接:https://zhkac.iteye.com/blog/494493
Spring源代码解析(二):IoC容器在Web容器中的启动.doc
(1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...
Spring与Web环境集成.pdf
11.7.在ASP.NET中的使用技巧 11.7.1.显示验证错误 11.7.1.1.配置错误显示类 第十二章. 使用Spring.NET进行面向方面的编程 12.1.简介 12.1.1.AOP基本概念 12.1.2.Spring.NET AOP的功能 12.1.3.Spring.NET的AOP代理 ...
在利用spring框架做web开发之前(引入spring MVC之前),仅创建java工程即可。 本文档仅引入基本的spring jar包,用于学习IOC基础。后续学习AOP,spring结合JDBC或mybatis时,以及spring MVC时,再介绍其它所需的jar...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
该文档结合sso单点登录的cas,详细讲述了Spring Web Flow的原来及运行过程,文档详细描述了Spring Web Flow的作用及如何搭建Spring Web Flow环境,结合项目,讲述的非常清晰。
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...
个人备份; 第 1-3 课 Spring Boot 依赖环境和项目结构介绍 第 1-4 课:写一个 Hello World 来感受 Spring Boot/hello ...第 2-9 课:Spring Boot 中使用 Swagger2 构建 RESTful APIs/spring-boot-sw
spring-webmvc-struts.jar整合spring与struts时要用到的jar包 特别是在eclipse开发环境下,搭建好tomcat project工程后,最新版的spring.jar不能完全满足所有的需求的,
Spring的简单介绍及反转控制和依赖注入的代码演示
9章 在Struts中应用Ajax技术 10章 Struts之项目实战 3篇 持久层框架Hibernate技术 11章 Hibernate快速上手 12章 精解Hibernate之核心文件 13章 探究Hibernate之核心接口 14章 Hibernate之项目实战 4篇 业务层...
JAVA WEB spring 的 环 境 配 置
Spring 2.5 jar 所有开发包及完整文档及项目开发实例 spring jar 包详解spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock....这使得在OSGi环境中使用Spring 更加方便,不再需要定制打包了。
在没有使用 Spring Boot 之前我们开发一个 web 项目需要做哪些工作: 1)配置 web.xml,加载 Spring 和 Spring mvc 2)配置数据库连接、配置 Spring 事务 3)配置加载配置文件的读取,开启注解 4)配置日志文件 ...
Spring如何在Web环境中集成IoC容器并为Web应用开发提供利器? 我们耳熟能详的MVC模式在Spring中是如何实现的? Spring MVC如何灵活地集成各种丰富的视图展现方案? Spring实现远端调用的方案有很多种,你...
spring MVC环境搭建 所需jar包 ├── commons-logging-1.1.1.jar ├── jstl.jar ├── spring-aop-4.3.0.RELEASE.jar ├── spring-aspects-4.3.0.RELEASE.jar ├── spring-beans-4.3.0.RELEASE.jar ├...
如何整合这4个开源框架进行J2EE开发,如何基于MyEclipseIDE进行开发,J2EE开发的基础和基础环境搭配,Struts、Hibernate、Spring、Eclipse四个框架的使用,如何整合Struts+Hibernate+Spring+Eclipse框架。...