Spring 快速开始 Profile 和 Bean
和maven profile类似,Spring bean definition profile 有两个组件:声明和激活。
【栗子:开发测试环境使用HyperSQL 生产环境使用JNDI上下文根据配置查找数据库源】
<beans>
<beans profile="development,qa">
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:com/wrox/config/sql/schema.sql"/>
<jdbc:script location="classpath:com/wrox/config/sql/test-data.sql"/>
</jdbc:embedded-database>
</beans>
<beans profile="production">
<context:property-placeholder location="file:/settings.properties" />
<jee:jndi-lookup id="dataSource" jndi-name="java:/comp/env/${production.dsn}" />
</beans>
</beans>
【栗子:@Configuration版本】
interface DataConfiguration{
DataSource dataSource();
}
@Configuration
@Import({DevQaDataConfiguration.class,ProductionDataConfiguration.class})
@ComponentScan(basePackages = "com.wrox.site",excludeFilters = @ComponentScan.Filter(Controller.class))
public class RootContextConfiguration{}
@Configuration
@Profile({"development","qa"})
public class DevQaDataConfiguration implements DataConfiguration{
@Override
@Bean
public DataSource dataSource(){
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:com/wrox/config/sql/schema.sql")
.addScript("classpath:com/wrox/config/sql/test-data.sql")
.build();
}
}
@Configuration
@Profile("production")
@PropertySource("file:settings.properties")
public class ProfuctionDataConfiguration implements DataConfiguration{
@Value("production.dsr")
String dataSourceName;
@Override
@Bean
public DataSource dataSource(){
return new JndiDataSourceLookup().getDataSource("java:/comp/env/"+this.dataSourceName);
}
}
【激活profile】
1.应用程序级别激活
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>development</param-value>
</context-param>
2.Servlet级别激活
<servlet>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>development</param-value>
</init-param>
3.运行时命令行参数激活 -Dspring.profiles.active=development
4.编程式激活 configurableEnvironment.setActiveProfiles("development"); 将影响倒子应用上下文。
【profile细节】@Profile和@Component类似可以用作元数据注解,我们自己定义一个@Development注解。
@Documented
@Retention(value={ElementType.TYPE,ElementType.METHOD})
@Profile("development")
public @interface Development{}
【反模式和安全问题】当我们使用bean definition profile 解决问题要问自己。
1.有没有更简单的办法。如果多个profile创建的都是相同bean但使用的设置不同,这其实可以通过@PropertySource和properties文件完成。
2.profile中bean有哪些类型。大多数情况下,两个不同的profile具有的bean应该非常接近的,QA一搬和Prod一致,DEV和QA有一定区别。
3.安全意义?禁止去使用profile控制应用程序安全,因为终端用户可能会启用或禁用JVM命令行属性的方式 激活或冻结 profile,这样就绕过安全了。
一种比较傻叉的做法,dev禁用产品许可,qa和prod激活产品许可,聪明的用户只要将prod和qa切换回dev就能免费使用商业产品。
Spring 快速开始 Profile 和 Bean的更多相关文章
- Spring实战(四)Spring高级装配中的bean profile
profile的原意为轮廓.剖面等,软件开发中可以译为“配置”. 在3.1版本中,Spring引入了bean profile的功能.要使用profile,首先要将所有不同的bean定义整理到一个或多个 ...
- Spring Boot 之 Profile --快速搞定多环境使用与切换
Spring Profile是Spring3引入的概念,主要用在项目多环境运行的情况下,通过激活方式实现多环境切换,省去多环境切换时配置参数和文件的修改,并且Spring profile提供了多种激活 ...
- spring快速入门(三)
一.在spring快速入门(二)的基础上,原先我们是采用构造方法完成对象的注入.这里还有其他的方法可以完成注入,通过set方法来完成. 修改UserActionImpl package com.mur ...
- spring快速入门(二)
一.在spring快速入门(一)的基础上,我们来了解spring是如何解决对象的创建以及对象之间的依赖关系的问题 (比如client中依赖UserAction的具体实现,UserActionImpl中 ...
- Spring实战3:装配bean的进阶知识
主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...
- spring注解之@profile
spring中@profile与maven中的profile很相似,通过配置来改变参数. 例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套 ...
- Spring Boot 之 Profile 使用
Spring Boot 之 Profile 使用 一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理.Spring Boot 对此提供了简便的支持. 关键词: @Profile.spri ...
- Spring 环境与profile(一)——超简用例
什么是profile,为什么需要profile? 在开发时,不同环境(开发.联调.预发.正式等)所需的配置不同导致,如果每改变一个环境就更改配置不但麻烦(修改代码.重新构建)而且容易出错.Spring ...
- Java基础-SSM之Spring快速入门篇
Java基础-SSM之Spring快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java ...
随机推荐
- luogu3188/bzoj1190 梦幻岛宝珠 (分层背包dp)
他都告诉你能拆了 那就拆呗.把每个重量拆成$a*2^b$的形式 然后对于每个不同的b,先分开做30个背包 再设f[i][j]表示b<=i的物品中 容量为$ j*2^i+W\&((1< ...
- Python学习day3 数据类型Ⅰ(str,int,bool)
day3 数据类型 @上节内容补充 每周一个思维导图-xmind.exe in / not in #示例:(是否包含敏感字符)while True: text = input('请输入你要说的 ...
- 编写高质量代码:改善Java程序的151个建议 --[0~25]
警惕自增的陷阱 public class Client7 { public static void main(String[] args) { int count=0; for(int i=0; i& ...
- Cookie知识点总结
Cookie机制是采用客户端保持Http状态信息的方案. Cookie是在浏览器访问web服务器的某个资源的时候,由web服务器在http响应消息头中附带给浏览器的一个小文本文件. 一旦web服务器保 ...
- kafka为什么这么优秀!
kafka为什么这么优秀! 阿飞的博客 匠心零度 今天 1.动机2.持久化3.效率4.生产者4.1负载均衡4.2异步发送5.消费者Push vs. Pull消费者位置离线数据加载 1.动机 kafka ...
- Injection with CDI (Part I)
官方参考:http://docs.jboss.org/weld/reference/latest/en-US/html/index.html https://antoniogoncalves.org/ ...
- vue-router 如何默认显示三级子路由
{ path: '/aaa', name: 'aaa', title: '统计分析', component: () => import('@/aaa.vue'),//一级子组件.容器 child ...
- Python中pandas dataframe删除一行或一列:drop函数
用法:DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False) 参数说明:labels 就是要删除的行列的 ...
- 【洛谷P1059 明明的随机数】
题目描述明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着 ...
- R语言绘图(FZ)
P-Value Central Lmit Theorem(CLT) mean(null>diff) hist(null) qqnorm(null) qqline(null) pops<-r ...