<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="concreteImplementor" class="com.marving.aop.ConcreteImplementor" />
<bean id="interceptorHandler" class="com.marving.aop.InterceptorHandler" />
<aop:config>
<aop:aspect id="interceptor" ref="interceptorHandler">
<aop:pointcut id="addAllMethod" expression="execution(* com.marving.aop.Abstration.*(..))" />
<aop:before method="doSomething" pointcut-ref="addAllMethod" />
<aop:after method="doSomething" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
</beans>
public interface <span style="font-size:12px;">Abstration</span> {
public void operation()
}
//具体实现化角色
public class ConcreteImplementor implements Implementor{
@Override
public void operation() {
System.out.println("ConcreteImplementor");
}
}
public class InterceptorHandler{
public void printTime(){
System.out.println("CurrentTime = " + System.currentTimeMillis());
}
}
<bean id="MyInterceptor" class="com.yesjpt.interceptor. MyInterceptor"></bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>MyInterceptor</value>
</list>
</property>
</bean>
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* @author
*
*/
public class MyInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();//获取被拦截的方法
Object[] arguments = invocation.getArguments();//获取拦截方法的参数
/*
* 特殊,某些权限需要做特殊处理
* 比如用户信息权限,在方法执行完毕返回的时候,要将电话号码与邮箱抹除
*/
//环绕通知前置特殊处理
this.beforeReslove();
Object proceed = invocation.proceed();//调用目标方法
//环绕通知后置特殊处理
proceed = this.afterReslove();
return proceed;
}
private Object afterReslove() {
System.out.println("CurrentTime = " + System.currentTimeMillis());
return null;
}
private void beforeReslove() {
System.out.println("CurrentTime = " + System.currentTimeMillis());
}
}
<aop:aspectj-autoproxy/>
package com.marving.aop;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectHandler {
@Pointcut("execution(* com.marving.service.BaseServ+.*(..))")
private void doMethod() {
}
/**
* This is the method which I would like to execute before a selected method
* execution.
*/
@Before("doMethod()")
public void beforeAdvice() {
System.out.println("before method invoked.");
}
/**
* This is the method which I would like to execute after a selected method
* execution.
*/
@After("doMethod()")
public void afterAdvice() {
System.out.println("after method invoked.");
}
// 配置controller环绕通知,使用在方法aspect()上注册的切入点
@Around("doMethod()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
Object result = null;
String methodName = pjp.getSignature().getName();
try {
System.out.println("The method [" + methodName + "] begins with " + Arrays.asList(pjp.getArgs()));
result = pjp.proceed();
} catch (Throwable e) {
System.out.println("The method [" + methodName + "] occurs expection : " + e);
throw new RuntimeException(e);
}
System.out.println("The method [" + methodName + "] ends");
return result;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有