Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

Java Spring Boot VS .NetCore (三)Ioc容器处理

Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

Java Spring Boot VS .NetCore (七) 配置文件

Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute

Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor

Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper

Spring Boot 配置文件

主要说下 properties & yml

下面来看下

application.properties的文件格式

Spring.datasource.url=jdbc:mysql://192.168.0.233:3306/test1?useSSL=false
Spring.datasource.username=uoso
Spring.datasource.password=uosotech_123
Spring.datasource.driver-class-name=com.mysql.jdbc.Driver

yml配置格式

spring1:
url1: dbc:mysql://192.168.0.102:3306/test1?useSSL=false

那么在代码中我们怎么来使用这些配置文件呢?

这里要介绍的就是注解 @Component  、 @Value 的使用了

默认只要编写名称为application.*.yml 都是能够被直接使用的,只需要把 配置文件映射到类的属性里面,代码如下

@Component
public class PropertiesConfig {
@Value("${Spring.datasource.url}")
private String database; public String getDatabase() {
return database;
} public void setDatabase(String database) {
this.database = database;
}
}

如果世界使用的application.yml 能够自动被填充的配置资源里面,写法给上面样就能拿到配置

我这里自定义一个yml 如:test.yml

这里需要添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

重新构建后,我新建一个class ,这里需要说明的就是 @ProtertySource  指定yml的路径 需要注意是的 classpath 这个指向的地址,是打包后的目录,调试的时候肯定找不到对应路径目录的文件需要自己配置下,否则就放在下图的目录结构

@Component
@PropertySource(value = "classpath:/test.yml")
@ConfigurationProperties(prefix = "spring1")
public class YMLConfig { public String getUrl1() {
return url1;
} public void setUrl1(String url1) {
this.url1 = url1;
}
@Value("${url1}")
private String url1;

使用通过注解 @Autowired \ @Resource 都可以

//测试yml配置
@Autowired
private YMLConfig ymlConfig;
@Test
public void testYmlConfig()
{ System.out.print(ymlConfig.getUrl1()); }

得到了我们想要的结果,下面在来说下.NetCore中的配置使用情况

.NetCore 配置文件

.NetCore提供了json 、xml 的方式来实现配置,这里我以json 来举个例子看下使用方式

默认也有一个appsettings.json 的文件,在不使用自定义配置的情况下,系统会加载这个配置文件 ,那么如果要把配置文件映射到class里面需要怎么处理呢?

下来创建一个配置类

 public class AuthorityConfig
{
public string Authority { get; set; }
public bool RequireHttpsMetadata { get; set; }
}

在appsetting里面我们根据目录结构添加即可

  "AuthorityConfig": {
"Authority": "http://localhost:20001",
"RequireHttpsMetadata": false
}

那么怎么把这两个Bind起来呢? 前面可以看到Java是通过@Value注解

.NetCore则是通过 在Startup 启动类中添加服务 里面有 IConfiguration 接口,根据这个接口来操作即可

services.AddOptions();
services.Configure<AuthorityConfig>(Configuration.GetSection("AuthorityConfig"));

根据节点位置配置关联的类,需要使用的时候通过构造函数注入即可

如果需要添加自定义的配置,我们使用启动类的构建来创建相关路径下的相关文件绑定到配置中Configuration 最后通过 Configuration 来绑定 Class之间的关系

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
var configurationbuilder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true)
.AddXmlFile($"appsettings.xml", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = configurationbuilder.Build(); // Configuration = configuration;
}

总结

其实 Spring Boot中也有其他的方式加载自定义的文件  ,这里的 ConfugurationBuilder 跟 Spring Boot中的  PropertySourcesPlaceholderConfigurer 类似,Java还提供了2中创建方式

1、YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();

2、YamlPropertySourceLoader loader = new YamlPropertySourceLoader();

MutablePropertySources sources = new MutablePropertySources();

可以看到第二种里面多了MutablePropertySources,英文上就是加载多个资源文件的,事实就是这个意思,加载多个资源文件,而第一种没抛出异常

下面来解析下第一种:看源码

