★★★Spring常用,不断更新...
来源:互联网 发布:空间日志软件 编辑:程序博客网 时间:2024/06/05 14:28
一、Spring简介
Spring 是一个开源框架。
Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的JavaBean 实现以前只有 EJB 才能实现的功能。
Spring 是一个 IOC(DI) 和 AOP 容器框架。
二、配置bean
1、 eclipse上安装SPRING - TOOL- SUITE
SPRING TOOL SUITE 是一个 Eclipse 插件,利用该插件可以更方便的在 Eclipse 平台上开发基于 Spring 的应用。
安装方法说明(springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip):
a) Help --> Install New Software...
b) Click Add...
c) In dialog Add Site dialog,click Archive...
d) Navigate to springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip and click Open
e) Clicking OK in the AddSite dialog will bring you back to the dialog 'Install'
f) Select the xxx/Spring IDEthat has appeared
g) Click Next and then Finish
h) Approve the license
i) Restart eclipse when that isasked
安装选取的时候,只选择带有“Spring IDE”的就够了。
2、 Spring环境配置
第一步,导入架包。
Spring需要的架包如下:
第二步,配置文件
配置文件约定俗称的名字叫:applicationContext.xml,内容可以从Spring的实例中拷贝出,也可以用以上的SPRING TOOL SUITE这个工具建立。
3、Spring如何应用在web应用中
步骤:
第一步、加入相应的web架包
spring-web-4.1.4.RELEASE.jar
spring-webmvc-4.1.4.RELEASE.jar
第二步、创建IOC容器
我们知道,非web项目创建IOC容器(一般用ApplicationContext)是在main方法中创建,但是web项目创建IOC容器一般是在web服务器加载整个web项目的时候创建,这就需要用上ServletContextListener这个监听器。这个监听器会在web服务器一加载web项目的时候启动,contextInitialized()方法是这个监听器的主要方法。
在ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器。在创建好IOC容器后,可以把其放在ServletContext(即application域)的一个属性中。
第三步、配置Spring 配置文件的名字和位置,以提高应用的可扩展性。一般将其配置到当前 WEB 应用的初始化参数中。
【实例】
监听器代码
public classSpringServletContextListener implements ServletContextListener { /** * Default constructor. */ publicSpringServletContextListener() { // TODO Auto-generated constructor stub } /** * @seeServletContextListener#contextInitialized(ServletContextEvent) */ publicvoidcontextInitialized(ServletContextEvent arg0) { //1. 获取 Spring 配置文件的名称. ServletContext servletContext =arg0.getServletContext(); String config =servletContext.getInitParameter("configLocation"); //1. 创建 IOC 容器 ApplicationContext ctx = newClassPathXmlApplicationContext(config); //2. 把 IOC 容器放在 ServletContext 的一个属性中. servletContext.setAttribute("ApplicationContext",ctx); } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ publicvoidcontextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub }}
web.xml代码
<context-param> <param-name>configLocation</param-name> <param-value>applicationContext.xml</param-value></context-param><!-- 启动IOC容器的ServletContextListner方法 --><listener> <listener-class>com.atguigu.spring.struts2.listeners.SpringServletContextListener</listener-class></listener>
对于上述过程,其实spring已经为我们准备好了一个自带的监听器ContextLoaderListener,在web.xml中可以直接创建这个监听器的配置,不用自己再重复建立一个监听器:
步骤如下:
第一步、在web.xml文件中配置ContextLoaderListener监听器。
<!-- needed forContextLoaderListener --><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext.xml</param-value></context-param> <!-- Bootstraps the root webapplication context before servlet initialization --><listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class></listener>
第二步、在web应用中取出IOC容器。
// 从application域中得到IOC容器ApplicationContext ctx =WebApplicationContextUtils.getWebApplicationCotext(application);
4、Spring如何集成Struts2
一般来说,struts的action都是由struts2自己创建的,整合spring和struts的目的就是将action交给spring管理,在访问action的时候从spring的IOC容器中获取,而不是让struts创建。
步骤:
第一步、加入架包
struts2-spring-plugin-2.3.16.3.jar
第二步:
将相应的action类配置成bean即交给spring管理。这里注意,需要配置scope属性为prototpe,不能采用默认的单例模式。
第三步、
<action name="personAction"class="com.spring.web.PersonAction"> <result>/success.jsp</result></action>
这里class属性就不用写类的全类名了,只需要写配置在IOC容器中的bean的id即可。
5、Spring的声明式事物管理
<!-- 配置事物管理器,事物管理器依赖于数据源-->
<bean id=" transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice id="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<!--根据方法名指定事务的属性 -->
<tx:methodname="purchase" propagation="REQUIRES_NEW"/>
<tx:methodname="get*" read-only="true"/>
<tx:methodname="find*" read-only="true"/>
<tx:methodname="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事物的切入点,以及把事物切入点和事物属性关联起来 -->
<aop:config>
<aop:pointcutexpression="execution(* com.cll.spring.tx.xml.service.*.*(..))"
id="txPointCut"/>
<aop:advisoradvice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
更加详细的事物管理配置:http://blog.csdn.net/it_man/article/details/5074371
- ★★★Spring常用,不断更新...
- ★★★Struts2常用配置项,配置属性备份,不断更新...
- Oracle常用语句(不断更新)
- 常用网站(不断更新中)
- 常用js, 不断更新中...
- 常用ABAP语句,不断更新
- 常用js函数(不断更新)
- oracle 常用SQL(不断更新)
- Spring MVC 积累-不断更新
- [oracle]常用SQL语句(不断更新)
- 软件测试常用单词(不断更新):
- iphone开发常用代码(不断更新)
- ubuntu常用快捷键(不断更新)
- QTP脚本常用代码(不断更新)
- php常用函数留存-不断更新
- 个人常用eclipse快捷键,不断更新
- DBA常用视图汇总【不断更新】
- iphone开发常用代码(不断更新)
- leetCode-Surrounded Regions DFS与BFS解法
- 数学之美之谷歌AK-47的设计者阿米·特辛格博士
- 勤哲Excel服务器-OA系统
- 胡言乱语找节奏 —— 什么才是机器学习的未来
- Linux命令之crontab定时任务执行
- ★★★Spring常用,不断更新...
- 主流IM协议简介
- 用Python和Pygame写游戏-从入门到精通(14)
- 关于ListView
- HTTP代理穿透原理
- 感知机模型
- 阿里一面 阴差阳错的幸运
- 关联容器的操作
- 常用技术网站