springでの事務の管理はxmlと注釈に基づいて、及びspringはwebで応用します.

8077 ワード

springの核心の内容はaopです.springの他の枠組みはともかく、spring自体は、springはビジネス層の枠組みに属しています.一番主要な核心の内容は各層の結合を実現することと、事務管理を実現することです.しかし、aopはどのように事務管理を実現しますか?コードは以下の通りです.ここではインタフェースフォントの実装コードを貼りません.dao層の実現クラス:
package com.leige.dao.impl;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.leige.dao.UserDao;

public class UserDaiImpl extends JdbcDaoSupport implements UserDao  {
	//      
	@Override
	public void update(String name, Double money) {
		
		try {
			String sql="update t_user set money=money+? where name=?;";
			super.getJdbcTemplate().update(sql,money,name);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		
	}

}
service層実装:
package com.leige.service.impl;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.leige.dao.UserDao;
import com.leige.service.UserService;

public class UserServiceImpl  implements UserService{
	UserDao userDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Transactional(isolation=Isolation.DEFAULT)
	@Override//    xml  ,        
	public void transfer(String from, String to, Double money) {
		// TODO Auto-generated method stub
		userDao.update(from, -money);
	/*	int i=5;
		i=i/0;*/
		userDao.update(to, money);
	}

}
エンティティクラス:
package com.leige.domain;

public class User {
	private Integer id;
	private String name;
	private Double money;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getMoney() {
		return money;
	}
	public void setMoney(Double money) {
		this.money = money;
	}
	
}
注釈と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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/context
       					   http://www.springframework.org/schema/context/spring-context.xsd
       					     http://www.springframework.org/schema/tx 
       					   http://www.springframework.org/schema/tx/spring-tx.xsd
       					   ">
       					<context:annotation-config/>
      <!--  					         -->
<bean id="dataSourse" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"/>
<property name="password" value=""/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="acquireIncrement" value="5"/>
<property name="maxPoolSize" value="50"/>
<property name="initialPoolSize" value="5"/>
</bean>
<!--          -->
<bean id="userService" class="com.leige.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<!--   dao     -->
<bean id="userDao" class="com.leige.dao.impl.UserDaiImpl">
<!--             -->
<property name="dataSource" ref="dataSourse"/>
</bean>
<!--         -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourse"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<!--       ,               ,         xml             
<tx:advice id="txAdvice" transaction-manager="txManager">

		<tx:attributes>
		          
		
		
						<tx:method>         
							name :       
								transfer     
								add* 	add  
								*	  
							propagation     
							isolation     
							read-only="false"     
							rollback-for=""       (-)
							no-rollback-for=""       (+)
						    :     
							<tx:method name="add*"/>
							<tx:method name="update*"/>
							<tx:method name="delete*"/>
							<tx:method name="find*" read-only="true"/>
		
		<tx:method name="transfer" read-only="false" isolation="DEFAULT"/>
		</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.leige.service.impl.*.*(..))" id="myPointCut"/>

<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"/>
</aop:config> -->
</beans>
スプリングのwebでのアプリケーションはServletでspring容器を取得する:
まずweb.xmlにspringのモニターと必要なパラメータを入れます.
	<!--    servletContext        xml  ,     WEB-INF/applicationContext.xml       
	       ,        -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
  <listener>
<!--     spring    ,              spring          beans.xml 
-->
  <description>spring            </description>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  
  </listener>
servletでspringを取得するコンテナ:
package com.leige.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.leige.service.UserService;

public class UserServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext servletContext=request.getServletContext();
		// Servlet   spring  :
		//  WebApplicationContextUtils      ApplicationContext,      ServletContext
		ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(servletContext);
		//           service    
		UserService userService=(UserService) context.getBean("userService");
		userService.transfer("leige", "leige2", 100.0);//    
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


	}

}
注解の実現に必要な注意点は、SPring管理に事務を任せることです.
xmlに設定された事務管理の部分を削除し、追加します.
<tx:annotation-driven transaction-manager="txManager"/>
また、サービス層の方法に事務管理を必要とするコメントを配置する必要があります.
すなわち、クラスに追加してもいいし、方法に追加してもいいです.コメントスキャンの設定を忘れないでください.
@Transactional(isolation=Isolation.DEFAULT)
	@Override
	public void transfer(String from, String to, Double money) {
		// TODO Auto-generated method stub
		userDao.update(from, -money);
	/*	int i=5;
		i=i/0;*/
		userDao.update(to, money);
	}