【SpingBoot学习笔记】SpingBoot之读取resource/config目录下自定义properties/yml文件(注解方式)

之前已经写了一篇读写properties文件的文章,见《Java读取properties配置文件写法》,但如果是springboot项目,配置统一在resource/config目录下,使用注解如何读取呢,写法如下

打开IDEA,新建maven项目readproperties

项目结构

配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>readproperties</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--手动引入-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <!--手动引入-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


    <!--手动引入-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin </artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

新建springboot启动类

package test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringBootApplication {

    public static void main(String[] args) {
            SpringApplication.run(MySpringBootApplication.class,args);
    }
}

新建my.properties,放在resource/config目录

#MY CONFIG
#########################################
current.dbType = Oracle

创建配置类

package test.init;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Configuration
@PropertySource("classpath:config/my.properties")
public class MyConfig {
    
    //获取配置中的信息
    @Value("${current.dbType}")
    private  String currentDbType;

    public String getCurrentDbType() {
        return currentDbType;
    }

    public void setCurrentDbType(String currentDbType) {
        this.currentDbType = currentDbType;
    }
}

创建测试类

package test.webservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import test.init.MyConfig;

@RestController
public class HelloWorldRestController {

    @Autowired
    private MyConfig myConfig;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String hello(){
        String currentDbType=myConfig.getCurrentDbType();
        return    "current.dbType为:"+currentDbType;
    }
}

然后启动项目,在浏览器输入http://localhost:8080/,执行结果如下

 

 内容换成中文(修改为:current.dbType = Oracle数据库),果然也会乱码

 

 依照网上的解决方法,修改MyConfig类的PropertySource注解

@PropertySource(value = "classpath:config/my.properties",encoding ="UTF-8")

然后encoding报红,提示不支持,应该是当前springboot版本太低了,因此还需修改pom.xml

    <!--手动引入-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

然后重启,浏览器器上查看,已正常

 

 希望对大家有所帮助!

ps:据说使用yml文件,中文不会乱码,验证如下

增加配置类MyConfigYML,配置文件my.yml

current:
  dbType : Oracle数据库文件

 

package test.init;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Configuration
@PropertySource(value = "classpath:config/my.yml")
public class MyConfigYML {
    
    //获取配置中的信息
    @Value("${current.dbType}")
    private  String currentDbType;

    public String getCurrentDbType() {
        return currentDbType;
    }

    public void setCurrentDbType(String currentDbType) {
        this.currentDbType = currentDbType;
    }
}

测试类中增加

    @Autowired
    private MyConfigYML myConfigYML;   

 @RequestMapping(value = "/yml",method = RequestMethod.GET)
    public String helloYml(){
        String currentDbType=myConfig.getCurrentDbType();
        System.out.println(currentDbType);
        return    "my.yml中current.dbType为:"+currentDbType;
    }

运行,执行结果为

 

 

 果然不会乱码,这么来看yml的优势比较明显,哈哈,总觉得写了不少废话。

posted @ 2022-10-17 15:33  泠雨0702  阅读(1237)  评论(0编辑  收藏  举报