Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional @PostConstruct annotation

I sometimes may not need to post-construct of a bean at startup according to values passed via JVM argument.

I tried @Conditional annotation but it only works on @Bean annotations.

Have you tried/needed such a thing before?

like image 299
jit Avatar asked Aug 16 '26 14:08

jit


1 Answers

AFAIK Conditional can not be mix with postconstruct. So my suggetions

  1. Add two diferent beans one with postconstruct and one without as use @Conditional in beans declaration

  2. Add programatic code validatiing your application args like:

    import org.springframework.core.env.Environment;
    
    private final Environment env;
    
    public void execute() {
        if (env.getProperty("condition") == "condition") {
        }
    }
    
  3. Use aproximation in 1 but @Profile and pass active profile as parameter -Dspring.profiles.active=my_profile

like image 68
Juan Rada Avatar answered Aug 20 '26 02:08

Juan Rada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!