第一步:定制 service接口,为什么用接口我也不清楚

package com.inspur.services;

import com.hsp.domain.User;

public interface IaddService {
 public void addUser(User e);
}

第二步,实现service接口

package com.inspur.services;
import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;

import com.hsp.domain.User;

//要是没有这个transactional将会导致如下错误:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
@Transactional
public class AddUser implements IaddService {
//注 这里的sf必须与在applicationContext.xml中的属性sf一致

private SessionFactory  sf;
 public SessionFactory getSf() {
  return sf;
 }
 public void setSf(SessionFactory sf) {
  this.sf = sf;
 }

public void addUser(User e) {
  sf.getCurrentSession().save(e);
 }

}

第三步:在spring容器中部署(applicationContext.xml)

<!-- 配置EmployeeService对象 -->
<bean id="userSevice" class="com.inspur.services.AddUser">
 <property name="sf" ref="sessionFactory" />
</bean>

第四步:在action中使用

package com.inspur.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hsp.domain.User;
import com.inspur.forms.*;
import com.inspur.services.AddUser;
import com.inspur.services.IaddService;

public class LoginAction extends DispatchAction {

//注这里的userservice必须与applicationContext.xml中的配置的loginaction的属性userservice属性名字一致
 private IaddService userservice;
  public IaddService getUserservice() {
  return userservice;
 }

public void setUserservice(IaddService userservice) {
  this.userservice = userservice;
 }

public ActionForward add(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
   System.out.println("******通过新的方式响应请求***********");
   /*
   
   EmployeeForm employeeForm=(EmployeeForm)form;
   //构建一个Employee对象
   Employee e=new Employee();
   e.setId(employeeForm.getId());
   e.setName(employeeForm.getName());
   e.setLeader(employeeForm.getLeader());
   e.setMid("111111");
   eif.addEmployee(e);
   request.getSession().setAttribute("adder", e);
   */
   User u=new User();
   u.setId(1);
   u.setName("wj");
   u.setPassword("123");
   userservice.addUser(u);
   return mapping.findForward("add");
  }

//响应登录
  public ActionForward login(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
   System.out.println("******通过新的方式响应请求***********");
   /*
   
   EmployeeForm employeeForm=(EmployeeForm)form;
   //构建一个Employee对象
   Employee e=new Employee();
   e.setId(employeeForm.getId());
   e.setName(employeeForm.getName());
   e.setLeader(employeeForm.getLeader());
   e.setMid("111111");
   eif.addEmployee(e);
   request.getSession().setAttribute("adder", e);
   */
   return mapping.findForward("ok");
  }
  
   
}

郑重补充:

  要想使用重量级工具最好用单例模式,可以通过spring的依赖注入实现这个问题,但是本例中LoginACtion以及AddUser使用了依赖注入后的对象sessionfactory,userservice,因此在类中必须做到变量名字与applicationContext.xml的一致,且用上一级的接口或者类来接受依赖注入后的对象。

  一般都会需要在applicationContext.xml中配置代理事物管理:

<!-- 启用注解扫描 -->
<context:annotation-config/>
<!-- 配置事务管理器,统一管理sessionFactory的事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>

然后在AddUser中添加@Transactional,由于使用了注解扫描,所以web容器会识别出AddUser使用了事物管理器以及事物注解。

如何将service绑入到spring 并且在action中使用的更多相关文章

  1. spring security在spring mvc的action中获取登录人信息

    @RequestMapping("/index") public ModelAndView login( @RequestParam(value = "error&quo ...

  2. spring在IoC容器中装配Bean详解

    1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...

  3. Spring Batch在大型企业中的最佳实践

    在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...

  4. 关于 spring MVC 配置自动扫描中 use-default-filters 属性

    1.springMVC 设置扫描 Bean 的两种常见写法 1.1.先看第一种常见的配置:默认 <!-- 配置Controller扫描 --> <context:component- ...

  5. service注入到action中

    service注入到action中 之前本人每次要获得service都是在action自己通过WebApplicationContext的getBean获得的,一直在spring中只配置到了servi ...

  6. 创建swagger的springboot-stater,并在spring cloud zuul网关中引入

    Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 通过在controller中添加注解,即可轻易实现代码文档化. Swagger提供ui界 ...

  7. Spring 获取propertise文件中的值

    Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...

  8. Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案

    运行环境:Spring框架整合MaBitis框架 问题叙述: 在Spring配置文件applicationContext-mybatis.xml中配置好mybatis之后 <?xml versi ...

  9. 重新学习Spring一--Spring在web项目中的启动过程

    1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...

随机推荐

  1. e-olymp Problem8352 Taxi

    作为我在这个OJ玩了一下午的终结吧. 水题一道,阅读理解OJ. 传送门:点我 Taxi At the peak hour, three taxi buses drove up at the same ...

  2. unity3D OnTriggerEnter和OnCollisionEnter的区别

    1,测试OnTriggerEnter和OnCollisionEnter的区别 测试:如果两个物体A,B 两者都有碰撞体collider(Box Collider,Sphere Collider,Cap ...

  3. TZOJ 1321 Girls and Boys(匈牙利最大独立集)

    描述 the second year of the university somebody started a study on the romantic relations between the ...

  4. ajax中的contendType和dataType知识点梳理

    在ajax中有2个参数比较重要,之前一直没有搞清楚,下面我们开始梳理一下 1.contentType字段:这个字段的意思,ajax发送给后端的数据是什么类型 如果在ajax中不指定这个属性,则默认是u ...

  5. 数据库(mysql)

    一.left join  right join  inner join left join(左连接),在两张表进行连接查询时,会返回左表所有的行,即使在右表中没有匹配的记录. right join(右 ...

  6. day 03

    1.数字类型 int 数字主要是用于计算用的,使用方法并不是很多,就记住一种就可以: bit_length()  当前十进制用二进制表示时,最少使用的位数 s = 5 print(s.bit_leng ...

  7. 设置Tomcat的JAVA_OPTS参数

    修改$TOMCAT_HOME/bin/catalina.bat 添加set JAVA_OPTS= ... rem ----- Execute The Requested Command ------- ...

  8. http协议的学习

    TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.

  9. node.js中npm包管理工具

    现在安装node.js,默认就会帮我们装上了npm包管理工具,npm主要用来下载,安装,管理第三方模块. 创建一个包描述文件: npm init [-y] 查看包的信息 npm info <pa ...

  10. sqlserver中对于特定数据字段定义特定的数据类型

    char和varchar:汉字占两个字节,英文.数字或字符占一个 比如: 性别:男   女 可以定义为:char(2)或者是varchar(2)    因为性别是中文,中文占两个字节 nchar和nv ...