gpt4 book ai didi

java - spring boot项目将依赖类定义为spring bean

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

我在包服务中声明了一个服务,例如:

import a.b.c.d.soapapim.SoapApimMiddlewareService;

@Service
@EnableDefaultExceptionHandling
public class MyService{
private final SoapApimMiddlewareService myService;

public MyService(SoapApimMiddlewareService myService){
this.myService = myService;
}

public Response pullEvents(numEvents){
myService.pullEvents(numEvents);
}

}

SoapApimMiddlewareService 是我的 spring boot 项目中的依赖项,在 a.b.c.d.soapapim 包中的项目 pom.xml 中声明

在我的application.java中:

@ComponentScan({"a.b.c.d.soapapim", "service", "scheduler"})
SpringBootApplication
@EnableScheduling
public class Application implements WebMvcConfigurer {´

public static void main(String[] args){
SpringApplication app = new SpringApplication(Application.class);
app.setRegisterShutdownHook(false);
app.run(args);
}

}

我还有一个调度程序:

@Component
public class Scheduler {

private MyService myService;

@Scheduled(fixedRate = 1200)
public void myScheduledTask(){
myService.pullEvents(1);
}
}

我收到以下错误:

UnsatisfiedDependencyException: Error creating bean with name MyService No qualifying bean of type 'a.b.c.d.soapapim.SoapApimMiddlewareService'

我无法注释该类,因为它是我无权访问的依赖项。我只是将其声明为对我的项目的 pom.xml 的依赖项。

如何将此 bean 放入我的 Spring Boot 应用程序的上下文中?

最佳答案

您可以在配置类中使用@Bean注释将其声明为spring bean

@Bean
public SoapApimMiddlewareService soapApimMiddlewareService() {
return new SoapApimMiddlewareService():
}

关于java - spring boot项目将依赖类定义为spring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58365755/

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