spring配置檔案中的使用
阿新 • • 發佈:2019-01-06
spring 3.0 後提供了 註解配置引入,<context:property-placeholder/>,方便。但是往往第一次使用時會遇到此屬性配置進applicationContext.xml檔案中,會出現紅叉:
“The prefix "context" for element "context:property-placeholder" is not bound. ”
只需要在檔案頭中引入:xmlns:context="http://www.springframework.org/schema/context" 即可。
舉例如下:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
jdbc.properties:
#jdbc配置
jdbc.driverClassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@//localhost:1521/orcl
jdbc.username = scotter
jdbc.password = 123
“The prefix "context" for element "context:property-placeholder" is not bound. ”
只需要在檔案頭中引入:xmlns:context="http://www.springframework.org/schema/context" 即可。
舉例如下:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
jdbc.properties:
#jdbc配置
jdbc.driverClassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@//localhost:1521/orcl
jdbc.username = scotter
jdbc.password = 123