How to Autowire conditionally in spring boot?

user1731485

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 ?

shazin

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";
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Autowire conditionally in spring boot?

From Dev

How to autowire default XmlMapper in Spring Boot application

From Dev

How to autowire dependency within a Spring Boot Converter?

From Java

Conditionally autowire Spring Data Repositories

From Dev

Conditionally autowire Spring Data Repositories

From Dev

How to Autowire repository interface from a different package using Spring Boot?

From Dev

How to Autowire repository interface from a different package using Spring Boot?

From Dev

Could not autowire SessionRegistry in spring boot

From Dev

Spring Boot could not autowire and run

From Dev

Spring Boot autowire dependency in TestExecutionListeners

From Dev

Autowire not working in Spring boot Forms

From Dev

Spring Boot @EnableScheduling conditionally

From Dev

Spring Boot @EnableScheduling conditionally

From Java

How to conditionally make a spring boot application terminate at start-up

From Dev

How to conditionally make a spring boot application terminate at start-up

From Java

Could not autowire field:RestTemplate in Spring boot application

From Dev

Can't @Autowire repository interface Spring Boot

From Dev

could not autowire field spring-boot

From Dev

Spring Boot can't autowire @ConfigurationProperties

From Dev

Could not autowire authentication manager in Spring Boot 2.0.0

From Dev

JUnit Spring Boot Application - Could not autowire fields

From Dev

Failed to Autowire beans in a Java thread in Spring Boot

From Dev

How to autowire by name in Spring with annotations?

From Dev

how to work with autowire concept in spring?

From Dev

spring boot how to autowire bean using yaml file in src/test/resource/

From Dev

Spring Boot, @Autowire into an unmanaged class using @Configurable and load time weaving

From Dev

Autowire doesn't work for custom UserDetailsService in Spring Boot

From Dev

spring boot: unable to autowire bean, but the bean is defined for sure

From Dev

Can't Autowire JpaRepository in Junit test - Spring boot application

Related Related

  1. 1

    How to Autowire conditionally in spring boot?

  2. 2

    How to autowire default XmlMapper in Spring Boot application

  3. 3

    How to autowire dependency within a Spring Boot Converter?

  4. 4

    Conditionally autowire Spring Data Repositories

  5. 5

    Conditionally autowire Spring Data Repositories

  6. 6

    How to Autowire repository interface from a different package using Spring Boot?

  7. 7

    How to Autowire repository interface from a different package using Spring Boot?

  8. 8

    Could not autowire SessionRegistry in spring boot

  9. 9

    Spring Boot could not autowire and run

  10. 10

    Spring Boot autowire dependency in TestExecutionListeners

  11. 11

    Autowire not working in Spring boot Forms

  12. 12

    Spring Boot @EnableScheduling conditionally

  13. 13

    Spring Boot @EnableScheduling conditionally

  14. 14

    How to conditionally make a spring boot application terminate at start-up

  15. 15

    How to conditionally make a spring boot application terminate at start-up

  16. 16

    Could not autowire field:RestTemplate in Spring boot application

  17. 17

    Can't @Autowire repository interface Spring Boot

  18. 18

    could not autowire field spring-boot

  19. 19

    Spring Boot can't autowire @ConfigurationProperties

  20. 20

    Could not autowire authentication manager in Spring Boot 2.0.0

  21. 21

    JUnit Spring Boot Application - Could not autowire fields

  22. 22

    Failed to Autowire beans in a Java thread in Spring Boot

  23. 23

    How to autowire by name in Spring with annotations?

  24. 24

    how to work with autowire concept in spring?

  25. 25

    spring boot how to autowire bean using yaml file in src/test/resource/

  26. 26

    Spring Boot, @Autowire into an unmanaged class using @Configurable and load time weaving

  27. 27

    Autowire doesn't work for custom UserDetailsService in Spring Boot

  28. 28

    spring boot: unable to autowire bean, but the bean is defined for sure

  29. 29

    Can't Autowire JpaRepository in Junit test - Spring boot application

HotTag

Archive