How to load properties file in xml configuration in spring

In Spring, you can use PropertyPlaceholderConfigurerto load and parse properties files. Here are the steps to load the properties file in the XML configuration:

Suppose we have a jdbc.properties configuration file:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm753as
jdbc.username=root
jdbc.password=123456

contextFirst, introduce the namespace in the XML configuration file as follows:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

PropertyPlaceholderConfigurerThen, add the bean definition in the XML configuration file and specify the path of the properties file to be loaded, for example:

<context:property-placeholder location="classpath:jdbc.properties" />

Finally, you can ${}use the property values ​​​​in the properties file in other bean definitions through the syntax, for example:

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

Guess you like

Origin blog.csdn.net/tyxjolin/article/details/130774637