当前位置: 首页 > news >正文 最新网站查询网站建设策划报价 news 2025/10/22 9:46:08 最新网站查询,网站建设策划报价,广西住房城乡和建设厅网站首页,als冰桶挑战赛的网络营销方式最近考虑项目在不同环境下配置的切换#xff0c;使用profile注解搭配PropertyPlaceholderConfigurer实现对配置文件的切换#xff0c;简单写了个demo记录下实现。 基本知识介绍 Profile Profile通过对bean进行修饰#xff0c;来限定spring在bean管理时的初始化情况#…最近考虑项目在不同环境下配置的切换使用profile注解搭配PropertyPlaceholderConfigurer实现对配置文件的切换简单写了个demo记录下实现。 基本知识介绍 Profile Profile通过对bean进行修饰来限定spring在bean管理时的初始化情况只有环境中激活的profile状态和修饰的value值对应时该bean才会被顺利加载并管理。 PropertyPlaceholderConfigurer PropertyPlaceholderConfigurer是PlaceholderConfigurerSupport的实现类是spring提供的一个解析yml或properties配置文件并将对应的值进行映射通过${}形式进行调用的配置读取类。举例来说配置文件中akb.num48那么在bean或配置文件中使用${akb.num}即可获取配置的值48。 简单实现 profile修饰的bean实例在不同环境下的切换 首先定义bean接口与default、dev、prob三种情况下的bean。 接口 DeafultProfileBean Component public interface DefaultProfileBean {String getInfo(); }default bean ProfileBeanDefault Profile(default) Component public class ProfileBeanDefault implements DefaultProfileBean{Overridepublic String getInfo() {return 这是default状态下的bean;} }dev bean ProfileBeanDev Profile(dev) Component public class ProfileBeanDev implements DefaultProfileBean{Overridepublic String getInfo(){return 这是dev环境使用的bean; } }dev bean ProfileBeanProd Profile(prod) Component public class ProfileBeanProd implements DefaultProfileBean{Overridepublic String getInfo() {return 这是prod环境使用的bean; } }加载上下文并输出加载的bean结果 AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext();// 设置profile环境context.getEnvironment().setActiveProfiles(dev);context.scan(com.huiluczP);context.refresh();System.out.println(loading success);DefaultProfileBean bean context.getBean(DefaultProfileBean.class);System.out.println(bean.getInfo());切换profile环境后的不同输出结果 可以看出Profile注解生效了。 配置类和配置文件 SpringConfigure配置类 Configuration public class SpringConfigure {BeanProfile(default)// 默认状态配置加载类public PropertyPlaceholderConfigurer defaultConfig(){PropertyPlaceholderConfigurer ppc new PropertyPlaceholderConfigurer();Resource resource new ClassPathResource(config/default.properties);ppc.setLocation(resource);return ppc;}BeanProfile(dev)// dev状态配置加载类public PropertyPlaceholderConfigurer devConfig(){PropertyPlaceholderConfigurer ppc new PropertyPlaceholderConfigurer();Resource resource new ClassPathResource(config/dev.properties);ppc.setLocation(resource);return ppc;}BeanProfile(prod)// prod状态配置加载类public PropertyPlaceholderConfigurer prodConfig(){PropertyPlaceholderConfigurer ppc new PropertyPlaceholderConfigurer();Resource resource new ClassPathResource(config/prod.properties);ppc.setLocation(resource);return ppc;}}管理了三个PropertyPlaceholderConfigurer类型的配置读取类分别对应不同的profile状态。通过ClassPathResource读取对应的配置文件如果用xml配置文件进行PropertyPlaceholderConfigurer bean的管理直接增加property location将value设置为对应的配置文件地址即可。 三个不同的配置文件内容 default.properties config.infodefault informationdev.properties config.infodev informationprod.properties config.infoprod information配置获取测试接口和bean类 public interface DefaultConfigBean {String getConfigInfo(); } Component public class DifferentConfigBean implements DefaultConfigBean{Value(${config.info})private String config;Overridepublic String getConfigInfo() {return 当前环境下的config信息为 config;} }通过${config.info}实现对配置文件的获取。 加载上下文进行处理 AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext();// 设置profile环境context.getEnvironment().setActiveProfiles(prod);context.scan(com.huiluczP);context.refresh();DefaultConfigBean configBean context.getBean(DefaultConfigBean.class);System.out.println(configBean.getConfigInfo());切换环境输出结果 profile激活 特别的说明下对项目profile环境怎么进行设置以对profile进行激活。没有特别指定时默认调用default修饰的bean。 直接上下文设定也就是上文中使用的对enviroment中的activeProfile进行设置即可。web项目中可以在web.xml中设置全局的变量 context-paramparam-namespring.profiles.alive/param-nameparam-valuedev/param-value /context-param如果是springMVC管理可以在DispatcherServlet的配置中增加init-param init-paramparam-namespring.profiles.alive/param-nameparam-valuedev/param-value /init-param可以在jvm的运行属性中设置tomcat等服务器的启动option也可设置jvm属性。 -Dspring.profiles.activedev对测试类使用注解ActiveProfiles进行修饰value设置为对应的环境。 总结 简单记录了一下spring profile和PropertyPlaceholderConfigurers类实现不同环境下的不同配置文件加载的方法在分支中进行快速切换还是挺方便的而且PropertyPlaceholderConfigurer映射的配置在spring读取其他的配置文件时也可以通过${}进行读取这样不同的环境配置文件只需要一份并将其中需要变动的部分用PropertyPlaceholderConfigurer进行管理即可。 文章转载自: http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.gwhjy.cn.gov.cn.gwhjy.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.wxgd.cn.gov.cn.wxgd.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.rgsnk.cn.gov.cn.rgsnk.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.qbkw.cn.gov.cn.qbkw.cn http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.hjrjr.cn.gov.cn.hjrjr.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.hsxkq.cn.gov.cn.hsxkq.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.smj79.cn.gov.cn.smj79.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.ggqcg.cn.gov.cn.ggqcg.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.wwdlg.cn.gov.cn.wwdlg.cn 查看全文 http://www.tj-hxxt.cn/news/239346.html 相关文章: c2c电子商务网站用到的技术陕西汉中最新消息今天 网站配色 蓝色电话销售-网站建设-开场白 西部数码网站管理助手 绑定域名wordpress无法上传图片 重庆网站制作多少钱html电影网站源码 做养生的网站多吗阿里虚拟机建设网站 成都门户网站建设多少钱wordpress建站创业 上海网站建设 中华企业录域名备案 网站备案 优质手机网站建设企业网站侧栏软件排行榜怎么做的 网站建设微信公众号文章上海做网站费用 华为建站wordpress天津建设工程信息网官网入口 房源网站建设网站到期忘记续费 郑州网站运营企业网站服务器选择 南昌网站开发商哪家强sem seo 通州区网站建设公司敦煌网介绍 阆中市网站建设服务twenty ten wordpress 正规的网站建设公司seo研究中心学员案例 巅峰网站建设如何做闲置物品自己的网站 北京城乡建设集团网站广西网站建设教程 企业网站建设基本流程图没有文字的网站怎么优化 合肥模板网站建设软件网站开发支持多个币种 自助个人网站注册建筑企业管理软件排名 网站建设预算和流程介绍如何建设好网站 如何做影视剧网站网站设计的论坛 织梦网站产品如何进入wordpress前台 高密做网站织梦做网站首页 晋州网站建设黑龙江网站开发 陕西省建设总工会网站搜狗推广手机客户端 找人做效果土去那网站找广州新闻发布 中国企业网站建设阴阳师网站怎么做 用别人网站做app的危害网站出现转站怎么办