gpt4 book ai didi

java - 如何将Spring-boot服务注入(inject)aspectj类?

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

我遇到了问题..我正在创建一个aspectj类,并且在我的类中我需要访问spring-boot服务,但是当我尝试使用@Autowired或通过构造函数注入(inject)它时,我遇到了错误:

“无法 Autowiring 。未找到“UserService”类型的 bean”

这是我的类(class):

package com.ingressolive.bar.aop.interceptor;


@Aspect
@Configuration
public class TenantAspect {
private final Logger log = LoggerFactory.getLogger(this.getClass());


private final Environment env;

@Autowired
private UserService userService;


public TenantAspect(Environment env) {
this.env = env;

}

@Before("execution(* com.ingressolive.bar.service.*.*(..))")
public void aroundExecution(JoinPoint pjp) {
log.debug("##################################### Entered here !!!!!!!!!!!!!!!!!!!!!!!!!!");

}
}

有人可以帮助我吗?

最佳答案

您可以尝试使用组件而不是配置吗?我正在使用这样的方面,并且 Autowiring 工作得很好。

package com.ingressolive.bar.aop.interceptor;

@Aspect
@Component
public class TenantAspect {
...
}

也许您必须寻找其他配置问题,例如配置文件,未加载 xml 配置?如果您的 bean 有任何 xml 配置,请考虑使用以下模式:

package com.yourpackage.config;

@Configuration
@ImportResource(
locations = {
"classpath:/your-extra-config-1.xml",
"classpath:/your-extra-config-2.xml",
})
public class AppConfig {
...
}

关于java - 如何将Spring-boot服务注入(inject)aspectj类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57227496/

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