这篇也是主要讲解 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的更多相关文章

  1. java之spring之整合ssh

    这篇主要讲解spring + struts2 + hibernate : 目录结构如下: t_role t_user 1.新建 web项目 :spring_ssh 2.在 WebRoot/WEB-IN ...

  2. JAVA springmvc+spring+mybatis整合

    一.springmvc---controller  spring----service  mybatiss---dao pring(包括springmvc).mybatis.mybatis-sprin ...

  3. JAVA框架 Spring junit整合单元测试

    一.准备工作 1:Junit的需要的jar包: 2.spring的整合的jar包:spring-test-4.2.4.RELEASE.jar 3.代码实现 1) //导入整合的类,帮我们加载对应的配置 ...

  4. 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】

    一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...

  5. .net到Java那些事儿--整合SSH

    一.介绍       整体介绍分成两个部分,第一.net转到Java的原因,第二开发SSH时候的环境介绍:       .net到Java的原因: .net开发也将近快3年的样子,加上现在的老东家换过 ...

  6. Spring+Hibernate+Struts(SSH)框架整合

    SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...

  7. Spring框架的第四天(整合ssh框架)

    ## Spring框架的第四天 ## ---------- **课程回顾:Spring框架第三天** 1. AOP注解方式 * 编写切面类(包含通知和切入点) * 开启自动代理 2. JDBC模板技术 ...

  8. Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例

    Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...

  9. 【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 ], ...

随机推荐

  1. ServerManager.exe 0xc0000135 应用程序错误

    问题: 将 Windows Server 2016 .NET Framework移除. IIS卸载后, Server Manager.exe.事件查看器等都无法正常开启. 解决方案: 在运行中,输入C ...

  2. 二分法递归版本(c++)

    利用二分法求解在区间[0,π/2]上的根 #include<iostream> #include <cmath> using namespace std; double dic ...

  3. tomcat 启动中文乱码

        1.情景展示 从Apache官网下载的tomcat,启动后中文日志信息显示乱码. 启动startup.bat后 2.原因分析 通过修改日志输出的字符集来解决. 3.解决方案 tomcat安装目 ...

  4. 【技术博客】JWT的认证机制Django项目中应用

    开发组在开发过程中,都不可避免地遇到了一些困难或问题,但都最终想出办法克服了.我们认为这样的经验是有必要记录下来的,因此就有了[技术博客]. JWT的认证机制Django项目中应用 这篇技术博客基于软 ...

  5. nginx 日志打印响应时间 request_time 和 upstream_response_time

    设置log_format,添加request_time,$upstream_response_time,位置随意 og_format  main  '"$request_time" ...

  6. Linux文件系统被占用,磁盘使用量与实际不一致【转】

    用lsof检查后才发现原因是,有文件被删除,而进程还活着,因而造成还占用空间的现象 [root@/]# lsof |grep delete 根据lsof列出的进程号,kill这些进程后,空间就释放出来 ...

  7. h5 唤起app或跳转appStore

    //唤起app通过唤端媒介(URL Scheme)   //微信浏览器自6.3.x版本起禁用了大多数Scheme跳转功能,扫一扫目前可用   // URL 的组成:   // [scheme:][// ...

  8. cp复制

    将Data文件复制到B目录下: cp -r /home/hp/Data /home/hp/B/  

  9. org.apache.hadoop.fs.FsUrlStreamHandlerFactory 在哪个jar包

    org.apache.hadoop.fs.FsUrlStreamHandlerFactory在org.apache.hadoop类中,org.apache.hadoop在hadoop安装目录下.

  10. beetl模版for循环渲染字符串

    beetl for循环渲染html字符串的方式, beetl if条件判断输出, beet自定义标签和标签引用, beetl html赋值, beetl渲染json,beetl注释.变量定义, 更多文 ...