• SSH 整合- 6 - service_serviceImp…


    1. 流程图

     

    SSH <wbr>整合- <wbr>6 <wbr>- <wbr>service_serviceImpl+dao_daoImpl+struts2+spring_01

    2. 工程结构

     

    SSH <wbr>整合- <wbr>6 <wbr>- <wbr>service_serviceImpl+dao_daoImpl+struts2+spring_01

    3. Beans.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

               http://www.springframework.org/schema/context

               http://www.springframework.org/schema/context/spring-context-2.5.xsd

               http://www.springframework.org/schema/aop

               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

               http://www.springframework.org/schema/tx 

               http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.edu.ssh" />

     

    <bean

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">

    <value>classpath:jdbc.properties</value>

    </property>

    </bean>

     

    <bean id="dataSource" destroy-method="close"

    class="org.apache.commons.dbcp.BasicDataSource">

    <property name="driverClassName"

    value="${jdbc.driverClassName}" />

    <property name="url" value="${jdbc.url}" />

    <property name="username" value="${jdbc.username}" />

    <property name="password" value="${jdbc.password}" />

    </bean>

     

    <bean id="sessionFactory"

    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />

    <property name="packagesToScan">

    <list>

    <value>com.edu.ssh.model</value>

    </list>

    </property>

    <property name="hibernateProperties">

    <props>

    <prop key="hibernate.dialect">

    org.hibernate.dialect.MySQLDialect

    </prop>

    <prop key="hibernate.show_sql">true</prop>

    </props>

    </property>

    </bean>

     

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

    <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

     

    <bean id="txManager"

    class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property name="sessionFactory" ref="sessionFactory" />

    </bean>

     

    <!-- 用spring AOP给service层的方法添加事务管理 -->

    <aop:config>

    <aop:pointcut id="bussinessService"

    expression="execution(public * com.edu.ssh.service.*.*(..))" />

    <aop:advisor pointcut-ref="bussinessService"

    advice-ref="txAdvice" />

    </aop:config>

     

    <tx:advice id="txAdvice" transaction-manager="txManager">

    <tx:attributes>

    <tx:method name="exists" read-only="true" />

    <tx:method name="insert*" propagation="REQUIRED"/>

    </tx:attributes>

    </tx:advice>

     

    </beans>

    4. UserAction.java

    package com.edu.ssh.action;

     

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

     

    import com.edu.ssh.model.User;

    import com.edu.ssh.service.UserManager;

    import com.opensymphony.xwork2.ActionSupport;

     

    public class UserAction extends ActionSupport {


    private static final long serialVersionUID = 1L;

     

    // 用编译器生成getter/setter方法

    private int id;

    private String username;

    private String password;

    private String password2;

     

     

    private UserManager userManager;

     

    public UserAction() {

    // 直接用编码实现UserManeger的初始化

    // struts2的Action暂时还没交给spring管理,

    //下一个工程将会实现这个功能,

    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

    userManager = (UserManager) ctx.getBean("userManager");

    }

     

     

    public UserManager getUserManager() {

    return userManager;

    }

     

     

    public void setUserManager(UserManager userManager) {

    this.userManager = userManager;

    }

     

     

    @Override

    public String execute() throws Exception {

    User user = new User();

    user.setUsername(username);

    user.setPassword(password);

    if (userManager.exists(user))

    return "fail";

     

    userManager.insertOneUser(user);

    return "success";

    }

    // 。。。。。由于代码是和之前的是一样的在这就省略

    }

     

    5. UserActionTest.java

    package com.edu.ssh.action;

     

    import org.junit.After;

    import static org.junit.Assert.*;

    import org.junit.Before;

    import org.junit.Test;

     

    public class UserActionTest {

     

     

    @Test

    public void testExecute() {

    UserAction userAction = new UserAction();

    userAction.setUsername("hehe");

    userAction.setPassword("hehe");

    try {

               // 用户hehe已经存在

    assertEquals("fail", userAction.execute());

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

     

    }

     

    7. UserDAOImpl.java

    package com.edu.ssh.dao.impl;

     

    import java.util.List;

     

    import javax.annotation.Resource;

     

    import org.springframework.orm.hibernate3.HibernateTemplate;

    import org.springframework.stereotype.Component;

     

    import com.edu.ssh.dao.UserDAO;

    import com.edu.ssh.model.User;

     

    @Component

    public class UserDAOImpl implements UserDAO {

     

    private HibernateTemplate hibernateTemplate;

     

     

    public HibernateTemplate getHibernateTemplate() {

    return hibernateTemplate;

    }

     

     

    // 使用spring的依赖注入,默认找 名字为 hibernateTemplate 的bean

    @Resource

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {

    this.hibernateTemplate = hibernateTemplate;

    }

     

    // 。。。。。由于代码是和之前的是一样的在这就省略

     

    }

    8. UserManagerImpl.java

    package com.edu.ssh.service.impl;

     

    import javax.annotation.Resource;

     

    import org.springframework.stereotype.Component;

     

    import com.edu.ssh.dao.UserDAO;

    import com.edu.ssh.model.User;

    import com.edu.ssh.service.UserManager;

     

    // 用Component注解,使得本类交给spring容器管理,

    // 并且指定bean的名字为userManager

    @Component("userManager")

    public class UserManagerImpl implements UserManager {

     

     

    private UserDAO userDAO;

     

     

    public UserDAO getUserDAO() {

    return userDAO;

    }

     

     

    // 使用spring依赖注入

    @Resource

    public void setUserDAO(UserDAO userDAO) {

    this.userDAO = userDAO;

    }

     

    // 。。。。。由于代码是和之前的是一样的在这就省略

    }

     

    9. 结果

     

    SSH <wbr>整合- <wbr>6 <wbr>- <wbr>service_serviceImpl+dao_daoImpl+struts2+spring_01

     

    SSH <wbr>整合- <wbr>6 <wbr>- <wbr>service_serviceImpl+dao_daoImpl+struts2+spring_01



     

    SSH <wbr>整合- <wbr>6 <wbr>- <wbr>service_serviceImpl+dao_daoImpl+struts2+spring_01

     10. 分析

         有了spring就不需要hibernate.cfg.xml文件了,里面的配置都放在了spring的配置文件beans.xml里,HibernateUtil.java也不需要了,HibernateTemplate hibernateTemplate可以使用spring的依赖注入初始化除了struts2的Action 之外的实现类基本上都被spring管理了。要使得Action也被spring管理,我们还需要在web.xml里添加一些配置,还要添加一个struts2的插件。

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    JDBC-HikariCP
    11、JDBC-Druid
    JDBC-DBCP
    JDBC-C3P0
    第十七篇-使用RadioGroup实现单项选择
    第十六篇-使用CheckBox实现多项选择
    第一篇-ubuntu18.04访问共享文件夹
    第十五篇-EditText做简单的登录框
    第十四篇-ImageButton控制聚焦,单击,常态三种状态的显示背景
    第十三篇-通过Button设置文本背景颜色
  • 原文地址:https://www.cnblogs.com/ubuntuvim/p/4796550.html
Copyright © 2020-2023  润新知