基于spring的advice实现

siye@r480:~/svlution/workspace/springaop4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springaop
│   │               ├── advice
│   │               │   ├── AfterReturningAdviceImpl.java
│   │               │   ├── MethodBeforeAdviceImpl.java
│   │               │   ├── MethodInterceptorImpl.java
│   │               │   └── ThrowsAdviceImpl.java
│   │               ├── setup
│   │               │   └── Appconfig.java
│   │               └── target
│   │                   └── User.java
│   └── resources
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springaop
    │               └── target
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

17 directories, 9 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springaop.advice;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Component;

// 后置通知推荐实现的接口,具体可参考 Advice AfterAdvice 接口的规范
@Component
public class AfterReturningAdviceImpl implements AfterReturningAdvice {

	private final Logger logger = Logger.getLogger(this.getClass());

	// 在spring中默认是没有后置通知和后置返回通知,两者的通知是在一起同时发生的。
	// 若要使用细化的功能,可以实现 AfterAdvice 接口进行自定义的功能扩展。
	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		logger.info(returnValue.toString());
		logger.info(method.toString());
		logger.info(Arrays.toString(args));
		logger.info(target.toString());
		logger.info("return advice");
	}

}
package ocn.site.springaop.advice;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

//前置通知推荐实现的接口,具体参考 Advice BeforeAdvice 的组织结构。
@Component
public class MethodBeforeAdviceImpl implements MethodBeforeAdvice {

	private final Logger logger = Logger.getLogger(this.getClass());

	// 接口参数的说明。
	// method 表示当前拦截的方法对象。
	// args 表示当前拦截的方法参数数组对象。
	// target 表示当前拦截的方法所属的类对象实例。
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		logger.info(method.toString());
		logger.info(Arrays.toString(args));
		logger.info(target.toString());
		logger.info("before advice");
	}

}
package ocn.site.springaop.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class MethodInterceptorImpl implements MethodInterceptor {

	private final Logger logger = Logger.getLogger(this.getClass());

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		logger.info("start method");
		Object value = invocation.proceed();
		logger.info("ending method");
		return value;
	}

}
package ocn.site.springaop.advice;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.stereotype.Component;

// 异常通知推荐使用的接口,注意此接口是个标记接口。
@Component
public class ThrowsAdviceImpl implements ThrowsAdvice {

	private final Logger logger = Logger.getLogger(this.getClass());

	// method defined reference superclass doc
	public void afterThrowing(Method method, Object[] args, Object target, Exception ex) {
		logger.info(ex.toString());
		logger.info("exception advice");
	}

}
package ocn.site.springaop.setup;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan({ "ocn.site.springaop.advice", "ocn.site.springaop.target" })
public class Appconfig {

	@Bean
	public ProxyFactory getProxyFactory() {
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setProxyTargetClass(true);
		return proxyFactory;
	}

}
package ocn.site.springaop.target;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

//主要参考 Advice 接口
//此测试的是基于spring的 aop的实现
//
//在advice通知的方面,这些通知的实现,都会切入目标类的所有方法.
//
//5种通知方式需要实现的约定接口
//\____ org.springframework.aop.MethodBeforeAdvice
//\____ org.springframework.aop.AfterReturningAdvice
//\____ org.springframework.aop.ThrowsAdvice
//\____ org.aopalliance.intercept.MethodInterceptor
@Component
public class User {

	private final Logger logger = Logger.getLogger(this.getClass());

	public int work() {
		logger.info("start work");
		return 0;
	}

	public void throwException() {
		throw new NullPointerException();
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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">

	<!--完全基于spring内置的aop是有很大的不足之处的
		那就是切面的细化度不够,若是结合aspectj是能完成这点要求的。
		但是显然是多此一举。
		还不如直接的使用aspectj
		目前spring4.x版本对aspectj已经全面支持了。 -->

	<bean class="org.springframework.aop.framework.ProxyFactory">
		<property name="proxyTargetClass" value="true"></property>
	</bean>

	<bean class="ocn.site.springaop.target.User"></bean>

	<bean class="ocn.site.springaop.advice.MethodBeforeAdviceImpl"></bean>
	<bean class="ocn.site.springaop.advice.AfterReturningAdviceImpl"></bean>
	<bean class="ocn.site.springaop.advice.ThrowsAdviceImpl"></bean>
	<bean class="ocn.site.springaop.advice.MethodInterceptorImpl"></bean>

</beans>
package ocn.site.springaop.target;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import ocn.site.springaop.advice.AfterReturningAdviceImpl;
import ocn.site.springaop.advice.MethodBeforeAdviceImpl;
import ocn.site.springaop.advice.MethodInterceptorImpl;
import ocn.site.springaop.advice.ThrowsAdviceImpl;

@RunWith(SpringRunner.class)
//@ContextConfiguration(classes = Appconfig.class)//用于测试java-style的配置
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private @Autowired ProxyFactory proxyFactory;
	private @Autowired User target;
	private @Autowired MethodBeforeAdviceImpl beforeAdviceImpl;
	private @Autowired AfterReturningAdviceImpl returningAdviceImpl;
	private @Autowired ThrowsAdviceImpl throwsAdviceImpl;
	private @Autowired MethodInterceptorImpl aroundAdviceImpl;

	@Test
	public void beforeAdvice() throws Exception {
		proxyFactory.setTarget(target);
		proxyFactory.addAdvice(beforeAdviceImpl);
		User user = (User) proxyFactory.getProxy();
		user.work();
	}

	@Test
	public void returnAdvice() throws Exception {
		proxyFactory.setTarget(target);
		proxyFactory.addAdvice(returningAdviceImpl);
		User user = (User) proxyFactory.getProxy();
		user.work();
	}

	@Test
	public void throwsAdvice() throws Exception {
		proxyFactory.setTarget(target);
		proxyFactory.addAdvice(throwsAdviceImpl);
		User user = (User) proxyFactory.getProxy();
		user.throwException();
	}

	@Test
	public void aroundAdvice() throws Exception {
		proxyFactory.setTarget(target);
		proxyFactory.addAdvice(aroundAdviceImpl);
		User user = (User) proxyFactory.getProxy();
		user.work();
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值