SpringBoot--获取application.properties中的自定义的值

目录结构:

SpringBoot--获取application.properties中的自定义的值_第1张图片

 

这里的pom文件:



    4.0.0
    chapter-2-spring-boot-config
    《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 2 章《Spring Boot 配置》Demo

    demo.springboot
    chapter-2-spring-boot-config
    1.0
    jar

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.BUILD-SNAPSHOT
    

    
        UTF-8
        UTF-8
        1.8
    

    

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.spring4all
            spring-boot-starter-swagger
            1.5.1.RELEASE
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                1.5.1.RELEASE
            

        
    


    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/libs-snapshot
            
                true
            
        
    


 

application.properties:

## 书信息
demo.book.name=老人与海
demo.book.writer=海明威
demo.book.description=${demo.book.writer}'s${demo.book.name}


server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8


 

BookComponent:
package demo.springboot.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 书属性
 *
 */
@Component
@ConfigurationProperties(prefix = "demo.book")
@Validated
public class BookComponent {

    /**
     * 书名
     */
    @NotEmpty
    private String name;

    /**
     * 作者
     */
    @NotNull
    private String writer;

    public String getName() {
        return name;
    }

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

    public String getWriter() {
        return writer;
    }

    public void setWriter(String writer) {
        this.writer = writer;
    }
}

 

BookProperties:
package demo.springboot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 书属性
 *
 * Created by bysocket on 27/09/2017.
 */
@Component
public class BookProperties {

    /**
     * 书名
     */
    @Value("${demo.book.name}")
    private String name;

    /**
     * 作者
     */
    @Value("${demo.book.writer}")
    private String writer;

    public String getName() {
        return name;
    }

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

    public String getWriter() {
        return writer;
    }

    public void setWriter(String writer) {
        this.writer = writer;
    }
}

 

HelloBookController:
package demo.springboot.web;

import demo.springboot.config.BookProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Spring Boot Hello案例
 *
 * Created by bysocket on 26/09/2017.
 */
@RestController
public class HelloBookController {

    @Autowired
    BookProperties bookProperties;

    @GetMapping("/book/hello")
    public String sayHello() {
        return "Hello, " + bookProperties.getWriter() + " is writing "
                + bookProperties.getName() + " !";
    }
}

 

ConfigApplication:
package demo.springboot;

import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Spring Boot 应用启动类
 *
 * Created by bysocket on 26/09/2017.
 */
@EnableSwagger2Doc // 开启 Swagger
@SpringBootApplication
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

 

 

出现乱码的解决方案:

SpringBoot--获取application.properties中的自定义的值_第2张图片

 

你可能感兴趣的:(笔记)