spring配置文件中<context:property-placeholder />的使用
来源:互联网 发布:unity3d免费版够用吗 编辑:程序博客网 时间:2024/05/21 09:25
context:property-placeholder方便了我们数据库的配置。
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"
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource"
</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
另外需要注意的是,如果遇到下面着着这种问题:
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块的Spring配置文件如下:
- <?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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <context:property-placeholder location="classpath*:conf/conf_a.properties"/>
- <bean class="com.xxx.aaa.Bean1"
- p:driverClassName="${modulea.jdbc.driverClassName}"
- p:url="${modulea.jdbc.url}"
- p:username="${modulea.jdbc.username}"
- p:password="${modulea.jdbc.password}"/>
- </beans>
- modulea.jdbc.driverClassName=com.mysql.jdbc.Driver
- modulea.jdbc.username=cartan
- modulea.jdbc.password=superman
- modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
B模块的Spring配置文件如下:
- <?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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <context:property-placeholder location="classpath*:conf/conf_b.properties"/>
- <bean class="com.xxx.bbb.Bean1"
- p:driverClassName="${moduleb.jdbc.driverClassName}"
- p:url="${moduleb.jdbc.url}"
- p:username="${moduleb.jdbc.username}"
- p:password="${moduleb.jdbc.password}"/>
- </beans>
- moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver
- moduleb.jdbc.username=cartan
- moduleb.jdbc.password=superman
- moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
原因:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描只能定义一个context:property-placeholder,不然就出现那种个错误,那如何来解决上面的问题呢?
A和B模块去掉
- <context:property-placeholder location="classpath*:conf/conf_b.properties"/>
然后重新写个xml:
- <?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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <context:property-placeholder location="classpath*:conf/conf*.properties"/>
- <import resource="a.xml"/>
- <import resource="b.xml"/>
- </beans>
- spring配置文件中<context:property-placeholder />的使用
- Spring配置文件<context:property-placeholder>标签使用
- Spring配置文件<context:property-placeholder>
- spring配置文件<context:property-placeholder>
- spring中<context:property-placeholder>使用
- spring配置文件中util:properties和context:property-placeholder
- spring 配置文件中的占位符 使用 context:property-placeholder
- Spring配置文件<context:property-placeholder>标签使用漫谈
- spring 配置文件中的占位符 使用 context:property-placeholder
- spring 配置文件中的占位符 使用 context:property-placeholder
- spring 配置文件中的占位符 使用 context:property-placeholder
- Spring配置文件<context:property-placeholder>标签使用漫谈
- Spring配置文件<context:property-placeholder>标签使用漫谈
- Spring配置文件<context:property-placeholder>标签使用漫谈
- Spring配置文件<context:property-placeholder>标签使用漫谈
- Spring配置文件<context:property-placeholder>标签使用漫谈
- spring中context:property-placeholder
- spring中context:property-placeholder/
- 详解C中volatile关键字
- Ubuntu14.04安装配置Hadoop2.6.0(完全分布式)与 wordcount实例运行
- Python第四周:函数与递归函数
- iOS 系统图片详解
- sql语句中的 in 、not in 、exists、not exists 详细用法说明和差别----not in失效
- spring配置文件中<context:property-placeholder />的使用
- IOS 通用静态库、Makefile文件使用
- linux内核组件分析之---设备驱动模型之device-driver
- 秒杀多线程第十篇 生产者消费者问题
- 6410之AD驱动与触摸屏共存(下)
- SGU 130 Circle(卡特兰数)
- MySQL免安装版 安装,卸载,找不到指定文件
- 2016年腾讯实习后台开发工程师面试题-- N个骑士,聚会问题
- js表单校验方法(JQuery-Validate使用方法)