(A) spring Advanced Assembly - @ Profile

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.

Question three way above that for each type of environment you want to re-build applications.

Introduction Profile

In Java configuration, you can use @Profile annotation to specify which profile belongs to a bean. For example, a plurality of profile in different environments, the bean will be assembled, it is determined to be activated according to that 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

Spring in determining which profile is active, need to rely on two separate attributes: spring.profiles.active and spring.profiles.default.
 
There are several ways to set these two attributes:
  As the initialization parameter of DispatcherServlet;
  As the Web application context parameter;
  As JNDI entries;
  As an environment variable;
  As the JVM system properties;
  In the integrated test class, using @ActiveProfiles annotation setting.

Example:

  Use DispatcherServlet parameters will spring.profiles.default development environment to profile

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>

  

In spring.profiles.active and spring.profiles.default in, profile used is the plural form. This means you can simultaneously activate multiple profile, which can list multiple profile names, separated by commas to achieve.

Use profile tests

Spring provides @ActiveProfiles notes, which we can use it to specify a profile to be activated during the test run.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={PersistenceTestConfig.class})
@ActiveProfiles("dev")
public class PersistenceTest{
.............
}

  Creation bean parameters @ActiveProfiles annotation to specify under what circumstances

 

Guess you like

Origin www.cnblogs.com/lin-bunny/p/11496223.html