pursuit
pursuit

Reputation: 1

How to use the configuration of database to conditionally load beans In SpringBoot?

My SpringBoot version is 2.3.4, database is Mysql 5.7.35

It's we all known that SpringBoot can use @ConditionalOnProperty to conditionally load beans. Can we move the parameters from the configuration file to the database configuration to implement dynamic loading of the beans through the database's configuration table

I tried to implement Condition, ApplicationContextInitializer interface or custom BeanDefinitionRegistryPostProcessor but were unable to solve the problem of the process can't query to the database.

So I ask you if there is a feasible method, now I am more about this problem to understand the springboot loading mechanism.

the example code

@Slf4j
@Component
@ConditionalOnProperty(prefix = "type", name = "class-a", havingValue = "enable")
public class ClassA {

    @PostConstruct
    public void init() {
        System.out.println("do something, for example, a scheduled task a");
    }
}
@Slf4j
@Component
@ConditionalOnProperty(prefix = "type", name = "class-b", havingValue = "enable")
public class ClassB {

    @PostConstruct
    public void init() {
        System.out.println("do something, for example, a scheduled task b");
    }

}

At present, I can completely implement the requirements through the configuration file.

However, I am considering a way to implement this requirement through database configuration, but before Bean initialization, I can not get the data table information through jdbc or other tools, which is the problem.

Upvotes: 0

Views: 54

Answers (0)

Related Questions