如何将service绑入到spring 并且在action中使用
第一步:定制 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中使用的更多相关文章
- spring security在spring mvc的action中获取登录人信息
@RequestMapping("/index") public ModelAndView login( @RequestParam(value = "error&quo ...
- spring在IoC容器中装配Bean详解
1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...
- Spring Batch在大型企业中的最佳实践
在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...
- 关于 spring MVC 配置自动扫描中 use-default-filters 属性
1.springMVC 设置扫描 Bean 的两种常见写法 1.1.先看第一种常见的配置:默认 <!-- 配置Controller扫描 --> <context:component- ...
- service注入到action中
service注入到action中 之前本人每次要获得service都是在action自己通过WebApplicationContext的getBean获得的,一直在spring中只配置到了servi ...
- 创建swagger的springboot-stater,并在spring cloud zuul网关中引入
Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 通过在controller中添加注解,即可轻易实现代码文档化. Swagger提供ui界 ...
- Spring 获取propertise文件中的值
Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...
- Idea中Spring整合MyBatis框架中配置文件中对象注入问题解决方案
运行环境:Spring框架整合MaBitis框架 问题叙述: 在Spring配置文件applicationContext-mybatis.xml中配置好mybatis之后 <?xml versi ...
- 重新学习Spring一--Spring在web项目中的启动过程
1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...
随机推荐
- Mysql 表约束 非空、唯一、主键、自增长、默认、外键约束(基础6)
非空(not null).唯一(unique key).主键(primary key).自增长(auto_increment).默认约束(default) 准备基础环境: mysql> crea ...
- java定义object数组(可以存储String或int等多种类型)
需求| 想在数组中既有String类型又有int等类型,所以需要定义数组为Object类型 背景| 现在有一个字符串params,需要对其进行逗号分隔赋值到数组里,这时遇到了个问题,即使直接定义的 ...
- git ssh免登陆,以及ssh config
git去连接github或gitlab上的远程仓库,可以使用ssh方式,也可以使用git的账号密码登录 这里介绍使用ssh方式实现免登陆(第一步和第二步即可实现) 第一步:生成ssh秘钥 ssh- ...
- SQLdeveloper同时显示多个表的窗口
- 关于cdh 5.X 的agent 客户端镜像安装注意事项
当把客户端镜像安装时,每个客户端程序会生成UUID作为唯一标识,重新 安装时要删除 rm -rf /var/lib/cloudera-scm-agent 如果不删除会造成主机列表中IP一直在变的情况.
- Python之逻辑回归模型来预测
建立一个逻辑回归模型来预测一个学生是否被录取. import numpy as np import pandas as pd import matplotlib.pyplot as plt impor ...
- H3C S5800 MPLS----VPLS 三层路由透传二层网络
一.MPLS 介绍 多协议标签交换(Multi-Protocol Label Switching,MPLS)是新一代的IP高速骨干网络交换标准,由因特网工程任务组(Internet Engineeri ...
- Python os.sep()
os.sep是什么 python是跨平台的.在Windows上,文件的路径分隔符是'\',在Linux上是'/'. 为了让代码在不同的平台上都能运行,那么路径应该写'\'还是'/'呢? 使用os.se ...
- NumPy 数学函数
NumPy 数学函数 NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. 三角函数 NumPy 提供了标准的三角函数:sin().cos().tan(). 实例 ...
- 1、str.join() 2、fromkeys() 3、深浅拷贝 4、set()
1. 补充基础数据类型的相关知识点 1. str. join() 把列表变成字符串 2. 列表不能再循环的时候删除. 因为索引会跟着改变 3. 字典也不能直接循环删除. 把要删除的内容记录在列表中. ...