Struts2、Hibernate、Spring整合时怎么向Servlet注入属性
1.Spring数据库的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<!-- Hibernate的会话 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
scope="singleton">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 设置查询结果集可以回滚,在分页时必须这样 -->
<prop key="jdbc.use_scrollable_resultset">true</prop>
<prop key="hbm2ddl">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>abu/****/bean/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- Spring的Hibernate工具类HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" scope="singleton">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" scope="singleton">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=csu">
</property>
<property name="username" value="liky"/>
<property name="password" value="redhat"/>
</bean>
<!-- Spring的声明式事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务的传播特性,通常应该将事务设置在业务层,而非dao上 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- name为事务对哪些方法生效,可以使用通配符; propagation为事务的传播特性,一共有6种-->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切入点,就是哪些包的哪些类的哪些方法参与事务 -->
<aop:config>
<!-- id是一个标识,expression中的execution表达式比较复杂:
1.第一个星号表示匹配所有的返回值;2.abu.****.service表示包;3.第二个星号表示匹配这个包的所有类
4.第三个星号表示匹配类的所有方法;5.括号内表示方法的参数;6.两个点表示匹配所有的参数
-->
<aop:pointcut id="allServiceMethods"
expression="execution(* abu.****.service.*.*(..))" />
<!-- 指定事务和切入点 -->
<aop:advisor pointcut-ref="allServiceMethods"
advice-ref="txAdvice" />
</aop:config>
</beans>
2.Servlet
package abu.****.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; /** * <p> * 演示使用Spring向Servlet注入对象 * </p> * User: Abu Date: 2009-7-2 Time: 14:30:55 */ public class CopyOfShowImageServlet extends HttpServlet { HibernateTemplate hibernateTemplate; /** * <p> * 在Servlet中注入对象的步骤: * 1.取得ServletContext * 2.利用Spring的工具类WebApplicationContextUtils得到WebApplicationContext * 3.WebApplicationContext就是一个BeanFactory,其中就有一个getBean方法 * 4.有了这个方法就可像平常一样为所欲为了,哈哈! * </p> */ @Override public void init() throws ServletException { super.init(); ServletContext servletContext = this.getServletContext(); WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); hibernateTemplate = (HibernateTemplate)ctx.getBean("hibernateTemplate"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }3.总结
你应该看到了,我在Spring中使用了声明式事务,如果直接使用Spring的工厂类在这里是不行的,因为所有的对象都已经有Spring的IoC管理了,所以只能借助WebApplicationContextUtils这个工具类来获得Bean.