aspectj

Java Aspect returned value to be used in the method

谁说胖子不能爱 提交于 2019-12-02 13:15:21
问题 I have an @After java aspect that runs certain logic. I need it to return a result (an object) that can be used in the methods intercepted by the aspect's pointcut. Is it possible? 回答1: What you need is @Around which allows you to return whatever you want to the advised object: @Around("com.xyz.myapp.UserService.createUser()") public Object userCreationAdvice(ProceedingJoinPoint pjp) throws Throwable { //Do something if needed before method execution Object retVal = pjp.proceed(); //Do

Spring AOP框架 AspectJ

人盡茶涼 提交于 2019-12-02 12:55:25
1 AspectJ简介 v AspectJ是一个基于Java语言的AOP框架 v Spring2.0以后新增了对AspectJ切点表达式支持 v @AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面 v 新版本 Spring 框架,建议使用 AspectJ 方式来开发 AOP v 主要用途:自定义开发 2 切入点表达式【掌握】 execution() 用于描述方法 【掌握】 语法:execution(修饰符 返回值 包.类.方法名(参数) throws异常) 修饰符,一般省略 public 公共方法 * 任意 返回值,不能省略 void 返回没有值 String 返回值字符串 * 任意 包,[ 省略] com.zz.crm 固定包 com.zz.crm.*.service crm包下面子包任意 (例如:com.gyf.crm.staff.service) com.zz.crm.. crm包下面的所有子包(含自己) com.zz.crm.*.service.. crm包下面任意子包,固定目录service,service目录任意包 类,[ 省略] UserServiceImpl 指定类 *Impl 以Impl结尾 User* 以User开头 * 任意 方法名,不能省略 addUser 固定方法 add* 以add开头 *Do

Spring AOP Pointcut does not trigger

回眸只為那壹抹淺笑 提交于 2019-12-02 11:55:36
I am new to Spring and AOP. I am trying this simple thing where I have created a custom annotation which when placed before any method should execute some code. This is the annotation I created // Declares a custom annotation that validates json @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface JsonSchemaAnnotation { } Next I created the Spring Aspect class which holds the logic @Aspect public class UpdateUIMetadataInterceptor { @Pointcut("execution(public * com.fico.cardinal.cm.*.*(..))") public void anyPublicMethod() { System.out.println("Running"); } @Before(

Spring AOP: How to read path variable value from URI template in aspect?

大兔子大兔子 提交于 2019-12-02 08:09:21
问题 I want to create Spring aspect which would set method parameter, annotated by custom annotation, to an instance of a particular class identified by an id from URI template. Path variable name is parameter of the annotation. Very similar to what Spring @PathVariable does. So that controller method would look like: @RestController @RequestMapping("/testController") public class TestController { @RequestMapping(value = "/order/{orderId}/delete", method = RequestMethod.GET) public ResponseEntity<

How to modify the attributes of a returned object using AspectJ?

做~自己de王妃 提交于 2019-12-02 06:38:57
问题 I have a class that looks like follows (from Spring Roo DataOnDemand) which returns a new transient (not persisted) object for use in unit testing. This is what the code looks like after we do a push-in from Spring Roo's ITD. public class MyObjectOnDemand { public MyObjectOnDemand getNewTransientObject(int index) { MyObjectOnDemand obj = new MyObjectOnDemand(); return obj; } } What I need to do is make additional calls on the returned object reference to set additional fields that Spring Roo

第04章 AOP概述

我们两清 提交于 2019-12-02 05:41:56
第04章 AOP概述 1.AOP概述 ●AOP(Aspect-Oriented Programming, 面向切面编程 ):是一种新的方法论,是对传统 OOP(Object-Oriented Programming,面向对象编程)的补充。 ●AOP编程操作的主要对象是切面(aspect),而切面 模块化横切关注点 。 ●在应用AOP编程时,仍然需要定义公共功能,但可以明确的定义这个功能应用在哪里,以什么方式应用,并且不必修改受影响的类。这样一来横切关注点就被模块化到特殊的类里——这样的类我们通常称之为"切面"。 ●AOP的好处: ○每个事物逻辑位于一个位置,代码不分散,便于维护和升级 ○业务模块更简洁,只包含核心业务代码 2. AOP术语 横切关注点 从每个方法中抽取出来的同一类非核心业务。 切面(Aspect) 封装横切关注点信息的类,每个关注点体现为一个通知方法。 通知(Advice) 切面必须要完成的各个具体工作 目标(Target) 被通知的对象 代理(Proxy) 向目标对象应用通知之后创建的代理对象 连接点(Joinpoint) 横切关注点在程序代码中的具体体现,对应程序执行的某个特定位置。例如:类某个方法调用前、调用后、方法捕获到异常后等。 在应用程序中可以使用横纵两个坐标来定位一个具体的连接点: 切入点(pointcut): 定位连接点的方式

处理异常:org.springframework.beans.factory.BeanNotOfRequiredTypeException

寵の児 提交于 2019-12-02 04:53:29
异常信息: Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'baseDao' is expected to be of type [com.web.dao.base.BaseDaoImpl] but was actually of type [com.sun.proxy.$Proxy56] 分析处理: 这种情况一般在使用annotation的方式注入spring的bean出现,由于spring采用代理的机制导致的。 通过配置织入@Aspectj切面虽然可以通过编程的方式织入切面,但是一般情况下,我们还是使用spring的配置自动完成创建代理织入切面的工作。通过aop命名空间的声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。 当然,spring在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被隐藏起来了有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口

How do I use an ajc build parameter inside an aspect?

吃可爱长大的小学妹 提交于 2019-12-02 04:27:27
I need to know the name of my .jar inside the aspect so I can create a string field with it via @DeclareParents. I know I can pass things to the ajc compiler, but is it actually possible to use the passed arguments from the aspect? The end result should be classes with an additional field containing as value the name of my .jar. UPDATE: Test of suggestion. Gson.jar is a .jar on the classpath InputStream r = Gson.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"); String x = getString(r); System.out.println(x); Output: Manifest-Version: 1.0 Name: org/aspectj/lang/ Specification

How to modify the attributes of a returned object using AspectJ?

元气小坏坏 提交于 2019-12-02 04:24:59
I have a class that looks like follows (from Spring Roo DataOnDemand) which returns a new transient (not persisted) object for use in unit testing. This is what the code looks like after we do a push-in from Spring Roo's ITD. public class MyObjectOnDemand { public MyObjectOnDemand getNewTransientObject(int index) { MyObjectOnDemand obj = new MyObjectOnDemand(); return obj; } } What I need to do is make additional calls on the returned object reference to set additional fields that Spring Roo's auto-generated method is not taking care of. So without modifying the above code (or pushing it in

How to capture the parameters of System.out.println() in AspectJ's aspect and display in pointcut?

﹥>﹥吖頭↗ 提交于 2019-12-02 04:22:26
问题 I am very new to AspectJ...just started to learn. Till now i am able to get the parameters of user defined method in my aspect and print the captured parameter in my pointcut. Curiously i started to think to print the contents of System.out.println() in my pointcut's advice and written the following simple code: HelloClass.java: public class HelloClass { public static void main(String a[]) { System.out.println("hello sachin"); } } HelloAspect.java: public aspect HelloAspect { pointcut