aspectj

使用Spring Boot和AspectJ实现方法跟踪基础结构

删除回忆录丶 提交于 2019-12-14 21:16:33
了解如何使用Spring Boot和AspectJ实现方法跟踪基础结构!最近在优锐课学习收获颇多,记录下来大家一起进步! 在我们的应用程序中,获取方法的堆栈跟踪信息可能会节省很多时间。具有输入输出参数值和方法所花费的时间可以使查找问题变得更加容易。在本文中,我们将研究如何使用Spring Boot,AspectJ和Threadlocal为方法跟踪基础结构实现起点。 在此示例中,我使用了: Spring Boot Starter Web 2.1.7 Java 1.8 + AspectJ 1.8 Maven 3.2 1. 总览 在本教程中,我们将准备一个简单的REST服务,该服务将在书店中检索有关一本书的详细信息。然后,我们将添加一个 ThreadLocal 模型,该模型将在整个线程生命周期中保持堆栈结构。最后,我们将增加一个方面来削减调用堆栈中的方法,以获取输入/输出参数值。让我们开始吧! 项目结构 2. Maven依赖 Spring Boot Starter Web —使用Spring MVC的RESTful服务 Spring — 具备Aspect功能 AspectJ编织者向Java类引入建议 Apache Commons Lang —用于字符串实用程序 1 <parent> 2 <groupId>org.springframework.boot</groupId> 3

Spring源码(二)---AOP

℡╲_俬逩灬. 提交于 2019-12-14 17:09:23
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> AOP 基本概念 PointCut 切入点简单的理解就是定义了我们要切入的位置(在某些类 某些方法上切入)。因此Spring Aop的PointCut实现应该满足过滤得到目标类目标方法的需求。 从PointCut接口定义我们可以看到ClassFilter和MethodMatcher,ClassFilter判断那些类是要织入增强的,MethodMatcher是判断哪些方法是满足织入增强的。 public interface Pointcut { ClassFilter getClassFilter(); MethodMatcher getMethodMatcher(); Pointcut TRUE = TruePointcut.INSTANCE; } public interface ClassFilter { /** * 判断给定的clazz是否满足织入条件,满足则返回true */ boolean matches(Class<?> clazz); /** * Canonical instance of a ClassFilter that matches all classes. * 这个默认的Filter是说所有的类都满足增强条件,都会返回true */ ClassFilter TRUE =

AOP底层源码分析

孤人 提交于 2019-12-14 07:50:11
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 思维导图 AOP AOP: 面向切面编程[底层就是动态代理] 指程序在运行期间动态的将某段代码切入到指定方法位置进行运行的编程方式。 AOP通知方式 前置通知: logStart(),在目标方法(div)运行之前运行 (@Before) 后置通知:logEnd(), 在目标方法(div)运行结束之后运行,无论正常或异常结束 (@After) 返回通知:logReturn, 在目标方法(div)正常返回之后运行 (@AfterReturning) 异常通知:logException, 在目标方法(div)出现异常后运行(@AfterThrowing) 环绕通知:动态代理, 最底层通知,手动指定执行目标方法(@Around) public class Calculator { //业务逻辑方法 public int div(int i, int j) { System.out.println("--------"); return i / j; } } import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; //日志切面类 @Aspect public class LogAspects {

Entity Aspect (in Spring)

隐身守侯 提交于 2019-12-14 04:25:18
问题 I'm having a bit of a problem defining my aspects. I've got a bunch of entities that I'd like to profile the get-methods in, so I've written the following pointcut and method @Pointcut("execution(* tld.myproject.data.entities.*.get*()") public void getEntityProperty() {} @Around("getEntityProperty()") public Object profileGetEntityProperty(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); String name = pjp.getSignature().getName(); Object output = pjp

Spring with AspectJ compile-time weaving causing: java.lang.VerifyError: Illegal use of nonvirtual function call

懵懂的女人 提交于 2019-12-14 03:49:26
问题 I am trying to use Spring's @AspectJ compile-time weaving instead of <aop:autoproxy/> and it is causing some errors. First there are some warnings during the compilation phase: [WARNING] can not resolve this member: x.y.z.Severity[] x.y.z.ExceptionSeverity.values() [Xlint:unresolvableMember] Then when trying to run some tests that have advised methods I get the following exception: java.lang.VerifyError: (class: x/y/z/MonitoringAspect, method: ajc$inlineAccessMethod$x_y_z_MonitoringAspect$cx

Dependency Injection into Spring non-managed beans

不羁的心 提交于 2019-12-14 03:48:57
问题 I have a JPA domain class that is non managed. It is instantiated via the new operator. UserAccount account = new UserAccount(); userRepository.save(account) In my UserAccount class, I have a beforeSave() method which is dependent on my SecurityService to hash encode a password. My questions is "How do I get spring DI to inject the security service into my entity?". Seems that AspectJ and LoadTimeWeaving is what I need. I've tried an array for configurations, but I can't seem to get any of

Using aspectj to intercept an assignment with reflection

↘锁芯ラ 提交于 2019-12-14 03:21:36
问题 When I run the application with tomcat it says that the advices has not been aplied, so my aspects won't work. Do I have to configure that anywhere? I haven't done anything about it, so I don't know what code might be useful. Thank you! EDIT I've just found out how to fix that, and even when it says that the aspect has not been applied, when I call the setter it works, but I have a problem when using reflection for that. I have an aspect which intercept a setter to a field which works well,

AspectJ Pointcut call on JAX-RS annotated Interface method

北慕城南 提交于 2019-12-14 03:17:14
问题 I'm trying to intercept a method of an interface annoted with a JAX-RS @POST. My Pointcut works for all non-interface methods and if the @POST-Annotation is directly at the called method. The interface method to intercept: @POST Response postToConnector(@Context CallContext callContext, String contentStream) throws Exception; The Pointcut to match the method: @Pointcut("call(@(javax.ws.rs.DELETE || javax.ws.rs.GET || javax.ws.rs.HEAD || javax.ws.rs.OPTIONS || " + "javax.ws.rs.POST || javax.ws

Weaving production aspect into test class with Gradle AspectJ

ⅰ亾dé卋堺 提交于 2019-12-14 02:43:59
问题 I am using the Gradle AspectJ plugin to weave some production aspect into test Java code. I would have expected this to work out of the box with the plugin, but apparently that's not the case as demoed here: https://github.com/sedubois/gradle-aspectj-poc/tree/dc44f529831a485fcff8f4889dba8098784dddb4 The weaving of UnsupportedOperationAspect into MainSevice (both under src/main/java ) works, but the weaving of this same aspect into TestService (under src/test/java ) doesn't. I am new to Groovy

AspectJ: ClassCastException when trying to intercept object creation

家住魔仙堡 提交于 2019-12-14 02:29:15
问题 I'm trying to intercept object creation in legacy code to return another object. My sample code: public class ObjectCreationTest { interface A { String say(); } public static class MyImpl implements A { @Override public String say() { return "MyImpl"; } } public static class YourImpl implements A { @Override public String say() { return "YourImpl"; } } public static void main(String[] args) { A obj = new MyImpl(); System.out.println(obj.getClass()); System.out.println(obj.say()); } } @Aspect