heldt
heldt

Reputation: 4266

No qualifying bean of type when using @ConditionalOnProperty

I have a component that I what to be able to enable/disable from the applikation.properties in my spring boot application..

In my application.properties I have

wiki.enabled=false

And in component looks like this

@Component
@ConditionalOnProperty("wiki.enabled")
public class WikiClient {
...
}

And finally in my other class where I use the wikiclient I have autowired it like this in my constructor.

    @Autowired(required = false)
    public MigrationManager(UserService userService, WikiClient wikiClient) {
    ...
    }

Still I get the exception

No qualifying bean of type com.test.WikiClient

If I enable the property it works like if I enabled the component.

Upvotes: 1

Views: 2167

Answers (2)

Vaibhav Ranglani
Vaibhav Ranglani

Reputation: 213

In your main springboot application class add Annotation @ComponentScan and make sure the package in which WikiClient is defined is included in the ComponentScan.

Upvotes: 0

heldt
heldt

Reputation: 4266

I solved it by moving the the wikiClient argument to a property and used @Autowired(required = false) instead of having it in the constructor. Like M. Deinum said in the comments. Optional dependencies should not be in the constructor.

Upvotes: 2

Related Questions