前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java之spring之整合ssh-2

java之spring之整合ssh-2

作者头像
Vincent-yuan
发布2019-09-11 14:58:07
3750
发布2019-09-11 14:58:07
举报
文章被收录于专栏:Vincent-yuanVincent-yuan

这篇也是主要讲解 ssh 的整合,不同于上一篇的是它的注入方式。

这篇会采用扫描注入的方式,即去除 applicationContext-asd.xml 文件。

目录结构如下:

注意,这里只列举不同的文件

1. 首先,我们看下 applicationContext-dao.xml 文件

代码语言:javascript
复制
<?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 文件

代码语言:javascript
复制
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 文件

代码语言:javascript
复制
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 文件

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档