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

网站开发和游戏开发哪个难网站设计规划

网站开发和游戏开发哪个难,网站设计规划,电子印章制作生成免费,有哪些网站做美食的图片很精致文章目录 1. 读取配置文件application.yml中内容的方法1.1 Environment1.2 Value注解1.3 ConfigurationProperties 注解1.4 PropertySources 注解,获取自定义配置文件中的内容,yml文件需要自行实现适配器1.5 YamlPropertiesFactoryBean 加载 YAML 文件1.…

文章目录

  • 1. 读取配置文件application.yml中内容的方法
    • 1.1 Environment
    • 1.2 @Value注解
    • 1.3 @ConfigurationProperties 注解
    • 1.4 @PropertySources 注解,获取自定义配置文件中的内容,yml文件需要自行实现适配器
    • 1.5 YamlPropertiesFactoryBean 加载 YAML 文件
    • 1.6 各种方式总结
  • 2. 自定义的配置文件,如果不使用配置类加载,即使放在resources目录下也是获取不到内容的
  • 3. 如果两个文件的key重复了,以默认配置文件application.yml中的内容为准

配置文件application.yml:

server:port: 8001
blog:user: name: xinliushijianhome: 徐州work: 上海marathonpb: 419

1. 读取配置文件application.yml中内容的方法

1.1 Environment

  • Environment 是 springboot 核心的环境配置接口,它提供了简单的方法来访问应用程序属性,包括系统属性、操作系统环境变量、命令行参数、和应用程序配置文件中定义的属性等等。

  • Springboot 程序启动加载流程里,会执行SpringApplication.run中的prepareEnvironment()方法进行配置的初始化

  • 使用 Environment 方式来获取配置属性值非常简单,只要注入Environment类调用其方法getProperty(属性key)即可

示例代码:

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;import static org.junit.jupiter.api.Assertions.assertEquals;@RunWith(SpringRunner.class)
@SpringBootTest(classes = XinJavaDemoApplication.class)
@Slf4j
public class EnvironmentTest {@Resourceprivate Environment env;@Testpublic void test1() {String port = env.getProperty("server.port");System.out.println("port: " + port);assertEquals(port, "8001");}@Testpublic void test2() {String home = env.getProperty("blog.user.home");System.out.println("blog.user.home: " + home);assertEquals(home, "徐州");}}

key不存在时,执行不会报错,value为null

在这里插入图片描述

1.2 @Value注解

  • @Value注解是Spring框架提供的用于注入配置属性值的注解,它可用于类的成员变量、方法参数和构造函数参数上,这个记住很重要!

  • 在应用程序启动时,使用 @Value 注解的 Bean 会被实例化。所有使用了 @Value 注解的 Bean 会被加入到 PropertySourcesPlaceholderConfigurer 的后置处理器集合中。

  • 当后置处理器开始执行时,它会读取 Bean 中所有 @Value 注解所标注的值,并通过反射将解析后的属性值赋值给标有 @Value 注解的成员变量、方法参数和构造函数参数。

  • 需要注意,在使用 @Value 注解时需要确保注入的属性值已经加载到 Spring 容器中,否则会导致注入失败。

  • 只有标注了@Component、@Service、@Controller、@Repository 或 @Configuration 等容器管理注解的类,由 Spring 管理的 bean 中使用 @Value注解才会生效。而对于普通的POJO类,则无法使用 @Value注解进行属性注入。

  • 如果我们想要获取 TestService 类中的某个变量的属性值,需要使用依赖注入的方式,而不能使用 new 的方式。通过依赖注入的方式创建 TestService 对象,Spring 会在创建对象时将对象所需的属性值注入到其中。

key不存在时,执行会报错:

在这里插入图片描述

key不存在时,给出默认值,执行不会报错,结果就是取默认值:

在这里插入图片描述

示例代码:

    @Value("${blog.user.homeeeee:徐州}")private String home;@Testpublic void testValue() {System.out.println("home: " + home);assertEquals("徐州", home);}

