一、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