conf/db.properties
# jdbc.X
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://localhost/mytestdb
jdbc.user=SA
jdbc.pass=
# hibernate.X
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=
#1. Register a Properties File in Application Context XML
The “<property-placeholder>” tag.
1 | <context:property-placeholder location="classpath:conf/db.properties" /> |
OR
1 2 3 4 5 6 7 8 | <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:conf/db.properties</value> </list> </property> <property name="ignoreUnresolvablePlaceholders" value="true"/> </bean> |
#2. Register a Properties File via Java configuration
The @PropertySource annotation.
1 2 3 4 5 6 7 8 9 | @Configuration @PropertySource("classpath:conf/db.properties") public class MyAppConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } |
OR
1 2 3 4 5 6 7 8 | @Bean public static PropertySourcesPlaceholderConfigurer properties(){ PropertySourcesPlaceholderConfigurer propSourcePlaceHolder = new PropertySourcesPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[ ] { new ClassPathResource( "conf/db.properties" ) }; propSourcePlaceHolder.setLocations( resources ); propSourcePlaceHolder.setIgnoreUnresolvablePlaceholders( true ); return propSourcePlaceHolder; } |
#3.
Injecting the properties via annotations1 2 3 4 5 6 7 8 9 | @Value("${jdbc.url:}") private String jdbcUrl; @Value("${jdbc.driverClassName:}") private String jdbcDriverClassName; |
#4. Accessing via the Environment API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | @Configuration @Import(MyAppCommonConfiguration.class) @EnableTransactionManagement @ComponentScan("com.myapp") @PropertySource("classpath:conf/db.properties") @EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactory", basePackages = { "com.myapp.domain.repository" }, transactionManagerRef = "transactionManager") @EnableJpaAuditing public class MyAppServiceConfiguration { private static final Logger LOG = LoggerFactory.getLogger(MyAppServiceConfiguration.class); @Inject private Environment environment; @Bean DataSource dataSource() { final JndiDataSourceLookup jndiLookup = new JndiDataSourceLookup(); final String dataSourceJndi = environment.getRequiredProperty("myapp.datasource.jndi"); return jndiLookup.getDataSource(dataSourceJndi); } /** * Creates the persistence properties bean. */ @Bean public Properties myDbDomainProperties() { final Properties properties = new Properties(); properties.put(DIALECT, environment.getRequiredProperty(DIALECT)); properties.put(ORDER_UPDATES, environment.getRequiredProperty(ORDER_UPDATES)); properties.put(USE_SECOND_LEVEL_CACHE, environment.getRequiredProperty(USE_SECOND_LEVEL_CACHE)); properties.put(USE_QUERY_CACHE, environment.getRequiredProperty(USE_QUERY_CACHE)); properties.put(USE_STRUCTURED_CACHE, environment.getRequiredProperty(USE_STRUCTURED_CACHE)); return properties; } @Bean public List<String> domainPackagesToScan() { return ImmutableList.of("com.myapp.domain.entity"); } } |
#5. Using the custom ApplicationContextInitializer or WebApplicationInitializer
Spring 3.1 introduced a very convenient hook for customizing the application context with ApplicationContextInitializer. It can be used to set active profiles and register custom property sources. Spring loading properties files with ApplicationContextInitializer or with WebApplicationInitializer.
The “WebApplicationInitializer” is applicable in a Servlet 3.0+ compliant servlet container and provides a hook to pro-grammatically configure the servlet context.