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

网站优化推广哪家好搜索排行榜

网站优化推广哪家好,搜索排行榜,前端网站做中 英文,海南做房地产网站的网络公司最近考虑项目在不同环境下配置的切换,使用profile注解搭配PropertyPlaceholderConfigurer实现对配置文件的切换,简单写了个demo记录下实现。 基本知识介绍 Profile Profile通过对bean进行修饰,来限定spring在bean管理时的初始化情况&#…

最近考虑项目在不同环境下配置的切换,使用profile注解搭配PropertyPlaceholderConfigurer实现对配置文件的切换,简单写了个demo记录下实现。

基本知识介绍

@Profile

@Profile通过对bean进行修饰,来限定spring在bean管理时的初始化情况,只有环境中激活的profile状态和修饰的value值对应时,该bean才会被顺利加载并管理。

PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer是PlaceholderConfigurerSupport的实现类,是spring提供的一个解析yml或properties配置文件并将对应的值进行映射,通过${}形式进行调用的配置读取类。举例来说,配置文件中akb.num=48,那么在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 {@Bean@Profile("default")// 默认状态配置加载类public PropertyPlaceholderConfigurer defaultConfig(){PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();Resource resource = new ClassPathResource("config/default.properties");ppc.setLocation(resource);return ppc;}@Bean@Profile("dev")// dev状态配置加载类public PropertyPlaceholderConfigurer devConfig(){PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();Resource resource = new ClassPathResource("config/dev.properties");ppc.setLocation(resource);return ppc;}@Bean@Profile("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.info=default information

dev.properties

config.info=dev information

prod.properties

config.info=prod 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。

  1. 直接上下文设定,也就是上文中使用的,对enviroment中的activeProfile进行设置即可。
  2. web项目中可以在web.xml中设置全局的变量:
<context-param><param-name>spring.profiles.alive</param-name><param-value>dev</param-value>
</context-param>
  1. 如果是springMVC管理,可以在DispatcherServlet的配置中增加init-param:
<init-param><param-name>spring.profiles.alive</param-name><param-value>dev</param-value>
</init-param>
  1. 可以在jvm的运行属性中设置,tomcat等服务器的启动option也可设置jvm属性。
-Dspring.profiles.active=dev
  1. 对测试类使用注解@ActiveProfiles进行修饰,value设置为对应的环境。

总结

简单记录了一下spring profile和PropertyPlaceholderConfigurers类实现不同环境下的不同配置文件加载的方法,在分支中进行快速切换还是挺方便的,而且PropertyPlaceholderConfigurer映射的配置在spring读取其他的配置文件时也可以通过${}进行读取,这样不同的环境配置文件只需要一份,并将其中需要变动的部分用PropertyPlaceholderConfigurer进行管理即可。

http://www.hrbkazy.com/news/7318.html

相关文章:

  • 网络公司网站建设seo店铺描述例子
  • 物联网小项目河南靠谱seo电话
  • wordpress留言板隐藏seo网站营销推广公司
  • 做我女朋友网站p0rn视频全球搜索引擎市场份额
  • 欧 美 做 爱 视频网站抖音seo关键词排名技术
  • 政府 网站建设方案快速排名上
  • 企业网站建设相关书籍在线阅读百度收录关键词查询
  • 用什么java软件做网站什么软件可以免费引流
  • 黑帽seo寄生虫seo课程培训
  • 成都网站建设推广在地推扫码平台
  • 大兴 网站建设做网络推广的网站有哪些
  • 西安网站建设盈科产品线上推广渠道
  • 无锡做网站f7wl网站链接查询
  • 兼容模式网站错位怎么做网络营销平台
  • 论坛门户静态网页模板搜索引擎优化面对哪些困境
  • 品古典家具网站模板百度指数在线查询工具
  • 移动医护网站建设利弊百度经验app下载
  • 沈阳网站制作培训什么是seo?
  • 宜春做网站哪里好站长之家最新网站
  • 专业做网站建设公司如何注册一个平台
  • 购物网站排名2017凡科建站教程
  • 网站logo如何修改seo排名影响因素主要有
  • 无锡网站建设咨询百度知道个人中心
  • 别人怎么看见我做的网站湖北百度推广电话
  • 做的丑的网站有哪些知乎西安seo外包优化
  • 把一个网站挂到网上要怎么做免费网站建站
  • 带地板翻转的网站怎么做百度推广教程视频教程
  • 易语言网站开发教程青岛网站seo诊断
  • 用模板做网站的方法郑州搜索引擎优化公司
  • 有限公司网站建设 中企动力佛山百度搜索引擎优化的推广计划