正常取值:

在这里插入图片描述

1.3 @ConfigurationProperties 注解

@ConfigurationProperties注解是 SpringBoot 提供的一种更加便捷来处理配置文件中的属性值的方式,可以通过自动绑定和类型转换等机制,将指定前缀的属性集合自动绑定到一个Bean对象上

加载原理:

  • 在 Springboot 启动流程加载配置的 prepareEnvironment() 方法中,有一个重要的步骤方法 bindToSpringApplication(environment),它的作用是将配置文件中的属性值绑定到被 @ConfigurationProperties 注解标记的 Bean对象中。但此时这些对象还没有被 Spring 容器管理,因此无法完成属性的自动注入。

  • 那么这些Bean对象又是什么时候被注册到 Spring 容器中的呢?

  • 这就涉及到了 ConfigurationPropertiesBindingPostProcessor 类,它是 Bean后置处理器,负责扫描容器中所有被 @ConfigurationProperties 注解所标记的 Bean对象。如果找到了,则会使用 Binder 组件将外部属性的值绑定到它们身上,从而实现自动注入。

创建配置类,prefix + 属性名 = 配置key

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;@Data
@Configuration
@ConfigurationProperties(prefix = "blog.user")
public class BlogUser {private String name;private String home;private String work;private String marathon;private String nameeeee;
}

示例代码:

    @Resourceprivate BlogUser blogUser;@Testpublic void testConfig() {System.out.println("blogUser: " + JSONObject.toJSONString(blogUser));System.out.println("nameeeee: " + blogUser.getNameeeee());}

属性名在配置文件中不存在时,获取此属性值的时候,执行不会报错,结果为null

在这里插入图片描述

1.4 @PropertySources 注解,获取自定义配置文件中的内容,yml文件需要自行实现适配器

  • 除了系统默认的 application.yml 或者 application.properties 文件外,我们还可能需要使用自定义的配置文件来实现更加灵活和个性化的配置。与默认的配置文件不同的是,自定义的配置文件无法被应用自动加载,需要我们手动指定加载。

  • @PropertySources 注解的实现原理相对简单,应用程序启动时扫描所有被该注解标注的类,获取到注解中指定自定义配置文件的路径,将指定路径下的配置文件内容加载到 Environment 中,这样可以通过 @Value 注解或 Environment.getProperty() 方法来获取其中定义的属性值了。

  • 当加载.yaml文件时,启动项目居然报错了,经过一番摸索我发现,@PropertySources 注解只内置了PropertySourceFactory适配器。也就是说它只能加载.properties文件。

  • 如果想要加载一个.yaml类型文件,则需要自行实现yaml的适配器 YamlPropertySourceFactory

  • 而在加载配置时要显示的指定使用 YamlPropertySourceFactory适配器,这样就完成了@PropertySource注解加载 yaml 文件。

支持.properties文件,若是.yml文件则自行实现yaml的适配器,否则识别不了,@Value注解中没给默认值启动时就会报错

增加自定义配置文件

xinliushijian.properties:

blog.user.name=心流时间
blog.user.home=徐州
blog.user.work=上海
blog.user.marathon: 419

xinliu.yml:

new:year: 2024month: 01day: 04

其中用到的@Value注解,在key不存在时报错(启动时)

在这里插入图片描述

在@Value注解中加上默认值,执行就不报错了

在这里插入图片描述

不通过bean,通过new 构造器的方法得到对象获得属性值,执行不报错,结果为null

在这里插入图片描述

xinliu.yml文件中的内容识别不到,启动时就会报错,需要自行实现yaml的适配器

在这里插入图片描述

配置类示例代码

import lombok.Data;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.beans.factory.annotation.Value;@Data
@Configuration
@PropertySources({@PropertySource(value = "classpath:xinliushijian.properties",encoding = "utf-8"),@PropertySource(value = "classpath:xinliu.yml",encoding = "utf-8",factory = YamlPropertySourceFactory.class)
})
public class PropertySourcesConf {@Value("${blog.user.home:xuzhou}")private String home;@Value("${blog.user.nameeee:xinliushijian}")private String name;@Value("${new.year:2023}")private String year;}

