【Spring In Action】Section 3 高级装配

来源:互联网 发布:万方数据库 编辑:程序博客网 时间:2024/06/05 21:06
  • Spring Profile
  • 条件化的bean声明
  • 自动装配与歧义性
  • bean的作用域
  • Spring的语言表达式

一、环境与Profile
为了区分环境,引入了profile bean;
首先定义一个类:
@Configuration相当于XML中的Beans,@Bean相当于XML中的Bean
@Configuration@Profile("dev")public class DevelopmentProfileConfig{        @Bean    public DataSource dataSource(){
return new XXXXXBuildr()
.setType()
.setUserName()
.setpassword()
.build();
    }}

当然也可以写在具体的方法上,这样就可以在同一个class中配置多个环境的变量了。

在XMl中也是可以进行对应的配置的:
<beans profile="dev">    <bean>    </beans></beans><beans profile="prod">    <bean>    </beans></beans>

那么问题来了,我们应该怎么样去激活某个profile呢?
其实这需要两个独立的属性:spring.profile.active和spring.profile.default
如下的几个途径可以设置哦:
  1. 作为DispatcherServlet的初始化参数;
  2. 作为web应用的上下文参数;
  3. 作为JNDI条目
  4. 作为环境变量
  5. 作为JVM的系统属性
  6. 在集成测试类上,可以使用@ActiveProfile注解设置







原创粉丝点击