spring boot project define dependency class as spring bean

I declared a service in package service like:

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 is a dependency in my spring boot project declared in my project's pom.xml in the package a.b.c.d.soapapim

in my 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);
    }

}

I also have a scheduler:

@Component
public class Scheduler {

    private MyService myService;

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

I am getting the following error:

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

I can't annotate that class because its a dependency I don't have access to. I just declare it as a dependency on the pom.xml of my project.

How can I get this bean into the context of my spring boot application?

Upvotes: 1

Views: 174

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40088

You can declare it as spring bean using @Bean annotation in config class

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

Upvotes: 1

Related Questions