标签: spring-aspects

春天自动化了循环依赖

我正在使用java配置@ComponentScan来初始化我的bean并@EnableAspectJAutoProxy(proxyTargetClass=true)使用cglib代理.

在这个项目中,我们使用了很多生成的服务@Autowired.它工作得很好.

但是,对于我添加的其中一些服务@Async(我也在@EnableAsync(proxyTargetClass = true)我的@Configuration课程中添加了).

在那之后,我得到:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'ConversationUserLocalService': Bean with name 'ConversationUserLocalService' has been injected into other beans [ConversationUserHistoryLocalService] i
n its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider …
Run Code Online (Sandbox Code Playgroud)

aop spring spring-java-config spring-aspects

25
推荐指数
3
解决办法
2万
查看次数

Spring AOP排除了一些类

我正在使用Spring AspectJ来记录方法执行统计信息,但是,我希望在不更改切入点表达式的情况下从中排除某些类和方法.

为了排除某些方法,我创建了一个用于过滤掉的自定义注释.但是我无法对课程做同样的事情.

这是我的方面定义 -

@Around("execution(* com.foo.bar.web.controller.*.*(..)) "
            + "&& !@annotation(com.foo.bar.util.NoLogging)")
public Object log(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    // logging logic here
}
Run Code Online (Sandbox Code Playgroud)

NoLogging 是我的自定义注释,用于排除方法.

那么如何在不更改切入点表达式且不添加新顾问程序的情况下过滤掉某些类?

spring spring-aop spring-aspects

14
推荐指数
1
解决办法
9520
查看次数

从ProceedingJoinPoint中检索参数值

在我的请求中,我有一个参数名称"accessToken",如何从ProceedingJoinPoint获取请求参数值?

