spring 基于Aspect和注解的切面编程(aop)
来源:互联网 发布:安卓改iphone6在线软件 编辑:程序博客网 时间:2024/04/30 14:16
spring bean配置中
<bean id="toggleDataSourceAdvice" class="com.gsww.jzfp.core.dataresource.aop.ToggleDataSourceAdvice"></bean> <aop:aspectj-autoproxy/>
注解代码如下:
/** * 用于注解数据切换 * @author wyy *@version 2015-12-02 */@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface ToggleDataSource {/** * 数据源名称 * @return */String name() default DataSourceContextHolder.BASE;}
切面通知基类
/** * 切面通知抽象基类 * @author wyy *@version 2015-12-02 * @param <T> */public abstract class AbstractAdvice<T extends Annotation> {protected final Class<T> annotationClass;protected AbstractAdvice(final Class<T> annotationClass) {this.annotationClass = annotationClass;}protected abstract Logger getLogger();protected void warn(final Exception e, final String format, final Object... args) { if (getLogger().isWarnEnabled()) { getLogger().warn(String.format(format, args), e); } }protected Method getMethod(final JoinPoint jp)throws NoSuchMethodException {final Signature sig = jp.getSignature();if (!(sig instanceof MethodSignature)) {throw new InvalidAnnotationException("This annotation is only valid on a method.");}final MethodSignature msig = (MethodSignature) sig;final Object target = jp.getTarget();// cannot use msig.getMethod() because it can return the method where// annotation was declared i.e. method in// interfaceString name = msig.getName();Class<?>[] parameters = msig.getParameterTypes();Method method = findMethodFromTargetGivenNameAndParams(target, name,parameters);return method;}protected Method findMethodFromTargetGivenNameAndParams(final Object target, final String name, final Class<?>[] parameters)throws NoSuchMethodException {Method method = target.getClass().getMethod(name, parameters);getLogger().debug("Method to cache: {}", method);return method;}protected void verifyReturnTypeIsList(final Method method, final Class<?> annotationClass) { if (!verifyTypeIsList(method.getReturnType())) { throw new InvalidAnnotationException( String.format("The annotation [%s] is only valid on a method that returns a [%s] or its subclass. " + "[%s] does not fulfill this requirement.", annotationClass.getName(), List.class.getName(), method.toString())); } }protected boolean verifyTypeIsList(final Class<?> clazz) { return List.class.isAssignableFrom(clazz); } protected void verifyReturnTypeIsNoVoid(final Method method, final Class<?> annotationClass) { if (method.getReturnType().equals(void.class)) { throw new InvalidParameterException(String.format("Annotation [%s] is defined on void method [%s]", annotationClass, method.getName())); } }}
切面处理类
/** * * <bean id="toggleDataSourceAdvice" class="com.gsww.jzfp.core.dataresource.aop.ToggleDataSourceAdvice"></bean> * <aop:aspectj-autoproxy/> * *//** * * @author QQ:16349023 * */@Aspectpublic class ToggleDataSourceAdvice extends AbstractAdvice<ToggleDataSource> {private static final Logger LOG = LoggerFactory.getLogger(ToggleDataSourceAdvice.class);public ToggleDataSourceAdvice(){super(ToggleDataSource.class);}@Pointcut("@annotation(com.gsww.jzfp.core.dataresource.aop.ToggleDataSource)") public void getTargetMethod() { /* pointcut definition */ } @Around("getTargetMethod()") public Object around(final ProceedingJoinPoint pjp) throws Throwable { //return cache(pjp); //System.out.println("Around1***********************"); final ToggleDataSource annotation; Object result; try { final Method method = getMethod(pjp); System.out.println("method:"+method.toString()); annotation = method.getAnnotation(ToggleDataSource.class); //根据注解在方法执行前进行处理 to do System.out.println("annotation:"+annotation.toString()); result = pjp.proceed(); } catch (Exception ex) { warn(ex, "Caching on method %s and key [%s] aborted due to an error.", pjp.toShortString()); result = pjp.proceed(); } //final Object result = pjp.proceed(); //System.out.println("Around2***********************"); return result; } @Before("getTargetMethod()") public void before() throws Throwable{ System.out.println("before***********************"); } @After("getTargetMethod()") public void after() throws Throwable{ System.out.println("after**************************"); } @Overrideprotected Logger getLogger() { return LOG; }}
0 0
- spring 基于Aspect和注解的切面编程(aop)
- spring实战-基于注解的面向切面编程(AOP)
- Spring注解实现AOP(面向切面编程Aspect Oriented Programming,AOP)
- Spring面向切面编程--AOP,AspectJ,基于注解方式。
- 自定义注解 Aspect 实现aop 日志记录切面编程
- Spring AOP编程中--@aspect 标签,切面编程
- SpringBoot:AOP Aspect切面编程
- spring面向切面编程AOP(Aspect-orented programming)
- Spring aop(Aspect Oriented Programming:面向切面编程)入门
- 基于@Aspect的AOP注解配置
- 基于注解@Aspect的AOP实现
- Java-基于注解的面向AOP(切面)编程
- Spring AOP切面基于注解基础学习
- Spring 面向切面(AOP)编程,注解
- spring-aop---AspectJ注解式切面编程。
- spring的aop:advisor和aop:aspect
- 面向切面编程(AOP)应用,日志切面,基于注解
- Spring的AOP切面编程
- view 中的 opaque , alpha , 和 alpha 的区别
- 使用JavaScript实现一个“字节码解释器”,并用它重新实现JS科学计算器的后端(后续1)
- Blog 收藏
- 关于js中 json对象和json字符串的总结
- 数组的基本操作
- spring 基于Aspect和注解的切面编程(aop)
- 【Java基础】——HTML隐藏域
- UILabel属性
- WPF 样式和资源 给某个元素设置Style样式
- 第十四周项目1线性表的折半查找(递归法)
- NSBundle
- lua学习 lua,c++对象调用流程
- 远程控制桌面,外网电脑PC手机ios安卓mac远程桌面连接内网linux和windows主机,史上最全最详细图文教程
- iOS懒加载