1

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.

heldt
  • 4,166
  • 7
  • 39
  • 67
  • Well that is what you told it do to with `@ConditionalOnProperty`... It check if `wiki.enabled` is there, with a certain value (default empty) which if not explicitly defined must not be `false`. (As explained in the javadoc of [@ConditionalOnProperty](http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.html). – M. Deinum Sep 30 '16 at 06:50
  • But should not @Autowired(required = false) ignore that the bean does not exists and set it to null? – heldt Sep 30 '16 at 07:09
  • 2
    Not sure... If that actually works on a constructor. Also I wouldn't put optional dependencies in the constructor but as properties instead. – M. Deinum Sep 30 '16 at 07:17

2 Answers2

2

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.

heldt
  • 4,166
  • 7
  • 39
  • 67
  • You can also make the constructor parameter an `Optional`, see https://stackoverflow.com/questions/42135102/how-to-set-autowired-constructor-params-as-required-false-individually – MikaelF Apr 20 '22 at 15:52
0

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

Vaibhav Ranglani
  • 215
  • 1
  • 2
  • 14