如何将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 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...
随机推荐
- JMeter学习(二十四)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults(转载)
转载自 http://www.cnblogs.com/yangxia-test Test Plan的配置元件中有一些和HTTP属性相关的元件:HTTP Cache Manager.HTTP Autho ...
- day17 正则表达式 re模块和hashlib模块
今日内容 1. re&正则表达式(*****) 注:不要将自定义文件命名为re import re re.findall(正则表达式,被匹配的字符串) 拿着正则表达式去字符串中找,返回一个列表 ...
- Spring的一些资源
1.http://spring.io/ 2.http://projects.spring.io/spring-framework/
- 【scrapy】其他问题2
今天爬取豆瓣电影的是时候,出现了两个问题: 1.数据无法爬取并输出Retrying <GET https://movie.douban.com/robots.txt> 看起来像是被拦截了. ...
- Idea设置类注释模板
1.选择File–>Settings–>Editor–>File and Code Templates–>Includes–>File Header. 在Descript ...
- [1.16更新B14特征处理]津南数字制造题目解读及部分思路~~有趣的特征
[1.16更新B14特征处理]津南数字制造题目解读及部分思路--有趣的特征 Article onion啦啦啦 2019-01-17 16:03:38 11 1790 11 首先声明,我并不能保证这些特 ...
- Python+Selenium学习--访问连接
场景 web UI测试里最简单也是最基本的事情就是访问1个链接了. 在python的webdrive中,访问url时应该使用get方法. 代码 #!/usr/bin/env python # -*- ...
- expect命令自动登录ssh
expect是简单的工具原因,依赖于tcl. 直接apt安装就行. 四个关键字: spawn,派生出新进程. expect,期待得到的字符串,可以模式匹配. send,向进程发送字符串. intera ...
- python 中 类型转换 bytes
https://www.cnblogs.com/sesshoumaru/p/5980090.html
- php ActiveMQ的安装与使用
一.ActiveMQ是什么?ActiveMQ是Apache出品,最流行的,能力强劲的开源消息总线.支持多种语言客户端(Java,C,C++,C#,Python,Ruby,Perl,PHP), 支持多种 ...