zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ORACLE定时备份
    解压cpio.gz
    Fusioncharts的数字格式化
    linux apache+php+mysql安装及乱码解决办法
    两个Beta函数类型的积分及其一般形式
    【转载】巴塞尔问题(Basel Problem)的多种解法
    两本关于各种刁钻积分计算的书
    一个超几何函数类型的积分
    一个取整函数积分的一般形式
    Clausen Functions (and related series, functions, integrals)
  • 原文地址:https://www.cnblogs.com/Vincent-yuan/p/11260185.html
Copyright © 2011-2022 走看看