1, the environment and profile
Example: Database Configuration
a: By @Bean notes, create a data source through EmbeddedDatabaseBuilder
@Bean(destroyMethod="shutdown") public DataSource dataSource(){ return new EmbeddedDatabaseBuilder() .addScript("classpath:schema.sql") .addScript("classpath:test-data.sql") .build(); }
This creates a javax.sql.DataSource type of bean, the bean is how to create it is the most interesting. Hypersonic database using EmbeddedDatabaseBuilder will build an embedded, its schema (schema) defined in schema.sql, the test data is loaded through the test-data.sql.
b: created by JndiObjectFactoryBean
@Bean public DataSource dataSource(){ JndiObjectFactoryBean jndiObjectFactoryBean = new JndiOjbectFactoryBean(); jndiObjectFactoryBean.setJndiName("jdbc/myDS"); jndiObjectFactoryBean.setResourceRef(true); jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class); return (DataSource) jndiObjectFactoryBean.getObject(); }
By acquiring JNDI DataSource allows container decide how to create this DataSource, and even switched to container-managed connection pool.
c: BasicDataSource database configuration
@Bean(destroyMethod="close") public DataSource dataSource(){ BasicDataSource dataSource = new BasicDataSource(); dataSource.setUrl("jdbc:h2:tcp://dbserver/~/test"); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUsername("sa"); dataSource.setPassword("password"); dataSource.setInitialSize(20); dataSource.setMaxActive(30); return dataSource; }
Three or more different javax.sql.DataSource generated bean methods. During the build phase (you might use the Maven profiles) to determine which configuration you want to compile the application to be deployed.
Introduction Profile
@Configuration public class DataSourceConfig{ @Bean(destroyMethod="shutdown") @Profile("dev") public DataSource embeddedDataSource() { return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.H2) .addScript("classpath:schema.sql") .addScript("classpath:test-data-sql") .build(); } @Bean @Profile("prod") public DataSource jndiDataSource(){ JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName("jdbc/myDS"); jndiObjectFactoryBean.setResourceRef(true); jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class); return (DataSource) jndiObjectFactoryBean.getObject(); } }
By @Profile above comments, the definition of bean create different under different circumstances. Which specific bean is created, determined in accordance with the activated profile.
Activating Profile
Example:
web.xml
<context-param> <param-name>spring.profiles.default<param-name> <param-value>dev</param-value> <context-param> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet <servlet-class> <init-param> <param-name>spring.profile.default</param-name> <param-value>dev</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
Use profile tests
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={PersistenceTestConfig.class}) @ActiveProfiles("dev") public class PersistenceTest{ ............. }
Creation bean parameters @ActiveProfiles annotation to specify under what circumstances