spring boot的基本配置——spring boot的全局配置文件——读取应用配置——@PropertySource注解+@Value注解——读取其它配置文件


 

 

package com.test.controller;

import com.test.controller.model.StudentProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@PropertySource({"test.properties","ok.properties"})
public class StartMainController
{
    @Value("${my.msg}")
    private String mymsg;

    @Value("${your.msg}")
    private String yourmsg;

    @Autowired
    StudentProperties studentProperties;



    @Autowired
    private Environment ev;

    @Value("key_test_001")
    private  String as;

    @RequestMapping("/12")
    @ResponseBody
    public String home12()
    {
        return "hello---12";
    }


    @RequestMapping("/123")
    @ResponseBody
    public String home123()
    {
        return "hello---123";
    }

    @RequestMapping("/1234")
    @ResponseBody
    public String home1234()
    {
        return ev.getProperty("key_test");
    }

    @RequestMapping("/12345")
    @ResponseBody
    public String home12345()
    {
        return as;
    }

    @RequestMapping("/123456")
    @ResponseBody
    public String home123456()
    {
        return studentProperties.getSname();
    }

    @RequestMapping("/1234567")
    @ResponseBody
    public String home1234567()
    {
        return mymsg+"   "+yourmsg;
    }

}

  

相关