这篇也是主要讲解 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

最新文章

  1. 内核终端判断,微信?QQ?ipad?IE?移动?Google?opera……
  2. ECMAScript继承
  3. 【转】错误日志ID8021来源BROWSER导致电脑死机
  4. JavaScript语言基础知识8
  5. Activity的生命周期与加载模式——Activity的生命周期演示
  6. Use Prerender to improve AngularJS SEO
  7. 使用XML序列化实现系统配置 - 开源研究系列文章
  8. OutputStream与PrintWriter的使用与区别
  9. Consul集群搭建 2Server+ 3Client
  10. localStorage sessionStorage 用法
  11. SpringMVC-DispatcherServlet工作流程及web.xml配置
  12. 阿里中间件——消息中间件Notify和MetaQ
  13. go mod代理和小技巧
  14. ES6知识整理(2)--变量的解构赋值
  15. 这个不是第一次作业----艰难的安装Android studio历程
  16. 44 道 JavaScript 难题
  17. 541. Reverse String II
  18. mssql手工注入1
  19. foreign key constraint fails错误的原因
  20. IBATIS中‘$’与‘#’使用

热门文章

  1. 后台接收参数报错 Required String parameter &#39;id&#39; is not present
  2. [Beta阶段]第六次Scrum Meeting
  3. 关于用maven创建的springboot工程打包jar后找不到配置文件的问题
  4. 范仁义html+css课程---11、html补充知识
  5. AES采用CBC模式128bit加密工具类
  6. 原创:【ajax | axios跨域简单请求+复杂请求】自定义header头Token请求Laravel5后台【亲测可用】
  7. zookeeper acl认证机制及dubbo、kafka集成、zooviewer/idea zk插件配置
  8. ImportError: this is MySQLdb version (1, 2, 5, &#39;final&#39;, 1), but _mysql is version (1, 4, 4, &#39;final&#39;, 0)
  9. fatal error: linux/videodev.h: No such file or directory
  10. springboot踩坑记