- 前言
- 第一部分 核心实现
- 第 1 章 Spring 整体架构和环境搭建
- 第 2 章 容器的基本实现
- 第 3 章 默认标签的解析
- 第 4 章 自定义标签的解析
- 第 5 章 bean 的加载
- 第 6 章 容器的功能扩展
- 第 7 章 AOP
- 第二部分 企业应用
- 第 8 章 数据库连接 JDBC
- 第 9 章 整合 MyBatis
- 第 10 章 事务
- 第 11 章 SpringMVC
- 第 12 章 远程服务
- 第 13 章 Spring 消息
2.8.1 profile 属性的使用
我们注意到在注册 Bean 的最开始是对 PROFILE_ATTRIBUTE 属性的解析,可能对于我们来说,profile 属性并不是很常用。让我们先了解一下这个属性。
分析 profile 前我们先了解下 profile 的用法,官方示例代码片段如下:
<beans xmlns="http://www.Springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.
Springframework.org/schema/jdbc"
xmlns:jee="http://www.Springframework.org/schema/jee"
xsi:schemaLocation="...">
... ...
<beans profile="dev">
... ...
</beans>
<beans profile="production">
... ...
</beans>
</beans>
集成到 Web 环境中时,在 web.xml 中加入以下代码:
<context-param>
<param-name>Spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
有了这个特性我们就可以同时在配置文件中部署两套配置来适用于生产环境和开发环境,这样可以方便的进行切换开发、部署环境,最常用的就是更换不同的数据库。
了解了 profile 的使用再来分析代码会清晰得多,首先程序会获取 beans 节点是否定义了 profile 属性,如果定义了则会需要到环境变量中去寻找,所以这里首先断言 environment 不可能为空,因为 profile 是可以同时指定多个的,需要程序对其拆分,并解析每个 profile 是都符合环境变量中所定义的,不定义则不会浪费性能去解析。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论