java之spring之整合ssh-2
这篇也是主要讲解 ssh 的整合,不同于上一篇的是它的注入方式。
这篇会采用扫描注入的方式,即去除 applicationContext-asd.xml 文件。
目录结构如下:

注意,这里只列举不同的文件
1. 首先,我们看下 applicationContext-dao.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- sessionFactory对象由spring来创建 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 通用属性配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 配置映射文件 -->
<property name="annotatedClasses">
<array>
<value>cn.vincent.vo.User</value>
<value>cn.vincent.vo.Role</value>
</array>
</property>
</bean>
<!-- 配置声明式事务 -->
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 表示以save开头的方法都需要事务
propagation 表示事务的传播特性
REQUIRED 查看当前是否有事务,如果有,使用当前事务,如果没有开启新事务
-->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务aop -->
<aop:config>
<!--expression 指明事务在哪里起作用
第一个* 表示所有返回值
第二个* 表示所有类
第三个* 表示类中的所有方法
.. 表示所有参数
-->
<aop:pointcut expression="execution(* cn.vincent.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 自动扫描 指定包下及其子包下的所有类中注解,并根据注解完成指定工作 -->
<context:component-scan base-package="cn.vincent"></context:component-scan>
</beans>
2.UserDaoImpl 文件
package cn.vincent.dao.impl; import java.util.List; import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import cn.vincent.dao.UserDao;
import cn.vincent.vo.User; //相当于 <bean id="userDao" class="cn.sxt.dao.impl.UserDaoImpl">
@Repository("userDao")
public class UserDaoImpl implements UserDao{
//自动注入 在容器中查询 是否存在对应的bean
@Autowired
private SessionFactory sessionFactory;
@Override
public List<User> findAll() {
return sessionFactory.getCurrentSession().createQuery("from User").list();
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} }
3. UserServiceImpl 文件
package cn.vincent.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import cn.vincent.dao.UserDao;
import cn.vincent.service.UserService;
import cn.vincent.vo.User; @Service("userService")
public class UserServiceImpl implements UserService{ @Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
} public void setUserDao(UserDao userDao){
this.userDao=userDao;
}
}
4. UserAction.java 文件
package cn.vincent.action; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.Action; import cn.vincent.service.UserService;
import cn.vincent.vo.User; @Controller
@Scope("prototype")
public class UserAction { private List<User> list;
@Autowired
private UserService userService; public String list(){
list=userService.findAll();
return Action.SUCCESS;
} public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
} }
以上,就是采用注解的方式,来自动扫描,实现注入的目的。其他与上一篇相同
github地址:
https://github.com/Vincent-yuan/spring_ssh2
java之spring之整合ssh-2的更多相关文章
- java之spring之整合ssh
这篇主要讲解spring + struts2 + hibernate : 目录结构如下: t_role t_user 1.新建 web项目 :spring_ssh 2.在 WebRoot/WEB-IN ...
- JAVA springmvc+spring+mybatis整合
一.springmvc---controller spring----service mybatiss---dao pring(包括springmvc).mybatis.mybatis-sprin ...
- JAVA框架 Spring junit整合单元测试
一.准备工作 1:Junit的需要的jar包: 2.spring的整合的jar包:spring-test-4.2.4.RELEASE.jar 3.代码实现 1) //导入整合的类,帮我们加载对应的配置 ...
- 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】
一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...
- .net到Java那些事儿--整合SSH
一.介绍 整体介绍分成两个部分,第一.net转到Java的原因,第二开发SSH时候的环境介绍: .net到Java的原因: .net开发也将近快3年的样子,加上现在的老东家换过 ...
- Spring+Hibernate+Struts(SSH)框架整合
SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...
- Spring框架的第四天(整合ssh框架)
## Spring框架的第四天 ## ---------- **课程回顾:Spring框架第三天** 1. AOP注解方式 * 编写切面类(包含通知和切入点) * 开启自动代理 2. JDBC模板技术 ...
- Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例
Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...
- 【spring boot】【elasticsearch】spring boot整合elasticsearch,启动报错Caused by: java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8
spring boot整合elasticsearch, 启动报错: Caused by: java.lang.IllegalStateException: availableProcessors ], ...
随机推荐
- 冰多多团队-第二次scrum例会
冰多多团队-第二次Scrum会议 会议基本情况 会议时间:4月8日 19:00 - 19:30 会议地点:新主楼F座2楼沙发休息处 工作情况 团队成员 已完成任务 待完成任务 zpj Service实 ...
- 颜色空间模型(HSV\LAB\RGB\CMYK)
通过Photoshop的拾色器,我们知道表征颜色的模型的不止一种,本文将系统并且详细讨论这四种模型(HSV.LAB.RGB和CMYK)之间的联系以及应用.本文部分章节整合了多位优秀博主的博客(链接见本 ...
- Nexus Repository Manager OSS 3.x 安装配置
前言想要使用maven搭建项目,但是国内的网络环境可以想象,还有公司自己开发的jar包等问题,所以需要搭建一个maven的私服,这样便于管理. 找了一些教程,顺便记下来,当做笔记. 本文以Window ...
- vue-router踩坑日记Unknown custom element router-view
今天笔者在研究vue-router的时候踩到了一个小坑,这个坑是这样的 笔者的具体代码如下:router.js import Home from '@/components/Home.vue'; im ...
- order by 多个条件
ORDER子句按一个或多个(最多16个)字段排序查询结果,可以是升序(ASC)也可以是降序(DESC),缺省是升序.ORDER子句通常放在SQL语句的最后.ORDER子句中定义了多个字段,则按照字段的 ...
- Mockito 的用法
本文为博主原创,转载请注明出处: Mockito 是一个基于MIT协议的开源java测试框架. Mockito区别于其他模拟框架的地方主要是允许开发者在没有建立“预期”时验证被测系统的行为.对于moc ...
- LwIP应用开发笔记之六:LwIP无操作系统TCP客户端
上一篇我们基于LwIP协议栈的RAW API实现了一个TCP服务器的简单应用,接下来一节我们来实现一个TCP客户端的简单应用. 1.TCP简述 TCP(Transmission Control Pro ...
- phpspreadsheet 中文文档(六)读写文件+读取文件
2019年10月11日14:05:58 读写文件 从体系结构您已经知道,使用基本PhpSpreadsheet类无法对持久性存储进行读写.为此,PhpSpreadsheet提供读者和作家,这是实现\Ph ...
- Apache显示目录列表及icons目录的问题
今天想部署下开源项目pig,发现它的mysql需要5.7.8 +,为了能支持多个版本并且可以方便切换,所以选择了phpstudy_pro 刚开始Apache不支持目录访问 修改配置 <Virtu ...
- 超详尽-QThread的正确使用姿势-以及信号槽的跨线程使用
贴上两篇博文 一.http://www.cnblogs.com/findumars/p/5031239.html 循序渐进介绍了,怎样正确的让槽函数工作在子线程中. 同时介绍了信号槽的绑定与线程的关系 ...