public class YamlPropertiesFactoryBean extends YamlProcessor implements FactoryBean<Properties>, InitializingBean {
private boolean singleton = true;
@Nullable
private Properties properties; public YamlPropertiesFactoryBean() {
} public void setSingleton(boolean singleton) {
this.singleton = singleton;
} public boolean isSingleton() {
return this.singleton;
} public void afterPropertiesSet() {
if (this.isSingleton()) {
this.properties = this.createProperties();
} }

没有异常,而且是单例

看下第二种的源码,抛出了IO异常,在找不到文件目录文件的情况下会抛出异常

public class YamlPropertySourceLoader implements PropertySourceLoader {
public YamlPropertySourceLoader() {
} public String[] getFileExtensions() {
return new String[]{"yml", "yaml"};
} public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
if (!ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", (ClassLoader)null)) {
throw new IllegalStateException("Attempted to load " + name + " but snakeyaml was not found on the classpath");
} else {
List<Map<String, Object>> loaded = (new OriginTrackedYamlLoader(resource)).load();
if (loaded.isEmpty()) {
return Collections.emptyList();
} else {
List<PropertySource<?>> propertySources = new ArrayList(loaded.size()); for(int i = 0; i < loaded.size(); ++i) {
String documentNumber = loaded.size() != 1 ? " (document #" + i + ")" : "";
propertySources.add(new OriginTrackedMapPropertySource(name + documentNumber, (Map)loaded.get(i)));
} return propertySources;
}
}
}
}

配置这一块就说道这里~~~

下一章来介绍下自定的注解,同时也会结合.NetCore自定义属性标签来比较来时比较说明。

Java Spring Boot VS .NetCore (七) 配置文件的更多相关文章

  1. Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

    系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...

  2. Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  3. Java Spring Boot VS .NetCore (三)Ioc容器处理

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  4. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  5. Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  6. Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  7. Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  8. Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  9. Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

随机推荐

  1. PLSQL:orecal,tnsname简介

    导入ORACLE遇到很多问题,学了好多,其中很长时间花在网络配置上,刚开始学,具体原因不知道,先把搜集到的好文章存下来,以后慢慢研究. 监听配置文件             为了使得外部进程 如 CA ...

  2. 清明培训 清北学堂 DAY1

    今天是李昊老师的讲授~~ 总结了一下今天的内容: 1.高精度算法 (1)   高精度加法 思路:模拟竖式运算 注意:进位 优化:压位 程序代码: #include<iostream>#in ...

  3. [WC2007]剪刀石头布(最大流)

    洛古 一句话题意:给定一张图,每两点之间有一条有向边或无向边,把所有无向边定向,使图中三元环个数尽量多 因为原图是一个完全图,假设图中任意三点都能构成三元环,那么途中三元环的个数为:\(\binom{ ...

  4. Redmine简易安装与系统优化

    安装版本为bitnami-redmine-2.6.5-0 ,用的Bitnami的一键安装包 . 下载地址https://bitnami.com/stack/redmine/installer 简要安装 ...

  5. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  6. MySQL_列值为null对索引的影响_实践

    一.首先看一个我在某公众号看到的一个关于数据库优化的举措 二.如果where子句中查询的列执行了 “is null” 或者 “is not null” 或者 “<=> null” 会不会使 ...

  7. Redis(REmote DIctionary Server)基础

    Redis(REmote DIctionary Server)基础 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Redis是一个开放源代码(BSD许可)的内存数据结构存储,用作数 ...

  8. js 计算当年还剩多少时间的倒数计时 javascript 原理解析【复制到编辑器查看推荐】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. JAVA进阶9

    间歇性混吃等死,持续性踌躇满志系列-------------第9天 1.使用throw语句抛出异常 在通常情况下,程序发生错误时系统会自动抛出异常,而有时希望程序自动抛出异常,可以使用throw语句来 ...

  10. 查不到opencv版本的问题

    检查opencv版本:pkg-config --modversion opencv 前两天卸载了opencv3.0,想重装2.4版本.安装是没有问题,但现在查不到opencv版本,程序也编译不通过. ...