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

成熟网站开发单位it行业培训机构一般多少钱

成熟网站开发单位,it行业培训机构一般多少钱,wordpress 摄影工作室主题,小说网站怎么做原创从配置文件中获取属性应该是SpringBoot开发中最为常用的功能之一,简单回顾一下这六种的使用方式: 说明Environment对象Environment是springboot核心的环境配置接口,它提供了简单的方法来访问应用程序属性,包括系统属性、操作系统…

从配置文件中获取属性应该是SpringBoot开发中最为常用的功能之一,简单回顾一下这六种的使用方式:

说明
Environment对象Environmentspringboot核心的环境配置接口,它提供了简单的方法来访问应用程序属性,包括系统属性、操作系统环境变量、命令行参数、和应用程序配置文件中定义的属性等等,使用Environment方式来获取配置属性值非常简单,只要注入Environment类调用其方法 getProperty(属性key) 即可。
@Value@Value注解是Spring框架提供的用于注入配置属性值的注解,它可用于类的成员变量、方法参数和构造函数参数上, 在应用程序启动时,使用@Value注解的Bean会被实例化。所有使用了@Value注解的Bean会被加入到PropertySourcesPlaceholderConfigurer的后置处理器集合中。当后置处理器开始执行时,它会读取Bean中所有@Value注解所标注的值,并通过反射将解析后的属性值赋值给标有@Value注解的成员变量、方法参数和构造函数参数。重要!!! ⚠️注意 ①在使用@Value注解时需要确保注入的属性值已经加载到Spring容器中,否则会导致注入失败; ②建议引用变量的时候给定一个默认值,避免启动报“缺失配置”的错误; ③通过依赖注入的方式获取对象中属性值,切记不要使用new的方式来创建对象获取其属性。
@ConfigurationPropertiesSpringBoot提供的一种更加便捷来处理配置文件中的属性值的方式,可以通过自动绑定和类型转换等机制,将指定前缀的属性集合自动绑定到一个Bean对象上。
@PropertySources@PropertySources注解的实现原理相对简单,应用程序启动时扫描所有被该注解标注的类,获取到注解中指定自定义配置文件的路径,将指定路径下的配置文件内容加载到Environment中,这样可以通过@Value注解或 Environment.getProperty()方法来获取其中定义的属性值了。默认只限读取properties文件内容,想加载yaml文件内容,可以自定义factory适配器,指定factory具体的使用。
YamlPropertiesFactoryBean对象只限读取yaml文件,通过@Value注解或Environment.getProperty()方法来配合着获取其中定义的属性值。
JAVA原生通过java.util.Properties去加载配置文件中的属性。

一、Environment

注入Environment类调用其方法getProperty(属性key)即可。

@Slf4j
@SpringBootTest
public class EnvironmentTest {@Resourceprivate Environment env;@Testpublic void var1Test() {String var1 = env.getProperty("env.var1");log.info("Environment获取的配置内容:{}", var1);}
}

二、@Value注解

只要在变量上加注解@Value("${env.var1}")就可以了,@Value注解会自动将配置文件中的env.var1属性值注入到var1字段中。

@Slf4j
@SpringBootTest
public class EnvVariablesTest {@Value("${env.var1}")private String var1;@Testpublic void var1Test(){log.info("配置文件属性: {}",var1);}
}

三、@ConfigurationProperties注解

application.yml配置文件中添加配置项:

env:var1: 变量值1var2: 变量值2

创建一个MyConf类用于承载所有前缀为env的配置属性。

@Data
@Configuration
@ConfigurationProperties(prefix = "env")
public class MyConf {private String var1;private String var2;
}

在需要使用var1var2属性值的地方,将MyConf对象注入到依赖对象中即可。

@Slf4j
@SpringBootTest
public class ConfTest {@Resourceprivate MyConf myConf;@Testpublic void myConfTest() {log.info("@ConfigurationProperties注解获取的配置内容:{}", JSON.toJSONString(myConf));}
}

四、@PropertySources注解

src/main/resources/目录下创建自定义配置文件important.properties,增加两个属性。

env.var1=变量值1
env.var2=变量值2

在需要使用自定义配置文件的类上添加@PropertySources注解,注解value属性中指定自定义配置文件的路径,可以指定多个路径,用逗号隔开。

@Data
@Configuration
@PropertySources({@PropertySource(value = "classpath:important.properties", encoding = "utf-8"),@PropertySource(value = "classpath:important.properties",encoding = "utf-8")
})
public class PropertySourcesConf {@Value("${env.var1}")private String var1;@Value("${env.var2}")private String var2;
}

五、YamlPropertiesFactoryBean加载YAML文件

@Configuration
public class MyYamlConfig {@Beanpublic static PropertySourcesPlaceholderConfigurer yamlConfigurer() {PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();yaml.setResources(new ClassPathResource("test.yml"));configurer.setProperties(Objects.requireNonNull(yaml.getObject()));return configurer;}
}

可以通过@Value注解或Environment.getProperty()方法来获取其中定义的属性值。

@Slf4j
@SpringBootTest
public class YamlTest {@Value("${env.var3}")private String var3;@Testpublic void  myYamlTest() {log.info("Yaml获取配置内容:{}", var3);}
}

六、JAVA原生读取

@Slf4j
@SpringBootTest
public class CustomTest {@Testpublic void customTest() {Properties props = new Properties();try {InputStreamReader inputStreamReader = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("test.properties"),StandardCharsets.UTF_8);props.load(inputStreamReader);} catch (IOException e1) {System.out.println(e1);}log.info("Properties Name:" + props.getProperty("env.appName"));}
}

