1 Overview
1.1, in a Spring environment , the value of the injection properties file by file properties @PropertySource indicate the location, and then by injecting @Value value;
In SpringBoot environment , we only need to define attributes in application.yml, the injection can be used directly @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"; } }
result:
bookName:jackson+++++++++++bookAuthor:rose