1. 定义profile
    1.   

      <beans> //root
      <beans profile="dev">
      <bean id=.../>
      </beans>
      <beans profile="qa">
      <bean id=.../>
      </beans>
      <beans profile="prod">
      <bean id=.../>
      </beans>
      </beans>
  2.   激活profile
    1.   spring在确定哪个profile处于激活状态时,需要依赖两个独立的属性:srping.profiles.active 和 spring.profiles.default。其中active属性优先。如果都没有设置,只会创建没有定义在profile中的bean
    2. 设置方法有6个:
      1. 作为DispatcherServlet的初始化参数
      2. 作为Web应用的上下文参数
      3. 作为JNDI条目
      4. 作为环境变量
      5. 作为JVM的系统属性
      6. 在集成测试类上,使用@ActiveProfiles注解设置
    3. 示例
      1.   

        web.xml
        <web-app>
        <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/spring-bean-config.xml</param-value>
        </context-param>
        <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>dev</param-value>
        </context-param>
        <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
        <servlet-name>appservlet</servlet-name>
        <servlet-class>xxx</servlet-class>
        <init-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>dev</param-value>
        </init-param>
        </servlet>
        </web-app>
  3. 条件化创建bean
    1. 使用限定符注解@Qualifier
    2. 每个bean,默认有个ID,也默认有个限定符,这两个的默认值“恰巧”都是类名的首字母小写
    3. 创建bean时,使用@Qualifier,装配beans是,还是用@Qualifier

Spring 梳理-profile与条件化定义bean的更多相关文章

  1. (二)spring 高级装配-Condition -条件化的bean

    Condition:满足某个特定条件的情况下创建bean 条件化配置bean: a:@Conditional 指定一个class ,它指明了通过条件对比的类.如果没有指定class则通过Condito ...

  2. Spring入门(六):条件化的bean

    1. 概念 默认情况下,Spring中定义的bean在应用程序启动时会全部装配,不管当前运行的是哪个环境(Dev,QA或者Prod),也不管当前运行的是什么系统(Windows或者Linux),但有些 ...

  3. Spring高级装配(二) 条件化的bean

    如果你希望一个bean在特定的条件下才会出现: 应用的类路径下包含特定的库时才创建 只有当某个特定的bean也声明之后才会创建 某个特定的环境变量设定之后才创建某个bean 在Spring 4之前,很 ...

  4. 第3章—高级装配—条件化的Bean

    条件化的Bean 通过活动的profile,我们可以获得不同的Bean.Spring 4提供了一个更通用的基于条件的Bean的创建方式,即使用@Conditional注解. @Conditional根 ...

  5. 002Conditional条件化创建bean

    01.条件化配置bean @Bean @Conditional(MagicExistsCondition.class)---->条件化创建bean public MagicBean magicB ...

  6. 【Java Web开发学习】Spring4条件化的bean

    [Java Web开发学习]Spring4条件化的bean 转载:https://www.cnblogs.com/yangchongxing/p/9071960.html Spring4引入了@Con ...

  7. 001profile条件化创建bean

    01.类级别条件创建 @Configuration @Profile("dev") public class Aclass{}---->影响整个类,包括类的注解.开发环境,类 ...

  8. @Condition 条件化注册Bean

    看<Spring源码深度解析>笔记 1.@Condition: 按照一定的条件进行判断,满足条件给容器中注册bean:实例: 根据系统给容器中注册Bean,如果是windows注册(“bi ...

  9. spring对bean的高级装配之基于@Conditional条件化装配

    上篇介绍了如何基于profile来条件化创建bean,spring会根据profile的激活状态来进行创建;这篇介绍如何基于spring4.0引入的@Conditional和Condition接口来更 ...

随机推荐

  1. springboot之additional-spring-configuration-metadata.json自定义提示

    springboot之additional-spring-configuration-metadata.json自定义提示 简介 additional-spring-configuration-met ...

  2. Oracle在VMware虚拟机安装的配置

    我是在VMware虚拟机上安装的Oracle , 我只说说我踩过的几个坑吧. VMware的虚拟网络编辑器 仅主机模式相当于在你的主机和虚拟机之间建立了一个局域网,里面只有你的主机和虚拟机 可以通过D ...

  3. Java-手动搭建SSM(Maven)

    一.环境部署 操作系统:windows10专业版 jdk:1.8.0_144 IDE:eclipse-oxygen 服务器:tomcat 9.0 数据库:mysql 5.7.18 Maven:3.54 ...

  4. 【Linux命令】modprobe命令

    modprobe(module probe)命令 用于自动处理可载入模块. 1)语法 modprobe [-acdlrtvV][--help][模块文件][符号名称 = 符号值] 2)补充 modpr ...

  5. 成功项目管理与PMP认证2017

    http://study.163.com/course/courseLearn.htm?courseId=1064005#/learn/video?lessonId=1003778171&co ...

  6. P3225 [HNOI2012]矿场搭建 割点 tarjan 双联通分量

    https://www.luogu.org/problemnew/show/P3225 题意 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条 ...

  7. POJ-1469 COURSES ( 匈牙利算法 dfs + bfs )

    题目链接: http://poj.org/problem?id=1469 Description Consider a group of N students and P courses. Each ...

  8. js生成玫瑰图

    html: <canvas id="c" height="600" width="600"></canvas> js ...

  9. webpack4 output配置 filename chunkhash报错

    这里的hash由chunkhash改成hash,原因是使用HotModuleReplacementPlugin之后不能使用chunkhash和contenthash.看到有些地方说把“hot:true ...

  10. CentOS 7 下网络无法访问 Failed to start LSB: Bring up/.

    [root@localhost Desktop]# ping 192.168.2.1PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.64 byt ...