How to Autowire conditionally in spring boot?


I have created one scheduler class

public class TestSchedulderNew { @Scheduled(fixedDelay = 3000) public void fixedRateJob1() { System.out.println("Job 1 running"); } @Scheduled(fixedDelay = 3000) public void fixedRateJob2() { System.out.println("Job 2 running"); } }

In configuration i have put @ConditionalOnProperty annotation to enable this on conditional purpose.

@Bean @ConditionalOnProperty(value = "jobs.enabled") public TestSchedulderNew testSchedulderNew() { return new TestSchedulderNew(); }

Now in controller, i have created "stopScheduler" method to stop those scheduler , in this controller i have autowired TestSchedulderNew class

@RestController @RequestMapping("/api") public class TestCont { private static final String SCHEDULED_TASKS = "testSchedulderNew"; @Autowired private ScheduledAnnotationBeanPostProcessor postProcessor; /] @Autowired private TestSchedulderNew testSchedulderNew; @GetMapping(value = "/stopScheduler") public String stopSchedule(){ postProcessor.postProcessBeforeDestruction(testSchedulderNew, SCHEDULED_TASKS); return "OK"; } }

Now the problem is if conditional property is false then i get below exception

Field testSchedulderNew in com.sbill.app.web.rest.TestCont required a bean of type 'com.sbill.app.schedulerJob.TestSchedulderNew

In case of true everything works fine,

Do we have any option to solve this ?


You can use @Autowired(required=false) and null check in stopScheduler method.

@Autowired(required=false) private TestSchedulderNew testSchedulderNew; @GetMapping(value = "/stopScheduler") public String stopSchedule() { if (testSchedulderNew != null) { postProcessor.postProcessBeforeDestruction(testSchedulderNew, SCHEDULED_TASKS); return "OK"; } return "NOT_OK"; }



  • How can I import a deep hierarchy of merged dictionaries with 1 ResourceDictionary?
  • Javascript hasOwnProperty always false on Event objects?
  • Free implementation in scalaz
  • Standard behavior for direct initialization of unsigned short
  • Take picture automatically with no user interaction [closed]
  • How to make a Label appear then disappear after a certain amount of time in python tkinter
  • Java Generics - When to use “T” and “?” [closed]
  • Can't install profiler symfony flex
  • How to set current cell on SAP GUIContainerShell in C#?
  • How to align elements in cells of a css grid?
  • Fetch first location on android and stop updates
  • /.git/hooks/: No such file or directory protocol error: expected control record on Mac osx
  • How to indent if…any…do…end…end [closed]
  • Android upgrade db not updating db version after successful upgrade
  • Getting public URL to file in Google Drive using .NET API
  • Html.BeginForm inside of Html.BeginForm MVC3
  • R Reference Class multiple inheritance: how to call method in a specific parent class?
  • Proper Form of API request to Blogger using Java/App Engine -error 401
  • How to send the client id and secret id of OAuth2 using Angular 2?
  • “RepeatForUnit” item missing in Calendar entry?
  • Ruby on Rails - Settting up Reviews functionality
  • how to change the black color to Red with opencv python
  • how to reload page after uploading images
  • Pyinstaller GLIBC_2.15 not found
  • iOS: Address Book
  • Passing HTTP Post from AWS API GW to Lambda
  • Align microsoft access queries
  • create circular Auto Horizontal Scroll View?
  • Allowing audio files in Spring MVC 3.0?
  • Visual Studio 2017 Professional- Unable to find package at source
  • How to split wav file into two or more parts using c#
  • Another “Cannot make static reference…” Question
  • matrix multiplication apache pig
  • Angular 4: Responsive Grid List
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • JavaScript RegExp Replace
  • How to check if object is null in Java?