0

I've got a @RestController like below:

@RestController
public class SomeOperations {
    private RepositoryFailover repo;
    public SomeOperations (RepositoryFailover repo) {
        this.repo = repo;
    }
}

The class RepositoryFailover in above case resides in an external package and a bean for it is created there as shown below inside the config class FailoverConfiguration :

@Configuration
public class FailoverConfiguration {
    @Bean
    public RepositoryFailover repoFailover(ClassA abc) {
        return new RepositoryFailover(abc);
    }
}

When I run my project I get the error:

No qualifying bean of type RepositoryFailover available

That is mostly because im not importing this bean from external jar properly.

How do i import this bean that is created in the config class externally?

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
Plainsage
  • 116
  • 1
  • 2
  • 11
  • Add the package to scan to your `@SpringBootApplication` class (the `basePackages` attribute) or add an `@Import(FailoverConfiguration.class)` on the class. Either will include the config and thus create the bean. – M. Deinum Jul 14 '22 at 12:39

2 Answers2

0

You need to discover this bean either by using @ComponentScan

@ComponentScan("path.to.FailoverConfiguration.package")
@Configuration
class SomeConfig { ... }

or by using @Import

@Import(FailoverConfiguration.class)
@Configuration
class SomeConfig { ... }
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
-1

So, the applicationContext of your application is different from the applicationContext of the jar beans. So, the bean in your applicationContext is not able to find the bean inside the jar's applicationContext (If at all it is getting created there)

One way I can think of is to import the jar's spring configuration into your spring configuration class using @Import(Configuration.class) annotation.

After this, those beans will be available in your application's spring context which you can inject into the needed class as needed.

https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch04s03.html

Amol Limaye
  • 108
  • 1
  • 4
  • 1
    Not sure why it got downvoted but 1. there is only one context in a Spring Boot application (normally), 2 you are linking to ancient documentation on Spring Javaconfig a project that doesn't exist anymore and has been merged in Spring itself. – M. Deinum Jul 14 '22 at 13:20