If some parameters in the project need to be modified frequently, or may need to be modified later, then it is best to put these parameters into the properties file and read the configuration in the properties in the source code. In this way, only the properties file needs to be modified later, and there is no need to modify the source code, which is more convenient. This can also be done in Spring, and Spring has two ways to load properties files: based on the xml method and based on the annotation method . The following are the two methods.
1. Load properties file through xml
Let’s take Spring instantiated dataSource as an example. We generally configure the following in the beans.xml file:
<!-- com.mchange.v2.c3p0.ComboPooledDataSource class is in the com.mchange.v2.c3p0 package of c3p0-0.9.5.1.jar package --> <bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shop" /> <property name="user" value="root" /> <property name="password" value="root" /> </bean>
Now if we want to change the dataSource, we have to modify these source codes, but if we use properties files, we only need to modify the source code, and we don’t care about the source code. So how to do it?
There is a <context:property-placeholder location=""/> tag in Spring, which can be used to load properties configuration files. Location is the path to the configuration file. We now create a new conn.properties file under the src of the project directory, and write the above dataSource configuration:
dataSource=com.mchange.v2.c3p0.ComboPooledDataSource driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc/:mysql/://localhost/:3306/shop user=root password=root
Now you only need to make the following modifications in beans.xml:
<context:property-placeholder location="classpath:conn.properties"/><!-- Loading configuration file --> <!-- com.mchange.v2.c3p0.ComboPooledDataSource class is in the com.mchange.v2.c3p0 package of c3p0-0.9.5.1.jar package --> <bean id="dataSource"> <!-- These configurations are found in conn.properties at startup --> <property name="driverClass" value="${driverClass}" /> <property name="jdbcUrl" value="${jdbcUrl}" /> <property name="jdbcUrl" value="${jdbcUrl}" /> <property name="user" value="${user}" /> <property name="password" value="${password}" /> </bean> <context:property-placeholder location=""/> tags can also be replaced by the following <bean> tags. We are more familiar with the <bean> tags and are more readable: <!-- equivalent to the above configuration, the following is easier to understand --> <bean> <property name="locations"> <!-- There is a locations property in the PropertyPlaceholderConfigurer class, which receives an array, that is, we can assign multiple properties files below--> <array> <value>classpath:conn.properties</value> </array> </property> </bean> Although it seems to be as concise as the above <context:property-placeholder location=""/> , it is clearer. It is recommended to use the following one. However, this is limited to xml, that is, use ${key} to get the value in the configuration file in beans.xml.
2. Load properties file through annotation
Another way is to use @Value annotation to load the values in the configuration file through annotation.
Let's take an example: If we want to get the absolute path to a certain file in the program, we will naturally think that we cannot write it dead in the program, then we can also uninstall the properties file. Or create a new public.properties file in the src directory, assuming that a record is written in it:
filePath=E/://web//apache-tomcat-8.0.26//webapps//E_shop//image
If you want to get this filePath through annotations in Java code, you must first configure the annotation method in the beans.xml file:
<!-- The second method is to use annotation injection, which is mainly used in java code to inject the corresponding value value in the properties file using annotations--> <bean id="prop"> <property name="locations"><!-- This is the PropertiesFactoryBean class. It also has a locations property and also receives an array, just like the above <array> <value>classpath:public.properties</value> </array> </property> </bean>
Now we can use annotations in the java code to get the value of filePath:
@Component("fileUpload") public class FileUploadUtil implements FileUpload { private String filePath; @Value("#{prop.filePath}") //@Value means to find the bean with id="prop" in the beans.xml file. It reads the properties configuration file through annotation, and then reads the corresponding value value of key=filePath in the corresponding configuration file public void setFilePath(String filePath) { System.out.println(filePath); this.filePath = filePath; }Note that there must be a set method to be injected, and the annotation can be written on the set method. In the setFilePath method, the filePath is printed through the console to observe whether the console has output when starting tomcat. If so, it means that Spring has loaded the filePath when it is started. Let's take a look at the startup information of the console:
The above are two ways Spring loads properties configuration files. In fact, the PropertyPlaceholderConfigurer class above based on the xml method and the PropertiesFactoryBean class here based on the annotation method both inherit the PropertiesLoaderSupport and are both used to load the properties configuration file.
Summarize
The above is a detailed explanation of the two examples of Spring loading properties files introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!