Integration of java spring of ssh-2

This is mainly on the integration of ssh, it is different from the one of the injection method.

This scanning will inject a way that the removal of applicationContext-asd.xml file.

Directory structure is as follows:

Note, just to name a different file

1. First, we look applicationContext-dao.xml file

<?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">
      <-! configuration mapping file ->
      </ Property>
          </ The props></ prop>to true
      <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>
          propagation represents the propagation characteristics of the transactionexpressed in the beginning of the method needs to save the transaction      
          <! -
          REQUIRED see whether the current transaction, if any, to use the current transaction, if not open a new transaction
          -> 
          <tx: Method, name = "the Save *" propagation = "REQUIRED" /> 
          <tx: Method, name = "Update *" propagation = "REQUIRED "/> 
          <TX: Method name =" Find * "Read-only =" to true "/> 
          <TX: Method name =" * "propagation =" REQUIRED "/> 
      </ TX: Attributes> 
  </ TX: the advice> 
  <! - configurable transactional AOP -> 
    <AOP: config> 
        <! - expression the functions specified transaction where 
        the first * represents all the returns from 
        the second * indicates all classes 
        third * indicates all classes method 
        .. represents all parameters
           -> 
        <aop: pointcut expression = "execution (* cn.vincent .service.impl.*.*(..))" id="pointcut"/>
        <AOP: the advice-REF = Advisor "txAdvice" the pointcut-REF = "the pointcut" /> 
    </ AOP: config> <- under specified automatically scan all packets and annotation sub-class of the packet, and completed in accordance with annotation! specify Job ->  
  <context: Scan-base- Component Package = "cn.vincent"> </ context: Scan-Component> 
</ Beans>
   

2.UserDaoImpl file

Package cn.vincent.dao.impl; 

Import java.util.List; 

Import the 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; 

// equivalent <the bean ID = "userDao" class = "cn.sxt.dao.impl.UserDaoImpl"> 
@Repository ( "userDao" )
 public  class UserDaoImpl the implements UserDao {
     // automatic injector bean corresponding to the query whether there is in the container 
    @Autowired
     Private the SessionFactory the sessionFactory; 
    @Override 
    public List<User> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from User").list();
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    
}

3. UserServiceImpl file

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 file

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;
    }
    
    
}

 

Above, using the same general approach is to automatically scan, to achieve the purpose of injection. Other previous same

github Address:

https://github.com/Vincent-yuan/spring_ssh2

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11260185.html