当前位置: 首页 > news >正文

搭建网站需要哪些步骤晋江论坛手机版

搭建网站需要哪些步骤,晋江论坛手机版,wordpress怎么备份数据库,模板出售网站源码最近考虑项目在不同环境下配置的切换#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.hkea.cn/news/14528068/

相关文章:

  • 电子商务网站推广方案有域名有服务器怎么建站
  • 常德经开区网站重庆网站托管外包公司哪家好
  • 网站开发和推广方案云南微网站建设
  • 2015年友情链接网站源代码下载青岛专业网站制作
  • 网站维护服务器采购网站官网
  • 建设工程职称论文查询网站wordpress做新闻系统
  • 济南网站地址怎么选wordpress模板
  • ps做素材下载网站有哪些青岛html5网站制作
  • 四川省的建设厅注册中心网站金融外贸是做什么的
  • 上传到网站根目录百度竞价怎么收费
  • 西安公司网站设计建立网站的链接结构有哪几种形式?
  • 网络编程就是做网站么给自己女朋友做的网站
  • 如何设置网站域名网站基础三要素
  • 网站建设 猴王网络0成本今天军事新闻最新消息
  • 网站访问量统计怎么做wordpress文章内增加标题列表
  • 阳泉推广型网站建设营销型网站设计分析案例
  • c2c网站有哪几个php网站美化
  • 炫酷网站代码教育网站设计欣赏
  • 文库网站怎么做seoc网站开发案例详解
  • 如何自己搭建一个网站温州网站制作案例
  • 合肥有什么好的网站建设公司好国家级建设网站
  • 手机网站加百度商桥电脑网站与手机的区别
  • 网站怎么做免费推广方案广州百度推广外包
  • 衡水公司做网站建立外贸网站
  • 河北建设厅官方网站wordpress左栏主题
  • 高端网站定制的方法滨州公司做网站
  • 内蒙古网站备案网业升级坊问
  • 开发 程序网站wordpress根目录没有.htaccess
  • 利用网站制作网页湖南刚刚发生的大事件
  • 网站工作建设站电话在线图片制作工具大全