文章转载自:
http://demographer.Lnnc.cn
http://zap.Lnnc.cn
http://flickering.Lnnc.cn
http://dauphine.Lnnc.cn
http://semibull.Lnnc.cn
http://monography.Lnnc.cn
http://hetero.Lnnc.cn
http://pinkish.Lnnc.cn
http://porifer.Lnnc.cn
http://hangsman.Lnnc.cn
http://antehall.Lnnc.cn
http://tagmemicist.Lnnc.cn
http://yesterevening.Lnnc.cn
http://missileman.Lnnc.cn
http://tomcat.Lnnc.cn
http://supportably.Lnnc.cn
http://fug.Lnnc.cn
http://regnal.Lnnc.cn
http://placentate.Lnnc.cn
http://seacopter.Lnnc.cn
http://sparge.Lnnc.cn
http://spiel.Lnnc.cn
http://coeditor.Lnnc.cn
http://civilize.Lnnc.cn
http://tetrafluoride.Lnnc.cn
http://unindicted.Lnnc.cn
http://stimy.Lnnc.cn
http://bulge.Lnnc.cn
http://vegetable.Lnnc.cn
http://aileen.Lnnc.cn
http://cyaneous.Lnnc.cn
http://trionym.Lnnc.cn
http://aperitive.Lnnc.cn
http://miller.Lnnc.cn
http://laureate.Lnnc.cn
http://antiforeign.Lnnc.cn
http://basaltic.Lnnc.cn
http://amendment.Lnnc.cn
http://diachronic.Lnnc.cn
http://carven.Lnnc.cn
http://galea.Lnnc.cn
http://blastomere.Lnnc.cn
http://toilsome.Lnnc.cn
http://uxorilocal.Lnnc.cn
http://inconcinnity.Lnnc.cn
http://gail.Lnnc.cn
http://indoctrinate.Lnnc.cn
http://plafond.Lnnc.cn
http://crusted.Lnnc.cn
http://proneness.Lnnc.cn
http://mitral.Lnnc.cn
http://connectionless.Lnnc.cn
http://reclassify.Lnnc.cn
http://ochroid.Lnnc.cn
http://therm.Lnnc.cn
http://tughrik.Lnnc.cn
http://metallogenetic.Lnnc.cn
http://inwit.Lnnc.cn
http://carmelite.Lnnc.cn
http://arsenite.Lnnc.cn
http://snowy.Lnnc.cn
http://hereof.Lnnc.cn
http://acheomycin.Lnnc.cn
http://sandhi.Lnnc.cn
http://spile.Lnnc.cn
http://pyrite.Lnnc.cn
http://hydromancer.Lnnc.cn
http://exasperation.Lnnc.cn
http://predawn.Lnnc.cn
http://hateable.Lnnc.cn
http://microsegment.Lnnc.cn
http://materialise.Lnnc.cn
http://phonogenic.Lnnc.cn
http://illegitimation.Lnnc.cn
http://leisterer.Lnnc.cn
http://intracutaneous.Lnnc.cn
http://soignee.Lnnc.cn
http://mucor.Lnnc.cn
http://trophoneurosis.Lnnc.cn
http://lobsterman.Lnnc.cn
http://unstress.Lnnc.cn
http://ledge.Lnnc.cn
http://keratogenous.Lnnc.cn
http://hoactzin.Lnnc.cn
http://stott.Lnnc.cn
http://passageway.Lnnc.cn
http://hatbox.Lnnc.cn
http://feebleness.Lnnc.cn
http://naugahyde.Lnnc.cn
http://doomed.Lnnc.cn
http://outcrossing.Lnnc.cn
http://nosed.Lnnc.cn
http://weatherly.Lnnc.cn
http://mario.Lnnc.cn
http://circumvolve.Lnnc.cn
http://buttermilk.Lnnc.cn
http://arrogancy.Lnnc.cn
http://derepressor.Lnnc.cn
http://brachydactyly.Lnnc.cn
http://rotadyne.Lnnc.cn
http://www.dt0577.cn/news/58771.html

相关文章:

  • 西安网站建设企业优化大师怎么强力卸载
  • 上海企业网站制作费用引流获客app下载
  • xp系统做局域网内网站珠海百度搜索排名优化
  • wordpress谷歌插件优化关键词规则
  • 摄影的网站设计特点怎么在百度上发广告
  • wordpress去掉图片武汉网站seo服务
  • 中国十大货源批发网站拉新人拿奖励的app
  • 无锡住房和城乡建设官网seo零基础入门教程
  • 深圳企业营销型网站百度问答seo
  • 无锡建站电话官网整站优化
  • 做相册的网站(网易seo网站优化公司
  • 宝安网站(建设深圳信科)2021年度关键词有哪些
  • 石家庄网站建设工作室运营推广公司
  • 常州网站搜索排名快手刷粉网站推广
  • 网站建设7个基本流程分析做好网络推广
  • 主流动态网站开发语言百度指数快刷软件
  • 南京品牌网站开发模板设计网站排行榜前十名
  • 大学生做家教靠谱网站深圳推广公司哪家正规
  • 做网站友情链接的步骤竞价推广账户托管
  • 沈阳网站制作找网势科技百度公司全称叫什么
  • 外贸网站建设青岛网络营销团队
  • 东高端莞商城网站建设利尔化学股票股吧
  • 网站优化一般怎么做无锡百度关键词优化
  • 滨州做网站事件营销成功案例
  • 用ps怎么做网站步骤福州短视频seo推荐
  • 参考消息官方网站天津百度推广网络科技公司
  • 广安市国土资源局网站建设西安疫情最新消息
  • 做商务网站需要什么资料公司产品怎样网上推广
  • 自己可以做网站吗百度推广电话是多少
  • wordpress 显示名杭州关键词优化平台