高级装配用来适应开发和生产 不同环境下的软切换
一、环境与profile
1.开发环境下的profile
package com.bonc.config;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
/*
* 一、注解开发profile
* @Profile作用在了类上,告诉Spring这个配置类中的bean只有在dev profile激活时才创建
* dev profile没有被激活的话,@Bean的注解都会被忽略掉。
*
* Spring3.1中,只能在类级别上使用@Profile注解,在Spring3.2开始,
* 你也可以在方法级别上使用@Profile注解,与@Bean注解一同使用,这样的话就能将两个bean放置到同一个配置类中。
*
* 尽管只有当规定的profile被激活时,相应的bean才会被创建,但是可能会有其他的bean并没有声明在一个给定的profile中。
* 没有指定profile的bean始终都会被创建,与激活哪个profile没有关系。
*
*
* */
@Configuration
@Profile("dev")
public class DevelopmentProfileConfig {
@Bean(destroyMethod="shutdown")
public DataSource dataSource(){
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema.sql")
.addScript("classpath:test.sql").build();
}
}
二、生产环境下profile
package com.bonc.config;
import javax.sql.DataSource;
import o