spring aop

 

 

 

<?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:security="http://www.springframework.org/schema/security"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/cache 
		http://www.springframework.org/schema/cache/spring-cache.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/security 
		http://www.springframework.org/schema/security/spring-security.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd" >

	
	<!-- 启用组件注解扫描 -->
	<context:component-scan base-package="com.jaeson.springstudy.aop" />
	
	<!-- 启用AOP注解扫描,@Aspect/@Pointcut/@Before/@After/@Around/@AfterReturning/@AfterThrowing -->
	<aop:aspectj-autoproxy expose-proxy="true"/>

</beans>

 

package com.jaeson.springstudy.aop;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jaeson.hibernatestudy.bean.User;
import com.jaeson.springstudy.aop.AopService;

public class TestAop {

	private ClassPathXmlApplicationContext context;
	
	@Before
	public void before() {
		context = new ClassPathXmlApplicationContext(new String[] {"testAOP.xml"});
	}
	
	@After
	public void after() {
		context.close();
	}
	
	@Test
	public void testMethodAop() {
		
		AopService service = context.getBean("aopService", AopService.class);
		System.out.println("====================================");
		service.get(100086L);
		System.out.println("====================================");
		service.save(new User());
		System.out.println("====================================");
		service.selfInvoke();
		System.out.println("====================================");
		service.selfInvoke(new User());
		System.out.println("====================================");
		service.selfInvocation();
		System.out.println("====================================");
		try {
			service.delete(95977L);
		} catch (RuntimeException ex) {
			System.out.println(ex.getMessage());
		}
		System.out.println("====================================");
		
	}
}

 

package com.jaeson.springstudy.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AopExample {
	
	private static final Logger logger = LoggerFactory.getLogger(AopExample.class);

	@Pointcut("execution(* com.jaeson..dao..*.*(..))")
    public void dataAccessOperation() {}
	
    @Pointcut("execution(* com.jaeson..service..*.*(..))")
    public void businessService() {}
    
    @Pointcut("execution(* com.jaeson.springstudy.aop.*Service.*(..)))")
    public void aspect() {}

    /*
	 * 配置前置通知,使用在方法businessService()上注册的切入点
	 * 同时接受JoinPoint切入点对象,可以没有该参数
	 */
	@Before("businessService()")
	public void before(JoinPoint joinPoint) {
		
		logger.info("before {}", joinPoint);
	}

	//配置后置通知,使用在方法businessService()上注册的切入点
	@After("businessService()")
	public void after(JoinPoint joinPoint) {

			logger.info("after {}", joinPoint);
	}

	//配置环绕通知,使用在方法dataAccessOperation()上注册的切入点
	//@Around的返回类型必须为Object,否则在织入非void返回类型的切入点时会抛出异常:
	//Null return value from advice does not match primitive return type for:
	@Around("dataAccessOperation()")
	public Object around(JoinPoint joinPoint) throws Throwable {
		
		Object result = null;
		long start = System.currentTimeMillis();
		
		logger.info("begin around {} !", joinPoint);
		result = ((ProceedingJoinPoint) joinPoint).proceed();
		long end = System.currentTimeMillis();
		logger.info("end around {} Use time : {} ms!", joinPoint, (end - start));
		
		return result;
	}

	//配置后置返回通知,使用在方法businessService()上注册的切入点
	@AfterReturning("businessService()")
	public void afterReturn(JoinPoint joinPoint) {

		logger.info("afterReturn {}", joinPoint);
	}

	//配置抛出异常后通知,使用在方法businessService()上注册的切入点
	@AfterThrowing(pointcut="businessService()", throwing="ex")
	public void afterThrow(JoinPoint joinPoint, RuntimeException ex) {
		
		logger.info("afterThrow {} with exception : {}", joinPoint, ex.getMessage());
	}
	
	//配置前置通知,拦截返回值类型为com.jaeson.hibernatestudy.bean.User的方法
	@Before("execution(com.jaeson.hibernatestudy.bean.User com.jaeson.springstudy.aop.*Service.*(..))")
	public void beforeReturnUser(JoinPoint joinPoint) {

		logger.info("beforeReturnUser {}", joinPoint);
	}

	//配置前置通知,拦截参数类型为com.jaeson.hibernatestudy.bean.User的方法
	@Before("execution(* com.jaeson.springstudy.aop.*Service.*(com.jaeson.hibernatestudy.bean.User))")
	public void beforeArgUser(JoinPoint joinPoint) {

		logger.info("beforeArgUser {}", joinPoint);
	}

	//配置前置通知,拦截含有long类型参数的方法,并将参数值注入到当前方法的形参id中
	@Before("aspect() && args(id)")
	public void beforeArgId(JoinPoint joinPoint, long id) {

		logger.info("beforeArgId {} ({})", joinPoint, id);
	}
}

 

