lengthuo实战–代码读取配置文件

类中获取classpath下面的(resources)目录下面的配置文件

@RestController
@PropertySource(value= {"classpath:jdbc.properties"})   //添加注解
public class TestController {

    private final static Logger logger = LoggerFactory.getLogger(TestController.class);

    @Value("${a}")                                       //获取配置文件数据
    private String a;

    @Autowired
    private UserServices userServices;

    @GetMapping("/user/{id}")
    public User hello(@PathVariable("id") int id){
        logger.info("id={}",id);
        logger.info("a={}",a);                              //使用配置文件内容
        return userServices.getUserById(id);
    }