gpt4 book ai didi

java - 实现基于注解的Spring AspectJ (JavaConfig)

转载 作者:行者123 更新时间:2023-11-30 11:09:26 26 4
gpt4 key购买 nike

我们正在尝试将 AspectJ @Aspect 实现到我们现有的软件中,以便在进行服务调用后执行一些代码。

注意:

  • 我们的服务接口(interface)和实现是@Autowired通过其余 Controller 以及其他服务实现贯穿整个项目。
  • 这个项目完全是一个没有任何 XML 的 Java 配置。
  • 我们正在使用部署在 Tomcat 7.0.54 上的 Spring 4.1.2 RELEASE。

问题:

当我们将 @EnableAspectJAutoProxy 添加到我们的主 @JavaConfig 中时,我们遇到以下异常:

Unresolvable circular reference.

在一长串 bean 上每次 @Autowired 尝试都会失败。

尝试过:

  • 删除了 @EnableAspectJAutoProxy 注释,该注释正确地 Autowiring 所有内容,但我们的 @Aspect 永远不会被调用。
  • 通过声明在注释中添加了 CGLIB 支持proxytargetclass=true 无济于事。
  • 我们尝试直接从 Spring 中遵循此文档:@EnableAspectJAutoProxy Javadoc

这似乎是 AspectJ 处理 Autowiring 依赖项的代理机制的问题。

为什么当我们添加 @EnableAspectJAutoProxy 时会发生这种情况?

我们的 Java 配置:

@Configuration
@EnableWebMvc
@EnableJpaRepositories(basePackages ={"com.company.product.persistence.repository"})
@EnableTransactionManagement
@EnableSwagger
@EnableAspectJAutoProxy
@PropertySource({"classpath:hibernate.properties",
"classpath:auth.properties",
"classpath:mail.properties",
"classpath:locations.properties"
})
@ComponentScan(basePackages = {"com.company.product"})
public class WebConfig extends WebMvcConfigurerAdapter {
//Bean declarations here.
//Note: All services/repos/controllers are annotation based.
}

切面实现:

@Aspect
@Component
public class PostMessageAspect {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@After("execution(*com.company.product.persistence.serviceImpl.event.eventServiceImpl.methodCall(..))")
public void postMessageRun(final JoinPoint joinPoint) {
logger.info("CALLED AFTER METHOD");
}
}

更新:

设法让 AOP/AspectJ 在一台开发机器上完美运行,只需要对我们的 Spring Security 配置进行微小的更改。我们都在运行 Tomcat 7.0.56 默认实例的 Ubuntu 14.0.4 上使用 Intellij,openJDK 1.7.0_65。在运行相同软件栈的另一台机器上,得到以下信息。

堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatchingMessageController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.apx.efm.persistence.service.event.DispatchingEventService com.apx.efm.controllers.message.DispatchingMessageController.dispatchingEventService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatchingEventServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.apx.efm.persistence.service.building.BuildingAd dressesService com.apx.efm.persistence.serviceImpl.event.DispatchingEventServiceImpl.buildingAddressesService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'buildingAddressServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.apx.efm.persistence.service.building.BuildingService com.apx.efm.persistence.serviceImpl.building.BuildingAddressServiceImpl.buildingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'buildingServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.apx.efm.persistence.service.building.BuildingAddressesService com.apx.efm.persistence.serviceImpl.building.BuildingServiceImpl.buildingAddressesService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodSecurityInterceptor' defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.aopalliance.intercept.MethodInterceptor]: Factory method 'methodSecurityInterceptor' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.apx.efm.persistence.service.user.EfmUserService com.apx.efm.application.config.SecurityConfig.efmUserService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'efmUserServiceImpl' defined in file [/home/apxdev4/Development/GitRepositories/efim-restful-web-service/target/EFIM/WEB-INF/classes/com/apx/efm/persistence/serviceImpl/user/EfmUserServiceImpl.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'methodSecurityInterceptor': Requested bean is currently in creation: Is there an unresolvable circular reference?

最佳答案

这完全是我们 Spring 配置的问题。我们的 Spring Security 配置正在尝试 @Autowired bean,而它们仍在由我们的主要应用程序配置处理的过程中。我们通过确保在主 @Configuration 之后配置 Spring Security 来解决这个问题。

@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
// Initialize web mvc
appContext.setDisplayName("APP");
appContext.register(WebConfig.class);
appContext.register(SecurityConfig.class);

// Rest omitted (listeners, dispatcher servlet, etc.)
}

关于java - 实现基于注解的Spring AspectJ (JavaConfig),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28159323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com