zoukankan      html  css  js  c++  java
  • Spring Boot入门系列三(资源文件属性配置)

      Spring Boot 资源文件属性配置  

      配置文件是指在resources根目录下的application.propertiesapplication.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。

    在pom.xml中添加:

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

    一、资源文件属性配置直接读取

    /**
     * @author oyc
     * @Title:
     * @Description:
     * @date 2018/6/1321:44
     */
    @RestController
    @RequestMapping("/resource")
    public class ResourceController {
    
        @Value("${com.bestoyc.opensource.name}")
        private String name;
    
        @Value("${com.bestoyc.opensource.webSite}")
        private String webSite;
    
        @Value("${com.bestoyc.opensource.language}")
        private String language;
    
        @RequestMapping("/getResource1")
        public String GetResource1() {
            return "Name:" + name + "----WebSite:" + webSite + "----Language:" + language;
        }
    }
    

      注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody

    二、资源文件中的属性配置映射到实体类中

      将配置属性映射到实体类中,然后再将实体类注入到Controller或者Service中即可完成属性配置的读取。

      属性配置文件(resource.properties):

    com.bestoyc.opensource.name=bestoyc
    com.bestoyc.opensource.website=www.bestoyc.com
    com.bestoyc.opensource.language=java
    

        

      对应的实体类(Resource.java):

    /**
     * @author oyc
     * @Title:资源文件映射实体类
     * @Description:
     * @date 2018/6/13 21:32
     */
    @Configuration
    @ConfigurationProperties(prefix = "com.bestoyc.opensource")
    @PropertySource(value = "classpath:resource.properties")
    public class Resource {
        private String name;
        private String webSite;
        private String language;
    
      //省略getter、setter方法
    }

    注意:

    • @ConfigurationProperties注释中有两个属性:

      • locations:指定配置文件的所在位置
      • PropertySource:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以com.bestoyc.opensource开头)

        

      测试类(ResourceController.java):

    /**
     * @author oyc
     * @Title:
     * @Description:
     * @date 2018/6/1321:44
     */
    @RestController
    @RequestMapping("/resource")
    public class ResourceController {
    
        @Autowired
        private Resource resource;
    
    
        @RequestMapping("/getResource")
        public String GetResource() {
            return "Name:" + resource.getName() + "----WebSite:" + resource.getWebSite() + "----Language:" + resource.getLanguage();
        }
    }
    

      在SpringdemoApplication中加入@ComponentScan(basePackages= {"com.bestoyc"}),用于扫描我们的测试Controller包中的测试类方法:

       

    访问:http://localhost:8080/resource/getResource 时将得到Name:java----WebSite:www.bestoyc.com----Language:java

       

    --------------少年不努力,长大搞程序。欢迎关注,如有错误,恳请指正。
  • 相关阅读:
    Maven pom.xml中的元素modules、parent、properties以及import
    基于SpringBoot搭建应用开发框架(一) —— 基础架构
    Spring Boot项目使用Eclipse进行断点调试Debug
    eclipse 运行springboot项目
    如何在eclipse中使用mvn clean install
    https://www.cnblogs.com/zy-jiayou/p/7661415.html
    SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
    WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files
    在EF中使用Expression自动生成p=>new Entity(){X="",Y="",..}格式的Lambda表达式灵活实现按需更新
    EF跨库查询,DataBaseFirst下的解决方案
  • 原文地址:https://www.cnblogs.com/oycyqr/p/9180559.html
Copyright © 2011-2022 走看看