转自
http://blog.csdn.net/csuliky/archive/2009/07/03/4320007.aspx
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/csdn/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_r_r中的execution表达式比较复杂:
1.第一个星号表示匹配所有的返回值;
2.abu.csdn.service表示包;
3.第二个星号表示匹配这个包的所有类;
4.第三个星号表示匹配类的所有方法;
5.括号内表示方法的参数;
6.两个点表示匹配所有的参数
-->
<aop:pointcut id="allServiceMethods"
expression_r_r="execution(* abu.csdn.service.*.*(..))" />
<!-- 指定事务和切入点 -->
<aop:advisor pointcut-ref="allServiceMethods"
advice-ref="txAdvice" />
</aop:config>
</beans>
2.Servlet
package abu.csdn.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;
public class CopyOfShowImageServlet extends HttpServlet {
HibernateTemplate hibernateTemplate;
@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.
分享到:
相关推荐
这是一个关于mvc思想的框架,是一个用java实现servlet的一个mvc框架,有这方面需要的朋友,可以下载下来研究一下,可以自由评论
这是一个用servlet模拟struts_mvc框架的一个案例,通过案例可以让我们更好的懂得MVC框架的运行原理(面试经常考)。
新手搭建myeclpse基本Servlet框架源码,myeclipse搭建的Servlet框架源码。
使用spring的依赖注入,来实现servlet中注入dao层
使用servlet3实现的简易mvc框架,使用反射,自定义注解
jsp+servlet+mysql集成框架,内含数据库建表语句,下载之后直接可以用需要的小伙伴赶紧下载了
公司要求的,只用servlet和jsp,不能用任何后台框架,不能用异步、不能用连接池,其它技术更不用说了。花了2天时间写了一个框架出来。附带写了2个功能,算是例子。界面样式自己写的。前端用了jquery、jquery分页插件...
JavaWeb图书管理系统servlet+JSP+javabean 无框架适合新手学习的web项目
java开发基于Servlet框架的管理系统项目源代码。 实现登录 、 注册 、 增 、 删 、 改 、 查 ; 可继续完善增加前端、校验、其他功能等; 可作为 Servlet项目 开发练习基础模型; 课程设计、 毕业设计 开发基础; ...
Ext4+JSON+Servlet/Struts2+Ext.tree.Panel+Ext.tab.Panel构建Web应用系统框架
jsp+servlet+mysql项目框架
基于javaweb的商品后台管理系统servlet+jdbc+jsp框架源码
ACCP6.0 S2 使用JSP/Servlet/Ajax技术开发新闻发布系统答案
用servlet框架编写的,主要是用于学习servlet,以及如何配置xml文档。。
11 Servlet:servlet(代码题目) 10分 12 Servlet:jstl标签(代码题目) 8分 13 Servlet:welcom页面 3分 14 Servlet:session,accplication 6分 15 Servlet:get,post 4分 16 Servlet:http协议消息的分类 4分 ...
springboot内部绑定了嵌入式http servlet容器Jetty,主体框架基于spring,而spring除了Ioc(依赖注入)、Aop(切面)外,其它与http相关的东西都是基于Servlet和Filter的,而Servlet、Filter是servlet容器Jetty按照...
这个没有数据库文件,请查看另一个下载的地址。谢谢!!!!! https://download.csdn.net/download/qq_21181253/10667309
该项目未使用任何框架,使用最初的web编程技术写的demo
Spring的注入在Servlet中使用:在Servlet中使用Spring注入的信息,需要WebApplicationContext这个专门为Web准备的应用上下文
主要介绍了基于jsp+servlet实现的简单博客系统,以完整实例形式分析了基于jsp+servlet简单博客系统的原理与相关技巧,并附带了完整源码供读者下载学习,需要的朋友可以参考下