`
kakarot_java
  • 浏览: 161762 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring aop 注释

阅读更多
在spring的xml配置文件中配置aspectj 如下:
Xml代码
<aop:aspectj-autoproxy /> 

<aop:aspectj-autoproxy />
创建注释的定义如下:
Java代码
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.METHOD)  
public @interface AroundPointCut {  
    boolean accessRead() default false;  


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AroundPointCut {
boolean accessRead() default false;
}
声明切面、声明切入点、拦截对目标对象方法调用如下:
Xml代码
<bean id="loggerAspect" class="com.aspect.AroundAspect"></bean> 

<bean id="loggerAspect" class="com.aspect.AroundAspect"></bean>

Java代码
@Aspect // 声明切面  
public class AroundAspect {  
 
    // 声明切入点  
    @Pointcut("execution(@com.crane.aspect.AroundPointCut public * * (..))")  
    public void aroundPointCut() {  
 
    }  
 
    // 拦截对目标对象方法调用  
    @Around("com.crane.aspect.LoggerAspect.aroundPointCut()")  
    public Object doLoggerPointCut(ProceedingJoinPoint jp) throws Throwable {  
        // 获取连接点的方法签名对象  
        MethodSignature joinPointObject = (MethodSignature) jp.getSignature();  
        // 连接点对象的方法  
        Method method = joinPointObject.getMethod();  
        // 连接点方法方法名  
        String name = method.getName();  
        Class<?>[] parameterTypes = method.getParameterTypes();  
        // 获取连接点所在的目标对象  
        Object target = jp.getTarget();  
        // 获取目标方法  
        method = target.getClass().getMethod(name, parameterTypes);  
        // 返回@AroundPointCut的注释对象  
        AroundPointCut joinPoint = method.getAnnotation(AroundPointCut.class);  
        if (!joinPoint.accessRead()) {  
            throw new ApplicationException("没有权限!");  
        }  
        return jp.proceed();  
    }  


@Aspect // 声明切面
public class AroundAspect {

// 声明切入点
@Pointcut("execution(@com.crane.aspect.AroundPointCut public * * (..))")
public void aroundPointCut() {

}

// 拦截对目标对象方法调用
@Around("com.crane.aspect.LoggerAspect.aroundPointCut()")
public Object doLoggerPointCut(ProceedingJoinPoint jp) throws Throwable {
// 获取连接点的方法签名对象
MethodSignature joinPointObject = (MethodSignature) jp.getSignature();
// 连接点对象的方法
Method method = joinPointObject.getMethod();
// 连接点方法方法名
String name = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
// 获取连接点所在的目标对象
Object target = jp.getTarget();
// 获取目标方法
method = target.getClass().getMethod(name, parameterTypes);
// 返回@AroundPointCut的注释对象
AroundPointCut joinPoint = method.getAnnotation(AroundPointCut.class);
if (!joinPoint.accessRead()) {
throw new ApplicationException("没有权限!");
}
return jp.proceed();
}
}
业务的服务添加@AroundPointCut注释如下:
Java代码
@AroundPointCut(accessRead = true)  
public Object queryProuct() throws ApplicationException {  
    //TODO  
    return null;  
}
分享到:
评论

相关推荐

    Spring Aop的简单实现

    一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。

    java springAOP 事务+注释

    java springAOP 事务+注释 带全部jar包! 即下即用!

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    spring AOP注解的应用1

    关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...

    spring注解aop配置详解

    最近使用了springAOP编程,文档里面包含了springAOP的代码示例及详细注释说明,使用的是注解配置模式

    spring-aop-5.3.15-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    spring-aop-5.3.12-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...

    springAOP实现数据字典.zip

    java springAOP实现数据字典

    Spring AOP 权限

    Spring 权限 这里是一个 spring aop 实现的一个权限 包括权限设计 这个不是很能懂 包括注释

    spring-aop-5.3.10-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...

    spring-aop-4.2.2.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...

    Spring AOP--日志管理

    Spring AOP--日志管理,注释齐全,欢迎大家共同交流。

    spring-aop-5.3.15-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...

    spring-aop-4.3.20.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

    spring-aop-5.3.7-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.7.jar; 赠送原API文档:spring-aop-5.3.7-javadoc.jar; 赠送源代码:spring-aop-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.7.pom; 包含翻译后的API文档:spring-aop...

    spring-aop-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....

    Spring源码解析4章150页+Spring3.2.4中文注释源码

    一阶段 1、Spring概述 2、一切从bean开始 ...Spring AOP的涉及原理及具体实践 SpringJDBC的涉及原理及二次开发 SpringMVC框架设计原理及手写实现 四阶段 Spring事务源码解析 需要其他源码请私信我

Global site tag (gtag.js) - Google Analytics