1. Use the context:property-placeholder tag to implement configuration file loading
1. Usage example: Add tags in spring.xml configuration file
Copy the code code as follows:<context:property-placeholder ignore-unresolvable="true" location="classpath:redis-key.properties"/>
2. Use configuration file properties in spring.xml:
<!-- Basic properties url, user, password --> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" />3. Use in java files:
@Value("${jdbc_url}") ivate String jdbcUrl; // Note: The variable cannot be defined as static 2. Use the util:properties tag to load configuration files
1. Usage example: Add tags in spring.xml configuration file
Copy the code as follows:<util:properties id="util_Spring" local-override="true" location="classpath:jeesite.properties"/>
2. Use configuration file properties in spring.xml:
<property name="username" value="#{util_Spring['jdbc.username']}" /> <property name="password" value="#{util_Spring['jdbc.password']}" />3. Use in java files:
@Value(value="#{util_Spring['UTIL_SERVICE_ONE']}") private String UTIL_SERVICE_ONE; 3. Implement configuration file loading through @PropertySource annotation
1. Usage example: Use PropertySource annotation in java class files:
@PropertySource(value={"classpath:redis-key.properties"}) public class ReadProperties { @Value(value="${jdbc.username}") private String USER_NAME; } 2. Use in java files:
@Value(value="${jdbc.username}") private String USER_NAME;4. Read configuration files through PropertyPlaceholderConfigurer class
1. Usage example: Use the <bean> tag to configure in spring.xml
<bean> <property name="locations"> <list> <value>classpath:redis-key.properties</value> </list> </property> </bean>
2. PropertyPlaceholderConfigurer configuration method, equivalent to method one, refer to method one for usage.
5. You can also use org.springframework.beans.factory.config.PropertiesFactoryBean to load, and you will not list them one by one here.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.