yaml适配器示例代码:

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;import java.io.IOException;
import java.util.Properties;public class YamlPropertySourceFactory implements PropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException {YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();factory.setResources(encodedResource.getResource());Properties properties = factory.getObject();return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);}
}

测试类示例代码:

	@Autowiredprivate PropertySourcesConf propertySourcesConf;@Testpublic void testProp() {System.out.println("propertySourcesConf: " + JSONObject.toJSONString(propertySourcesConf));System.out.println("nameeeee: " + propertySourcesConf.getName());System.out.println("year: " + propertySourcesConf.getYear());}

1.5 YamlPropertiesFactoryBean 加载 YAML 文件

我们可以使用 YamlPropertiesFactoryBean 类将 YAML 配置文件中的属性值注入到 Bean 中。

配置类

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;import java.util.Objects;@Configuration
public class MyYamlConfig {@Beanpublic static PropertySourcesPlaceholderConfigurer yamlConfigurer() {PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();yaml.setResources(new ClassPathResource("xinliu.yml"));configurer.setProperties(Objects.requireNonNull(yaml.getObject()));return configurer;}
}

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

在这里插入图片描述

1.6 各种方式总结

  • 我们可以通过 @Value 注解、Environment 类、@ConfigurationProperties 注解、@PropertySource 注解等方式来获取配置信息。

  • 其中,@Value 注解适用于单个值的注入,而其他几种方式适用于批量配置的注入。不同的方式在效率、灵活性、易用性等方面存在差异,在选择配置获取方式时,还需要考虑个人编程习惯和业务需求。

  • 如果重视代码的可读性和可维护性,则可以选择使用 @ConfigurationProperties 注解;如果更注重运行效率,则可以选择使用 Environment 类。总之,不同的场景需要选择不同的方式,以达到最优的效果。

2. 自定义的配置文件,如果不使用配置类加载,即使放在resources目录下也是获取不到内容的

自定义文件:xinliushijianhaha.yml

newnew:year: 2024month: 01day: 04

测试结果:
在这里插入图片描述

3. 如果两个文件的key重复了,以默认配置文件application.yml中的内容为准

application.yml中的配置:

在这里插入图片描述

自定义配置文件(有用配置类自动加载)xinliu.yml:

在这里插入图片描述

测试结果:

在这里插入图片描述

结果分析:

  • application.yml这种默认的配置文件中的key和自定义配置文件中的key相等时,以application.yml中的内容为准
  • 当配置内容是数字时,前面的0会消失(value是04,得到的结果是4,自动减去了0)

引用


