Spring annotation to get the value in the properties file

The properties file format is as follows: (xxx_common.properties)

xxx.base.path=http://localhost:8080/xxx/

 Use annotations to scan and load property files in the spring file:

<context:component-scan base-package="com.xxx.xxx" />
<util:properties id="common" location="classpath:xxx_common.properties" />

To use util:properties to add constraints:

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd"

 

Annotated classes: (com.xxx.xxx)

@Repository("com.xxx.xxx.vo.CommonProperties")
public class CommonProperties {
@Value("#{common['xxx.base.path']}")
public String basePath;
public String getBasePath() {
  return basePath;
 }.......

 Note: The common of @Value("#{common['xxx.base.path']}") is the same as the id of this <util:properties id="common". ['xxx.base.path'] is the key of the properties file.
Next, we can call the get method of the CommonProperties class to get the path value.

Such as can be used in the Controller class:

@Autowired
protected CommonProperties commonProperties;
........
System.out.println(this.commonProperties.getBasePath());

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326310656&siteId=291194637