I have jdbc property files which I take from external configuration web-service In spring boot in order to set mysql props it\'s easy as adding those to application.properti
If you need do to this for testing purposes: since spring-test 5.2.5 you can use @DynamicPropertySource
:
@DynamicPropertySource
static void setDynamicProperties(DynamicPropertyRegistry registry) {
registry.add("some.property", () -> some.way().of(supplying).a(value) );
}
Takes precedence over pretty much all of the other ways of supplying properties. The method must be static though.
This is how you can set properties during startup if you are running spring boot application.
The easiest way is to set the properties before you even started an app.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
ConfigurableEnvironment env = new ConfigurableEnvironment();
env.setActiveProfiles("whatever");
Properties properties = new Properties();
properties.put("server.port", 9999);
env.getPropertySources()
.addFirst(new PropertiesPropertySource("initProps", properties));
application.setEnvironment(env);
application.run(args);
}
}
With this Method in your configuration you can set default properties.
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class)
.properties("propertyKey=propertyValue");
}
Under META-INF folder create exactly this folders and file: spring>batch>override>data-source-context.xml and in your xml file make sure to override the paramters you want like this:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${loader.jdbc.driver}" />
<property name="url" value="${loader.jdbc.url}" />
<property name="username" value="${loader.jdbc.username}" />
<property name="password" value="${loader.jdbc.password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
or use a jndi like this in the xml file to access your external configuration file like catalina.properties
<jee:jndi-lookup id="dataSource"
jndi-name="java:comp/env/jdbc/loader-batch-dataSource" lookup-on-startup="true"
resource-ref="true" cache="true" />