Spring配置文件简介
这么多的人,那么多的思想。 当我们为不同的客户实施软件时,有时我们需要处理同一项目的各种需求。 例如,客户A需要SAML身份验证,客户B需要LDAP身份验证。
借助Spring Profiles(可从Spring 3.1获得),我们能够提供一种方法来隔离我们已实现的应用程序配置的各个部分。 这个博客将帮助我们提供某些代码或仅适用于特定要求的某些Spring bean。 例如,当使用Spring Security时,此博客中使用的示例可用于为提供者管理器激活所需的身份验证提供者。
可以通过注释和/或xml配置配置文件。
注解
@Component或@Configuration注释的bean可以包含注释@Profile ,仅在特定环境中加载它们。
LDAP概要文件注释的配置
@Component
@Profile("ldap")
public class LDAPAuthentication { public LDAPAuthentication() {System.out.println("LDAP Authentication set by annotations");}
}
Saml配置文件注释的配置
@Component
@Profile("saml")
public class SAMLAuthentication { public SAMLAuthentication() {System.out.println("SAML Authentication set by annotations");}
}
XML格式
在刚开始的项目中可能不再使用,但也可以使某些Spring bean仅在XML配置中可用。
Spring XML配置
<!-- We use the profile attribute on the beans element to specify the profile.Only the child beans are loaded on initialization if the profile is active
-->
<beans profile="ldap"><bean class="com.jdriven.blog.profiles.xml.LDAPAuthentication" />
</beans>
<beans profile="saml"><bean class="com.jdriven.blog.profiles.xml.SAMLAuthentication" />
</beans>
激活正确的个人资料
当然,您可以组合两种配置,但是显而易见的是选择一种配置以使代码更可预测。 只是为了展示我们将它们组合在一个项目中的可能性。在普通的Java应用程序中,可以通过在应用程序上下文中激活配置文件来设置配置文件。
运行示例应用程序
public static void main(String[] args) {//Create new context to show the XML Spring profile setupGenericXmlApplicationContext ctx = new GenericXmlApplicationContext();//Setting 'ldap' as active profilectx.getEnvironment().setActiveProfiles("ldap");//Load the app-context.xml from the root of the classpathctx.load("classpath:app-context.xml");//We need to refresh the application because we added a resourcectx.refresh();//Closing the application context to release and destroy all resources and cached beansctx.close();//Creating a new context to show the annotation Spring profile setupAnnotationConfigApplicationContext actx = new AnnotationConfigApplicationContext();//Setting 'saml' as active profileactx.getEnvironment().setActiveProfiles("saml");//Scan base package for annotationsactx.scan("com.jdriven.blog");//We need to refresh the application because we added a scanactx.refresh();//Closing the application context to release and destroy all resources and cached beansactx.close();
}
有关此项目的完整源代码,请参见以下github:
- https://github.com/michelmeeuwissen/Spring-Profiles-Intro
翻译自: https://www.javacodegeeks.com/2015/03/introduction-to-spring-profiles.html
Spring配置文件简介相关推荐
- Spring入门简介
Spring概况 Spring的简介 Spring是一个轻量级的控制反转和面向切面的容器框架,它主要是为了解决企业应用开发的复杂性而诞生的: 目的:解决企业应用开发的复杂性 功能:使用基本的javaB ...
- spring boot简介_Spring Boot简介
spring boot简介 在本教程中,我们将看一下Spring Boot,看看它与Spring框架有何不同. 我们还将讨论Spring Boot提供的各种功能. 什么是Spring Boot? 在开 ...
- Spring 框架简介及官方压缩包目录
一.Spring 框架简介及官方压缩包目录 一.什么是spring spring是一个框架, 核心思想 IoC,AOP 简化企业开发,帮助分离对象之间的依赖关系 IoC:Inversion of Co ...
- Spring Boot简介,四大核心,特性
版权声明:本文为博主原创文章,未经博主允许不得转载 一.Spring Boot简介 1.Spring boot是Spring家族中的一个全新的框架,它用来简化Spring应用程序的创建和开发过程,也可 ...
- 【Spring框架一】——Spring框架简介
系列文章目录 Spring框架简介 系列文章目录 前言 一.什么是Spring框架? 二.Spring框架的优势 1.简化开发流程:Spring提供了许多现成的功能,可以使得开发人员在构建应用程序时减 ...
- 转 spring配置文件
spring配置文件 pom文件: <properties><commons-lang.version>2.6</commons-lang.version>< ...
- java spring 配置文件_[Java教程]Spring配置文件
[Java教程]Spring配置文件 0 2016-03-19 00:00:08 Spring配置文件是集成了Spring框架的项目的核心,引擎从哪里开始,中间都执行了哪些操作,小谈一下它的执行流程. ...
- 系统启动时,spring配置文件解析失败,报”cvc-elt.1: 找不到元素 'beans' 的声明“异常...
现象:spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseException; lineNumb ...
- 通过Spring配置文件中bean中的property赋值
基本数据类型赋值-通过spring配置文件中bean中的property 扩展-以此方式可以通过配置为连接数据的属性赋值 1.如果是基本数据类型,可以通过setter方法为对象中的属性设置初始值,应用 ...
最新文章
- java selenium (十四) 处理Iframe 中的元素
- 静态库和共享库之间的区别?
- php成绩查询系统的学习心得_PHP学习心得与体会
- OpenCV检测子像素中的角点位置
- OD逆向调试程序的笔记
- 如何优雅的统计代码耗时,快速知道你的程序慢在哪里!
- SaaS架构设计之共享设备
- UIBlock 按钮图片设置
- MapReduce实现大矩阵乘法
- 使用Robotframework-ride,导入Selenium2Library库后缺少“Open Browser”关键字
- 数据分析专题报告范文6篇_阿里大佬是怎么写数据分析报告的?
- 关于Stream流的使用
- spss软件简介与数据操作
- [TYVJ1827]『Citric II』一道防AK好题
- 计算机ab级ppt,计算机二级MS_Office考试PPT题型汇总附答案
- linux网络设备和一些知识点总结
- 任意模数ntt_任意模数NTT
- 不是青蛙就是王子,不是王子就是青蛙
- Effective C++:条款05:了解C++默默编写并调用哪些函数 (Know what functions C++ silently writes and calls.)...
- ListView滚动的方法
热门文章
- 第6章循环结构(二)
- openfire client聊天消息交互和存储离线(在线)消息记录策略
- leetcode初级算法6.字符串转整数(atoi)
- istio api_Istio的网络API解释了
- tomee_OpenLiberty:注入错误,适用于TomEE和Wildfly
- spring 定时器注释_带注释的控制器– Spring Web / Webflux和测试
- hashmap java_Java – HashMap详细说明
- java java se_Java 8 SE可选,严格的方法
- java常见的ide_在三个Java IDE中生成的三种常见方法
- react api_使用React流API将Akka流与rxJava结合在一起