这篇也是主要讲解 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. Tecplot显示周期和对称算例

    源视频链接:https://pan.baidu.com/s/1HdU3nsti8qLZhXvISxsSFA 提取码: 3kfu 模型链接:https://pan.baidu.com/s/1CQCGL7 ...

  2. Ubuntu 19.04 国内更新源

    2019年4月18日, Ubuntu 19.04 正式发布. Ubuntu 19.04 的 Codename 是"disco(迪斯科舞厅)": zkf@ubuntu:~$ lsb_ ...

  3. prometheus(docker)安装和报警 -- nginx域名监控

    软件组件:prometheusalertmanagerprometheus-webhook-dingtalk nginx-vts-exporternginx (###--add-module=../n ...

  4. 经典算法(一) top k

    问题:1亿数据中,找出最大的k个数,要求使用内存不超过1m (延伸问题:1亿数据中,找出重复出现次数最多的k个,要求使用内存不超过1m 等) 分析: 1亿数字(int)占内存:100000000 * ...

  5. java 注解,动态代理

    秒懂,Java 注解 (Annotation)你可以这样学 深入理解Java注解类型(@Annotation) 注解可以理解为标签. 当开发者使用了Annotation 修饰了类.方法.Field 等 ...

  6. 【转】Android root检测方法总结

    一 为什么要进行root检测?出于安全原因,我们的应用程序不建议在已经root的设备上运行,所以需要检测是否设备已经root,以提示用户若继续使用会存在风险. 二 root了会有什么风险?在Linux ...

  7. quartus仿真提示: Can't launch the ModelSim-Altera software

    quartus仿真提示: Can't launch the ModelSim-Altera software 2017年07月13日 17:54:50 小怪_tan 阅读数:3255   路径中的结尾 ...

  8. Running MYSQL 5.7 By Bash On Ubuntu On Windows:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

    root@PC-RENGUOQIANG:/usr/sbin# /etc/init.d/mysql start * Starting MySQL database server mysqld [ OK ...

  9. UICachedDeviceRGBColor CGImage]: unrecognized selector sent to instance 0xxxxxxxxxxx'

    UICachedDeviceRGBColor CGImage]: unrecognized selector sent to instance 0xxxxxxxxxxx' 报错原因是 本来应该写空间的 ...

  10. 650. Find Leaves of Binary Tree

    class Solution { public: vector<vector<int>> findLeaves(TreeNode* root) { vector<vect ...