以AspectJ实现AOP事物

1 使用Annotation方式
spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar

使用Aspect定义切入点

package com.travelsky.aop.aspect; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.AfterReturning; 
@Aspect 
public class serviceAspect { 
   @Pointcut("execution(* com.travelsky.service..*.*(..))") 
   public void doBeforeService() {
   } 
   @Pointcut("execution(* com.travelsky.service..*.*(..))") 
   public void doAfterService() {
   } 
} 

 

定义advice

package com.travelsky.aop.advice; 
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class serviceAdvice{
  @Before("com.travelsky.aop.aspect.serviceAspect.doBeforeService()")
  public void doAccessCheck() {
	// ...
	System.out.println("Aop: do before service."); 
  }
  @AfterReturning("com.travelsky.aop.aspect.serviceAspect.doAfterService()")
  public void logInfo() {
	// ...
	System.out.println("Aop: do after service");
  }
}

 

在Spring配置文件applicationContext.xml中配置


 
 
 

 

2 配置文件方式
spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar

 

在applicationContext.xml中配置


   
    
   
     
    
     
   

 

3 给出一个生产中实际使用的AspectJ管理AOP事物的配置文件(配置方式实现)




	
	
		
			
				classpath*:config.properties
			
		
	

	
	

	
	

	
	
		
					
		
		
		
		
	

	
	
		
			
			
			    
                
                
           	
			
		
	

 

 

你可能感兴趣的:(Spring)