I need to read my db to load a custom setting inside my Spring @Configuration class.
I have something like:
@Configuration
public MyConfigClass implements ApplicationContextAware{
@Bean(initMethod = "start", destroyMethod = "stop")
public ServerSession serverSession() throws Exception {
ServerSession serverSession = new ServerSession(urlGateway, useSsl, hostGateway, portGateway);
return serverSession;
}
I should read parameters from DB instead from property file. I know that I can't @Inject my repository directly into this class, but there is a trick or something that permit me to do this or at least make a query on db?
I'm using Hibernate + Spring + Spring Data.
I prefer injecting the necessary dependencies as a parameter. Using @Autowired
in a field looks unnatural to me in a @Configuration
class (just using stateful fields, as configuration is supposed to be stateless). Just provide it as a parameter for the bean method:
@Bean(initMethod = "start", destroyMethod = "stop")
public ServerSession serverSession(MyRepo repo) throws Exception {
repo.loadSomeValues();
ServerSession serverSession = new ServerSession(urlGateway, useSsl, hostGateway, portGateway);
return serverSession;
}
This might require using @Autowired
itself at method level, depending on the Spring version:
@Bean(initMethod = "start", destroyMethod = "stop")
@Autowired
public ServerSession serverSession(MyRepo repo) throws Exception {
repo.loadSomeValues();
ServerSession serverSession = new ServerSession(urlGateway, useSsl, hostGateway, portGateway);
return serverSession;
}
See also:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments