SpringBoot---常规属性配置

1、概述

      1.1、在Spring环境下,注入properties文件中的值,通过@PropertySource指明properties文件的位置,然后通过@Value注入值;

          在SpringBoot环境下,我们只需要  在application.yml中定义属性,直接使用@Value注入即可

package com.an.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @description:
 * @author: anpeiyong
 * @date: Created in 2019/11/19 14:56
 * @since:
 */
@Configuration
@ComponentScan(value = "com.an")
@PropertySource(value = "classpath:test.properties")
public class ELConfig {

}

  

package com.an.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: anpeiyong
 * @date: Created in 2019/11/19 15:34
 * @since:
 */
@RestController
public class ELTestController {

    @Value(value = "${book.name}")
    private String bookName;

    @Value(value = "${book.author}")
    private String bookAuthor;

    @RequestMapping(value = "/eltest",method = RequestMethod.GET)
    public String test(){
        System.out.println("bookName:"+bookName+"+++++++++++"+"bookAuthor:"+bookAuthor);
        return "hello";
    }

}

  

结果:

bookName:jackson+++++++++++bookAuthor:rose

      

猜你喜欢

转载自www.cnblogs.com/anpeiyong/p/11890045.html