SB3 Spring boot Configuration文件的使用和读取

在项目的时候我们需要配置一些静态数据,和一些配置。

那么springboot使用了yml配置文件。其一定要注意格式的使用,若果不知道百度一下就可以了。在此就不必多说了

开始正题。

1.application.yml文件配置



2.创建一个GirlProperties类读取文件内容


我们会发现有个报错not found in classpatch这个错误是以为无法读取yml文件,有两种方式

   A。把文件放到classpatch中

   B。配置pom文件

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

3.启动项目


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

下面复制代码给大家

package com.big.fly.controller;

import com.big.fly.config.GirlProperties;
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;

/**
 * Created by Administrator on 2018/4/2.
 */

@RestController
public class HelloController {

  @Autowired
  private GirlProperties girlProperties;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){
        return "hello Spring boots"+girlProperties.toString();
    }
}

============================================================================

package com.big.fly.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by 方文飞 on 2018/4/2.
 */
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {

    private  String age;
    private  String name;

    public String getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "GirlProperties{" +
                "age='" + age + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

============================================================================

<?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.big.fly</groupId>
   <artifactId>fly</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>fly</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.1.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <!--spring boot Configuration Annotation Proessor not found in classpath-->
      <dependency>
         <groupId> org.springframework.boot </groupId>
         <artifactId> spring-boot-configuration-processor </artifactId>
         <optional> true </optional>
      </dependency>
      <!--spring boot Configuration Annotation Proessor not found in classpath-->
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>


</project>

============================================================================

girl:
    age: 18
    name: "XXFF"
server:
  port: 8081
============================================================================

代码层次



猜你喜欢

转载自blog.csdn.net/wenfeifang/article/details/80099795