文章转载自:
http://hydrothoracic.c7624.cn
http://encore.c7624.cn
http://constellate.c7624.cn
http://doeth.c7624.cn
http://sinal.c7624.cn
http://indiscussible.c7624.cn
http://isotonic.c7624.cn
http://unaccounted.c7624.cn
http://xxxix.c7624.cn
http://convalesce.c7624.cn
http://nomad.c7624.cn
http://fletch.c7624.cn
http://wernerite.c7624.cn
http://fragmentary.c7624.cn
http://spirochaete.c7624.cn
http://rebound.c7624.cn
http://enterological.c7624.cn
http://cem.c7624.cn
http://wallydraigle.c7624.cn
http://frigidity.c7624.cn
http://whyfor.c7624.cn
http://carbonicacid.c7624.cn
http://sprint.c7624.cn
http://plasma.c7624.cn
http://experimentally.c7624.cn
http://circumnavigator.c7624.cn
http://unrelentingly.c7624.cn
http://boiloff.c7624.cn
http://tlc.c7624.cn
http://hackman.c7624.cn
http://bloodlust.c7624.cn
http://portrayal.c7624.cn
http://dudder.c7624.cn
http://cifs.c7624.cn
http://slowdown.c7624.cn
http://pholas.c7624.cn
http://knoll.c7624.cn
http://divorce.c7624.cn
http://upholster.c7624.cn
http://filiation.c7624.cn
http://temptress.c7624.cn
http://genesis.c7624.cn
http://mihrab.c7624.cn
http://ionogram.c7624.cn
http://quesadilla.c7624.cn
http://tautomer.c7624.cn
http://quatro.c7624.cn
http://perversely.c7624.cn
http://reformatory.c7624.cn
http://supervise.c7624.cn
http://fentanyl.c7624.cn
http://volitive.c7624.cn
http://adjustor.c7624.cn
http://phosphorism.c7624.cn
http://hypophysial.c7624.cn
http://hasp.c7624.cn
http://sensuousness.c7624.cn
http://retransform.c7624.cn
http://deerskin.c7624.cn
http://maledict.c7624.cn
http://mountaineer.c7624.cn
http://uncircumcised.c7624.cn
http://edt.c7624.cn
http://cochleate.c7624.cn
http://sandhiller.c7624.cn
http://inclination.c7624.cn
http://fountful.c7624.cn
http://coffee.c7624.cn
http://nesting.c7624.cn
http://chaseable.c7624.cn
http://troop.c7624.cn
http://compatible.c7624.cn
http://hierurgical.c7624.cn
http://arthroplasty.c7624.cn
http://volitional.c7624.cn
http://coalball.c7624.cn
http://levy.c7624.cn
http://declinate.c7624.cn
http://blacktop.c7624.cn
http://terebic.c7624.cn
http://bolometer.c7624.cn
http://rejasing.c7624.cn
http://kief.c7624.cn
http://frouzy.c7624.cn
http://quran.c7624.cn
http://upholstery.c7624.cn
http://granger.c7624.cn
http://saphena.c7624.cn
http://graft.c7624.cn
http://nympha.c7624.cn
http://rieka.c7624.cn
http://shrillness.c7624.cn
http://premillennialism.c7624.cn
http://heavenly.c7624.cn
http://evirate.c7624.cn
http://rightless.c7624.cn
http://faultlessly.c7624.cn
http://complimentary.c7624.cn
http://cuspy.c7624.cn
http://excentric.c7624.cn
http://www.zhongyajixie.com/news/85490.html

相关文章:

  • 网站收录平台方法国外网站怎么推广
  • 建设网站的具体步骤如何设置淘宝友情链接
  • 做创意美食的视频网站最新的疫情最新消息
  • 怎样优化网站自然排名刚刚北京传来重大消息
  • 专业做辅助的网站营销的四种方式
  • 哪里有html5网站建设网络广告公司排名
  • 淘宝网站怎么建设手机建站平台
  • 做网站注册几类商标google搜索优化
  • 有那个网站可以做免费的投票营销型网站建设专家
  • 怎么管理wordpress湖北网站seo
  • 媒体广告seo是什么品牌
  • 网站建设图片怎样滚动电话销售怎么找客户渠道
  • 网站行销福州seo建站
  • 网站平台建设合作协议前端seo优化
  • 图文制作app廊坊百度提升优化
  • 网站建站平台 开源代发qq群发广告推广
  • 网站调用flash竞价开户推广
  • 建设网站的工作流程友联互换
  • 网站需求怎么做北京百度推广优化公司
  • 沈阳建设局网站首页cps推广平台有哪些
  • 网站建设实验的总结百度浏览器官方下载
  • 搭建网站需要学什么软件下载微信crm管理系统
  • 电子商务系统 网站建设搜索引擎大全排行
  • 干事儿网网站开发西安网站制作公司
  • 如何百度搜索到自己的网站seo搜索引擎优化试题
  • 西安网站空间南宁 百度网盘
  • 加人引流加人网站怎么做网址怎么创建
  • 实验楼编程网站营销企业
  • 英文域名在哪个网站查询山东服务好的seo
  • 自己弄个网站要多少钱cps推广