Tag: aspectj

在切入点内获取带注释的参数

我有两个注释@LookAtThisMethod和@LookAtThisParameter ,如果我使用@LookAtThisMethod对方法进行@LookAtThisMethod ,我怎样才能提取用@LookAtThisParameter注释的所述方法的参数? 例如: @Aspect public class LookAdvisor { @Pointcut(“@annotation(lookAtThisMethod)”) public void lookAtThisMethodPointcut(LookAtThisMethod lookAtThisMethod){} @Around(“lookAtThisMethodPointcut(lookAtThisMethod)”) public void lookAtThisMethod(ProceedingJoinPoint joinPoint, LookAtThisMethod lookAtThisMethod) throws Throwable { for(Object argument : joinPoint.getArgs()) { //I can get the parameter values here } //I can get the method signature with: joinPoint.getSignature.toString(); //How do I get which parameters are annotated with @LookAtThisParameter? […]

AspectJ可以穿过sun.net。*包吗?

我正在使用AspectJ来拦截java.net.Socket调用。 我创造了非常简单的方面 after(): call(* java.net.Socket.connect(..)) { System.out.println(“Connect intercepted!”); } 和aop.xml 当调用堆栈是这样的,那么我可以看到控制台输出: java.lang.Exception at com.iggroup.lightstreamer.nwtp.SocketExceptionLoggingAspect.ajc$after$com_iggroup_lightstreamer_nwtp_SocketExceptionLoggingAspect$2$6e16217c(SocketExceptionLoggingAspect.aj:39) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:337) at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:91) at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:596) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475) at com.iggroup.lightstreamer.nwtp.users.SsoRestClientImpl.lambda$0(SsoRestClientImpl.java:68) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at […]

使用AspectJ实现虫洞模式

我正在寻找使用AspectJ的虫洞模式实现的一个例子(如果Guice AOP有能力实现这一点,将会感兴趣)。 蠕虫洞本质上允许您沿着调用流传递其他参数,例如: // say we have class foo { public int m0 int a, int b) { return m1(a,b); } public int m1 int a, int b) { return m2(a,b); } public int m2 int a, int b) { return a+b; } } // and I wanted in a non-invasive manner to pass a […]

带有注释的类的子类的@AspectJ切入点

我正在寻找一个切入点,该切入点匹配将类与特定注释子类化的类中的方法执行。 优秀的AspectJ备忘单帮我创建了以下切入点: within(@my.own.annotations.AnnotationToMatch *) && execution(* *(..)) 这匹配带有@AnnotationToMatch的类A的所有方法调用,但不匹配扩展A的类B的方法。如何匹配两者?

AspectJ在Maven项目中,不工作/编织

我正在尝试让AspectJ编织在一个简单的Maven项目中工作,并且不确定它出错的地方:当我使用“mvn exec:java”运行代码时,我看不到预期的输出。 我确信代码正常工作,因为我在STS中尝试了相同的工作,它工作正常。 我只是想让AspectJ在Maven项目中工作。 任何有关如何调试此类问题的提示将不胜感激。 4.0.0 com.aop aop1 0.0.1-SNAPSHOT jar aop1 http://maven.apache.org UTF-8 junit junit 3.8.1 test org.aspectj aspectjrt 1.7.3 org.codehaus.mojo aspectj-maven-plugin compile 1.7 1.7 true org.codehaus.mojo exec-maven-plugin 1.1 com.aop.aop1.App Aspect文件与代码位于同一文件夹中: package com.aop.aop1; public aspect aspect { pointcut secureAccess() : execution(* *.foo(..)); before() : secureAccess() { System.out.println(“BEHOLD the power of AOP !!!”); } } Java文件: […]

Spring乐观锁定:如何重试事务方法直到提交成功

我使用Spring 2.5和Hibernate JPA实现Java和“容器”管理事务。 我有一个“后用户提交”方法,它在后台更新数据,无论ConcurrencyFailureException还是StaleObjectStateExceptionexception都需要提交,因为它永远不会显示给客户端。 换句话说,需要将乐观锁定变为悲观。 (如果方法执行需要更长的时间并且有人在其他事务中更改了数据,则可能发生) 我读了很多关于幂等的东西,如果exception搜索DEFAULT_MAX_RETRIES或6.2.7则重试。 示例或第14.5章。 重试 。 我也在这里和这里找到了stackoverflow。 我试过这个: public aspect RetryOnConcurrencyExceptionAspect { private static final int DEFAULT_MAX_RETRIES = 20; private int maxRetries = DEFAULT_MAX_RETRIES; Object around(): execution( * * (..) ) && @annotation(RetryOnConcurrencyException) && @annotation(Transactional) { int numAttempts = 0; RuntimeException failureException = null; do { numAttempts++; try { return proceed(); […]

如何使用maven构建aspectj项目?

我在Eclipse ide中创建了一个Aspectj项目,但我需要使用maven构建它。 我有maven-aspectj插件但不知道如何使用它。

使用Autoproxy在AspectJ模式下无法使用@Secured注释

我正在尝试让我的Spring MVC应用程序与Spring @Secured注释和AspectJ自动代理一起使用,但它似乎不代理或识别我的@Secured注释。 我有一个像这样的控制器: @Controller @RequestMapping(“/”) public class ApplicationController { private ApplicationFactory applicationFactory; @Inject public ApplicationController(ApplicationFactory applicationFactory) { super(); this.applicationFactory = applicationFactory; } @Secured(“ROLE_USER”) @ResponseBody @RequestMapping(method = GET) public Application getApplicationInfo() { return applicationFactory.buildApplication(this); } } 一个Spring安全XML看起来像这样: 码: 以上是由no-xml Spring @Configuration组件加载的,如下所示: @Configuration @ComponentScan(basePackages = {“com.example”}) @EnableWebMvc @ImportResource(“classpath:security.xml”) public class ApplicationConfiguration extends WebMvcConfigurerAdapter { } 反过来使用Servlet […]

获取Java中注释的参数值

所以我有一个代码: @Path(“/foo”) public class Hello { @GET @Produces(“text/html”) public String getHtml(@Context Request request, @Context HttpServletRequest requestss){ … } 我使用AspectJ来捕获所有对getHtml方法的调用。 我想在我的建议中将参数传递给@Produces和@Path ,在这种情况下是”/foo”和”text/html” 。 我怎么能用reflection呢?

将AspectJ添加到pom.xml改为使用Maven的Java版本,为什么?

更新:这是我的maven-compiler-plugin配置: org.apache.maven.plugins maven-compiler-plugin 2.3.2 1.6 1.6 我在使用Maven构建的多项目应用程序上工作。 我们决定添加AspectJ,所以我将以下代码添加到顶级项目中的pom.xml :(来自官方文档) … … org.aspectj aspectjrt 1.7.3 … … org.codehaus.mojo aspectj-maven-plugin 1.5 compile test-compile … … 以及每个下属项目的以下片段: … …. … org.codehaus.mojo aspectj-maven-plugin … org.aspectj aspectjrt …. … 不知怎的,这个修改已经覆盖了我使用的Java版本。 如果我运行build我会得到多个这样的错误: 语法错误,注释仅在源级别为1.5或更高时可用 这让我怀疑我的Java版本(原来是1.6)以某种方式还原为1.4。 我什么也没做 – 至少不是故意的 – 可能影响Java版本,所以我怀疑上面提到的AspectJ相关代码负责改变。 我的问题是AspectJ如何改变Java版本以及如何解决这个问题。 或者我是否完全误解了一些事情,我是否走错了路?