package com.jaeson.springstudy.aop;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.aop.framework.AopContext;

import com.jaeson.hibernatestudy.bean.User;

@Service
public class AopService {
	
	private static final Logger logger = LoggerFactory.getLogger(AopService.class);
	
	//自调用无法触发aop的解决办法
	//开启暴露Aop代理到ThreadLocal支持
	//<aop:aspectj-autoproxy expose-proxy="true"/><!—注解风格支持--> 
	//<aop:config expose-proxy="true"><!—xml风格支持-->   
	public void selfInvocation() {
		logger.info("AopService.selfInvocation() method . . .");
		((AopService)AopContext.currentProxy()).get(0L);
	}
	
	
	//非aop方法的自调用不会触发this方法的aop
	public void selfInvoke() {
		logger.info("AopService.selfInvoke() method . . .");
		this.get(0L);
	}
	//aop方法的自调用不会触发this方法的aop
	public void selfInvoke(User user) {
		logger.info("AopService.selfInvoke(User) method . . .");
		this.save(user);
	}
	
	
	public User get(long id) {

		logger.info("AopService.get(Long) method . . .");
		return new User();
	}

	public void save(User user) {
		
		logger.info("AopService.save(User) method . . .");
	}

	public void delete(long id) throws RuntimeException {

		logger.info("AopService.delete(Long) method . . .");
		throw new UnsupportedOperationException("AopService.delete(Long) throw UnsupportedOperationException");
	}

}

 

 

     [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopExample.beforeArgId(AopExample.java:97) beforeArgId execution(User com.jaeson.springstudy.aop.AopService.get(long)) (100086) 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopExample.beforeReturnUser(AopExample.java:83) beforeReturnUser execution(User com.jaeson.springstudy.aop.AopService.get(long)) 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.get(AopService.java:39) AopService.get(Long) method . . . 

    ====================================

[INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopExample.beforeArgUser(AopExample.java:90) beforeArgUser execution(void com.jaeson.springstudy.aop.AopService.save(User)) 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.save(AopService.java:45) AopService.save(User) method . . . 

    ====================================

[INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.selfInvoke(AopService.java:27) AopService.selfInvoke() method . . . 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.get(AopService.java:39) AopService.get(Long) method . . . 

    ====================================

[INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopExample.beforeArgUser(AopExample.java:90) beforeArgUser execution(void com.jaeson.springstudy.aop.AopService.selfInvoke(User)) 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.selfInvoke(AopService.java:32) AopService.selfInvoke(User) method . . . 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.save(AopService.java:45) AopService.save(User) method . . . 

    ====================================

[INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.selfInvocation(AopService.java:20) AopService.selfInvocation() method . . . 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopExample.beforeArgId(AopExample.java:97) beforeArgId execution(User com.jaeson.springstudy.aop.AopService.get(long)) (0) 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopExample.beforeReturnUser(AopExample.java:83) beforeReturnUser execution(User com.jaeson.springstudy.aop.AopService.get(long)) 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.get(AopService.java:39) AopService.get(Long) method . . . 

    ====================================

[INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopExample.beforeArgId(AopExample.java:97) beforeArgId execution(void com.jaeson.springstudy.aop.AopService.delete(long)) (95977) 

    [INFO][2016-10-07 15:29:16] com.jaeson.springstudy.aop.AopService.delete(AopService.java:50) AopService.delete(Long) method . . . 

    AopService.delete(Long) throw UnsupportedOperationException

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值