public Object handleAccessToken(ProceedingJoinPoint joinPoint) throws Throwable { 
    final Signature signature = joinPoint.getStaticPart().getSignature();
    if (signature instanceof MethodSignature) {
        final MethodSignature ms = (MethodSignature) signature;
        String[] params = ms.getParameterNames();
        for (String param : params) {
            System.out.println(param);
            // here how do i get parameter value using param ?
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

通话方式:

public MyResponse saveUser(
    @RequestParam("accessToken") String accessToken,
    @RequestBody final UserDto userDto
) {
    // code 
}
Run Code Online (Sandbox Code Playgroud)

我想在AOP中获取此访问令牌.

提前致谢.

aop spring-aop spring-aspects

9
推荐指数
2
解决办法
2万
查看次数

获取Java中的所有异常并远程发送

我有一个庞大的Java应用程序。我想拦截所有Java异常并通过电子邮件发送它们。我不能在任何地方添加代码以通过发送代码,try-catch因此是否可以使用例如Aspect将异常拦截为低级类并获取异常内容?

还是有某种方法可以覆盖某些内部Java类并获取异常有效负载?

有什么可能?

java spring aspect spring-aspects

9
推荐指数
2
解决办法
386
查看次数

在Spring测试中没有调用Aspect

我正在使用Spring 4.16并且我有我的ValidationAspect,它验证方法参数并抛出ValidationException如果有问题.这是在我运行服务器并发送请求时调用,但不是在测试时调用:

package com.example.movies.domain.aspect;
...
@Aspect
public class ValidationAspect {

    private final Validator validator;

    public ValidationAspect(final Validator validator) {
        this.validator = validator;
    }

    @Pointcut("execution(* com.example.movies.domain.feature..*.*(..))")
    private void selectAllFeatureMethods() {
    }

    @Pointcut("bean(*Service)")
    private void selectAllServiceBeanMethods() {
    }

    @Before("selectAllFeatureMethods() && selectAllServiceBeanMethods()")
    public synchronized void validate(JoinPoint joinPoint) {
         // Validates method arguments which are annotated with @Valid
    }
}
Run Code Online (Sandbox Code Playgroud)

配置文件,我创建方面bean的方面

package com.example.movies.domain.config;
...
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AspectsConfiguration {

    @Bean
    @Description("Hibernate validator. Used to validate request's input")
    public Validator validator() { …
Run Code Online (Sandbox Code Playgroud)

java spring spring-test spring-aop spring-aspects

8
推荐指数
2
解决办法
6410
查看次数

@Validated时@Transactional不起作用

如果我@Validated向我的服务的接口/实现添加注释,那么该服务不再是事务性的.Stacktrace显示没有,TransactionInterceptor但我只看到MethodValidationInterceptor.如果我删除@Validated然后我看到TransactionInterceptorMethodValidationInterceptor消失当然.这些方面是否相互排斥?

@Service
//@Validated <- here is my problem :)
public interface AdminService {
  String test(String key, String result);
}

public class AdminServiceImpl implements AdminService, BeanNameAware, ApplicationContextAware {

  @Override
  @Transactional(transactionManager = "transactionManager")
  public String test(String key, String result) {

    return "hehe";
  }
}

@Configuration
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableTransactionManagement(order = AspectPrecedence.TRANSACTION_MANAGEMENT_ORDER)
public class AppConfiguration {..}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

validation aop spring transactions spring-aspects

8
推荐指数
1
解决办法
212
查看次数

弹簧测试@async方法

我正在尝试测试@AsyncSpring的注释是否在我的项目中按预期工作.但事实并非如此.

我有这个测试:

 @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = GlobalConfiguration.class)
    public class ActivityMessageListenerTest {

    @Autowired
    private ActivityMessageListener activityMessageListener;

    private Long USER_ID = 1l;
    private Long COMPANY_ID = 2l;
    private Date DATE = new Date(10000000);
    private String CLASSNAME = "className";
    private Long CLASSPK = 14l;
    private Integer TYPE = 22;
    private String EXTRA_DATA = "extra";
    private Long RECIVED_USER_ID = 99l;

    @Before
    public void setup() throws Exception {
    }

    @Test
    public void testDoReceiveWithException() throws Exception {
        System.out.println("Current thread " +      Thread.currentThread().getName());
        Map<String, Object> values …
Run Code Online (Sandbox Code Playgroud)

spring spring-aop spring-java-config spring-aspects

7
推荐指数
1
解决办法
3692
查看次数

使用AspectJ LTW允许Spring-proxy功能自我调用非公共方法和相关注意事项

我已经看到了相关的春天功能的例子不胜枚举@Cacheable,@Transactional,@Async其中相同的选项重申每次等:

  1. 通过代理对象进行的自我调用通过或者ApplicationContext.getBean(MyService.class)自动装配的MyService.class代理对象获得@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS),

  2. 将目标方法重定位到单独的@Service类,

  3. 使用AspectJ加载时编织.

虽然前两种方法通常都很好,但有时我们需要将上述三种(和其他)注释的功能附加到私有方法,无论是出于代码清晰度,设计还是其他原因.


前两种方法有很多例子,但最后一种方法很少.据我所知,由于AspectJ LTW的性质,默认情况下它与通常的Spring AOP行为相互排斥,可以@Cacheable轻松实现等行为.我的问题如下:

  1. 在使用AspectJ LTW时,通常使用前两个选项来启用上述行为是否有任何不错的示例?

  2. 有没有一种方法,使的AspectJ LTW选择性,如不@Async@Transactional,但只@Cacheable?一个示例用例可能是:由于团队的设计,所有可缓存的方法(其中一些可能是私有的)应该位于外观类中,该类调用执行繁重计算的私有方法,但可能更新某些状态(即'last-queried-at: #')在返回外部之前调用.

这个问题来自spring-boot用户的观点,但我认为它通常适用于Spring AOP和AspectJ LTW.如果在这种情况下需要特殊考虑,请纠正我.

spring aspectj spring-aop spring-boot spring-aspects

6
推荐指数
1
解决办法
527
查看次数

如何使用 Spring AOP 或 AspectJ 拦截给定方法中的每个方法调用

 class Test {

@override
public String a(){
b();
d();
}


private String b() {
c();
}

private String c(){
d();
}
private String d(){}

}
Run Code Online (Sandbox Code Playgroud)

我想拦截从重写方法 A() 调用的 Test 类的每个方法,并想知道 b()、c() 等每个方法在单独处理某些业务逻辑时花费了多少时间。

如何使用 Spring AOP 或 Aspectj 实现它?

java aspectj spring-aop spring-aspects

6
推荐指数
1
解决办法
1万
查看次数

spring aop - 链接多个方面

我有两个方面,一个用于获取@Around 调用的锁,另一个用于消除方法调用。这些方面看起来像这样:

@Pointcut("execution(public * * (..))")  
private void anyPublicMethod() {}

@Around("anyPublicMethod() && @annotation(lock)")
public Object all(ProceedingJoinPoint proceedingJoinPoint, Lock lock) throws Throwable {
   // acquire lock, then proceed()
}
Run Code Online (Sandbox Code Playgroud)

另一个看起来像这样:

@Pointcut("execution(public * * (..))")  
private void anyPublicMethod() {}

@Around("anyPublicMethod() && @annotation(debounce)")
public Object all(ProceedingJoinPoint proceedingJoinPoint, Debounce debounce) throws Throwable {
    // debouncing as described in 
    // http://stackoverflow.com/questions/4742210/implementing-debounce-in-java

}
Run Code Online (Sandbox Code Playgroud)

完整代码:

https://github.com/rmalchow/debouncer-aspect/blob/master/src/main/java/com/skjlls/aspects/debounce/impl/DebounceAspect.java

https://github.com/rmalchow/lock-aspect/blob/master/src/main/java/com/skjlls/aspects/lock/impl/LockAspect.java

当我将 @Debounce 和 @Lock 都放在一个方法上时,我得到了这个异常:

Required to bind 2 arguments, but only bound 1 (JoinPointMatch was NOT bound in …
Run Code Online (Sandbox Code Playgroud)

java aop spring spring-aop spring-aspects

5
推荐指数
1
解决办法
2294
查看次数