- 浏览: 366652 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (225)
- java (76)
- English (2)
- office (2)
- 架构设计 (1)
- 人在职场 (3)
- database (21)
- C# (18)
- 向往的院校 (0)
- C++ (1)
- AJAX (1)
- 操作系统 (8)
- eclipse (1)
- Spring (0)
- Linux (1)
- Javascript (6)
- 离散 (0)
- 协议 (1)
- sql server (5)
- sql server (0)
- fdf (0)
- xml (1)
- sql语句里top和distinct一起用 (1)
- 正则表达式 (7)
- 表达 (1)
- ms server (1)
- SWFObject (0)
- 线程 (2)
- Java线程 (0)
- Android & SQLite (0)
- Android (0)
- window.XMLHttpRequest (0)
- DB pool (0)
- tomcat内存溢出设置JAVA_OPTS (1)
- java bcp (1)
- 文件系统转换 (1)
- Microsoft XMLDom (1)
- tree (0)
- C# installer (0)
- 对付http cc攻击 (0)
- Ckeditor (0)
- MyEclipse (0)
- PDM (0)
- PDM OOM (0)
- asp.net (0)
- tomcat (1)
- Tomcat session (1)
- jdk (0)
- Bootstrap (0)
最新评论
-
kenail:
格式太乱了。
转 使用WebService压缩传输的心得 -
存在即为合理:
写得好乱,但是还是谢谢你的驱动
JDBC连接SQL server 2005 驱动 -
guji528:
长见识了,谢谢分享!
TL1协议(正文信息收集整理来源Internet) -
王大人:
Js window confirm()方法及其使用 -
Eastman:
SqlServer附加数据库出错,错误代码5123
在applicationContext.xml里配置DBCP数据链接池时,dbcp.properties
dbcp.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
dbcp.url = jdbc:sqlserver://192.168.1.132:1433;DatabaseName=HotelDB
dbcp.username = YXCK
dbcp.password = 123456
#连接池的最大数据库连接数,设为0表示无限制。
dbcp.maxActive = 5
#数据库连接的最大空闲时间。超过此空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
dbcp.maxIdle = 30
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
dbcp.maxWait = 10000
dbcp.defaultAutoCommit = true
#回收被遗弃的(一般是忘了释放的)数据库连接到连接池中。
dbcp.removeAbandoned = true
# 数据库连接过多长时间不用将被视为被遗弃而收回连接池中。
dbcp.removeAbandonedTimeout = 30
# 将被遗弃的数据库连接的回收记入日志。
dbcp.logAbandoned = true
applicationContext.xml配置
<!-- 定义数据源Bean,使用DBCP数据源实现 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dbcp.driverClassName}" />
<property name="url" value="${dbcp.url}" />
<property name="username" value="${dbcp.username}" />
<property name="password" value="${dbcp.password}" />
<property name="maxActive" value="30"/><!-- value="${jdbc.maxActive}" /> -->
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="maxWait" value="${dbcp.maxWait}" />
<property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}"/>
<property name="removeAbandoned" value="${dbcp.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${dbcp.removeAbandonedTimeout}"/>
<property name="logAbandoned" value="${dbcp.logAbandoned}"/>
</bean>以${}方式读取properties
文件里的配置,当为int类型时出现下面的错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'maxActive'; nested exception is java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'maxActive'; nested exception is java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
... 29 more
Caused by: java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.valueOf(Integer.java:553)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:157)
at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:114)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:382)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:358)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:173)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
... 33 more
发现时不能正确读取int类型的原因,经过变化关键词“applicationContext文件以${}方式读取properties文件的信息”搜索到,在Spring - applicationContext.xml配置文件中读取properties配置文件的内容可以使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类。哈哈,看到希望继续变化关键词“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”结果找到
http://thomaslee007.iteye.com/blog/167762
下面是其内容
Spring框架下PropertyPlaceholderConfigurer类
它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中.
要了解这个类首先要弄清楚一个概念:bean factory post-processor
官方解释是这样的:
A bean factory post-processor is a java class which implements the
org.springframework.beans.factory.config.BeanFactoryPostProcessor interface. It is executed manually (in the case of the BeanFactory) or automatically (in the case of the ApplicationContext) to apply changes of some sort to an entire BeanFactory, after it has been constructed.
我理解的意思是这样的:
1.首先bean factory post-processor实现了org.springframework.beans.factory.config.BeanFactoryPostProcessor接口。
2.在BeanFactory的情况下它被手动的执行。
3.在ApplicationContext的条件下它会自动的执行。
4.最关键的一点是,是在一个类的实例被构造出来之后,对整个BeanFactory进行修改。
--------------------------------------------------------------------------------------------------------------------------------
Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。藉由这个类别,您可以将一些组态设定,移出至.properties档案中,如此的安排可以让XML定义档负责系统相关设定,而.properties档可以作为客户根据需求,自定义一些相关的参数。
来看一个Bean定义档的实际例子:
beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>hello.properties</value>
</property>
</bean>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>${onlyfun.caterpillar.helloWord}</value>
</property>
</bean>
</beans>
假设在helloBean中有许多依赖注入的属性,这些都是比较不常变动的属性,而其中helloWord会经常变动,可以透过hello.properties来简单的设定,而这个资讯已设定在configBean的location属性中:
hello.properties
onlyfun.caterpillar.helloWord=Welcome!
在helloBean的helloWord属性中,${onlyfun.caterpillar.helloWord}将会被hello.properties的helloWord所取代。
如果有多个.properties档案,则可以透过locations属性来设定,例如:
beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>hello.properties</value>
<value>welcome.properties</value>
<value>other.properties</value>
</list>
</property>
</bean>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>${onlyfun.caterpillar.helloWord}</value>
</property>
...
</bean>
</beans> ======================================PropertyPlaceholderConfigurer类就是bean factory post-processor的一种,它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中。
例如---spring-context.xml----
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="url"><value>jdbc:${dbname}</value></property>
</bean>而实际的jdbc.propertis文件是
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
而jdbc.propertis是怎样引用的呢: 将上边一段配置注册在web.xml中就可以了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
当然,不要忘了spring的监听器注册
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
这样,一个简单的数据源就设置完毕了。
实际上,PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义
的工具。
这样真正的原因明白了,需要PropertyPlaceholderConfigurer类,在applicationContext.xml加上
<!-- 配置PropertyPlaceholderConfigurer,将占位符指向的数据库配置信息放在bean中定义 -->
<bean name="customName" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="location">
<value>/WEB-INF/dbcp.properties</value>
</property>
</bean>
问题解决。
dbcp.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
dbcp.url = jdbc:sqlserver://192.168.1.132:1433;DatabaseName=HotelDB
dbcp.username = YXCK
dbcp.password = 123456
#连接池的最大数据库连接数,设为0表示无限制。
dbcp.maxActive = 5
#数据库连接的最大空闲时间。超过此空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
dbcp.maxIdle = 30
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
dbcp.maxWait = 10000
dbcp.defaultAutoCommit = true
#回收被遗弃的(一般是忘了释放的)数据库连接到连接池中。
dbcp.removeAbandoned = true
# 数据库连接过多长时间不用将被视为被遗弃而收回连接池中。
dbcp.removeAbandonedTimeout = 30
# 将被遗弃的数据库连接的回收记入日志。
dbcp.logAbandoned = true
applicationContext.xml配置
<!-- 定义数据源Bean,使用DBCP数据源实现 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${dbcp.driverClassName}" />
<property name="url" value="${dbcp.url}" />
<property name="username" value="${dbcp.username}" />
<property name="password" value="${dbcp.password}" />
<property name="maxActive" value="30"/><!-- value="${jdbc.maxActive}" /> -->
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="maxWait" value="${dbcp.maxWait}" />
<property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}"/>
<property name="removeAbandoned" value="${dbcp.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${dbcp.removeAbandonedTimeout}"/>
<property name="logAbandoned" value="${dbcp.logAbandoned}"/>
</bean>以${}方式读取properties
文件里的配置,当为int类型时出现下面的错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'maxActive'; nested exception is java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'maxActive'; nested exception is java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
... 29 more
Caused by: java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.valueOf(Integer.java:553)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:157)
at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:114)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:382)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:358)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:173)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
... 33 more
发现时不能正确读取int类型的原因,经过变化关键词“applicationContext文件以${}方式读取properties文件的信息”搜索到,在Spring - applicationContext.xml配置文件中读取properties配置文件的内容可以使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类。哈哈,看到希望继续变化关键词“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”结果找到
http://thomaslee007.iteye.com/blog/167762
下面是其内容
Spring框架下PropertyPlaceholderConfigurer类
它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中.
要了解这个类首先要弄清楚一个概念:bean factory post-processor
官方解释是这样的:
A bean factory post-processor is a java class which implements the
org.springframework.beans.factory.config.BeanFactoryPostProcessor interface. It is executed manually (in the case of the BeanFactory) or automatically (in the case of the ApplicationContext) to apply changes of some sort to an entire BeanFactory, after it has been constructed.
我理解的意思是这样的:
1.首先bean factory post-processor实现了org.springframework.beans.factory.config.BeanFactoryPostProcessor接口。
2.在BeanFactory的情况下它被手动的执行。
3.在ApplicationContext的条件下它会自动的执行。
4.最关键的一点是,是在一个类的实例被构造出来之后,对整个BeanFactory进行修改。
--------------------------------------------------------------------------------------------------------------------------------
Spring的框架中为您提供了一个 BeanFactoryPostProcessor 的实作类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。藉由这个类别,您可以将一些组态设定,移出至.properties档案中,如此的安排可以让XML定义档负责系统相关设定,而.properties档可以作为客户根据需求,自定义一些相关的参数。
来看一个Bean定义档的实际例子:
beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>hello.properties</value>
</property>
</bean>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>${onlyfun.caterpillar.helloWord}</value>
</property>
</bean>
</beans>
假设在helloBean中有许多依赖注入的属性,这些都是比较不常变动的属性,而其中helloWord会经常变动,可以透过hello.properties来简单的设定,而这个资讯已设定在configBean的location属性中:
hello.properties
onlyfun.caterpillar.helloWord=Welcome!
在helloBean的helloWord属性中,${onlyfun.caterpillar.helloWord}将会被hello.properties的helloWord所取代。
如果有多个.properties档案,则可以透过locations属性来设定,例如:
beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>hello.properties</value>
<value>welcome.properties</value>
<value>other.properties</value>
</list>
</property>
</bean>
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
<property name="helloWord">
<value>${onlyfun.caterpillar.helloWord}</value>
</property>
...
</bean>
</beans> ======================================PropertyPlaceholderConfigurer类就是bean factory post-processor的一种,它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中。
例如---spring-context.xml----
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="url"><value>jdbc:${dbname}</value></property>
</bean>而实际的jdbc.propertis文件是
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
而jdbc.propertis是怎样引用的呢: 将上边一段配置注册在web.xml中就可以了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
当然,不要忘了spring的监听器注册
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
这样,一个简单的数据源就设置完毕了。
实际上,PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义
的工具。
这样真正的原因明白了,需要PropertyPlaceholderConfigurer类,在applicationContext.xml加上
<!-- 配置PropertyPlaceholderConfigurer,将占位符指向的数据库配置信息放在bean中定义 -->
<bean name="customName" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="location">
<value>/WEB-INF/dbcp.properties</value>
</property>
</bean>
问题解决。
发表评论
-
tomcat安装不成功.提示是:failed to install tomcat6 service ,check your setting and permis
2018-03-08 14:55 405以管理员身份运行 命令提示符,弹出窗口 ,选择“是”,输入 ... -
把系统时间设置成跟数据库的一致
2016-08-22 16:41 0public String time(int x) { ... -
struts标签<logic:iterate>的用法
2016-01-08 16:17 0<logic:iterate>主要用来 ... -
WIN7环境下cmd javac不是内部或外部命令 .
2015-07-21 11:27 1190一般步骤如下: 网上摘抄部分: JAVA_HOME ... -
jdk环境变量配置
2014-08-25 11:01 0进行java开发,首先要安装jdk,安装了jdk后还要进行环境 ... -
[转]JDBC使用TNS连接多节点Oracle
2012-06-29 15:15 1172JDBC使用TNS连接多节点O ... -
一个简单的JDBC通用工具
2012-06-29 15:01 0一个简单的JDBC通用工具 支持多种数据库,统一方式产 ... -
Java调用BCP导入数据到数据库解决标识列ID问题
2012-06-29 14:53 1083面的一篇博文讲解了调用bcp批量导出数据,对于批量导入数据则写 ... -
java 可变参数方法Object... objs
2012-06-29 14:42 4152public abstract List find(Str ... -
java调用存储过程
2012-06-14 12:34 0在java可以使用java.sql.CallableState ... -
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接 错误解决办法
2012-06-13 12:56 3036用java连接sqlserver2005时总是出现下面这个错误 ... -
javac编译外部jar包
2012-06-12 14:23 3553这个有个很简单到解决 ... -
[转]Eclipse中将Java项目(引用了第三方包) 打包为jar
2012-06-12 14:13 988如果自己的java project中需要引用额外的jar包作为 ... -
java 调用BCP导入文本数据到表
2012-06-04 15:53 0在dos下的导入语句bcp SMM_SQL_REPLICA.d ... -
[转]jdk和jre有什么区别?
2012-03-09 14:28 769来源 简单的说JDK是面 ... -
[转]Java线程:线程栈模型与线程的变量
2012-02-16 14:06 740Java线程:线程栈模型与线程的变量 SCJP5学 ... -
[转] Java线程:概念与原理
2012-02-16 13:29 793Java线程:概念与原理 ... -
Java线程:创建与启动
2012-02-16 13:26 497SCJP5学习笔记 一、定义线程 ... -
JSP页面用get传递参数乱码问题
2011-06-24 15:52 1240通过get 方式传递参数时,如果参数是中文 ,则会出现乱码现在 ... -
PO/VO/DAO/BO/POJO是什么(JAVA几种对象的解释)
2011-03-30 16:49 0/*PO:persistant object持久对 ...
相关推荐
Spring属性占位符PropertyPlaceholderConfigurer的使用,自己写的一个demo
Spring中PropertyPlaceholderConfigurer的使用
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
ApplicationContext 除了具备如 BeanFactory 基本的容器管理功能之外,并支援更多应用程式框架的特性,像是资源的取得、讯息解析、事件的处理与传播。 Resource 的取得 解析文字讯息 倾听事件 ...
设置索引器属性 4.3.3.5.内联对象定义 4.3.3.6.idref节点 4.3.3.7.引用协作对象 4.3.3.8.value和ref节点的简短格式 4.3.3.9.复合属性名 4.3.4.方法注入 4.3.4.1.查询方法注入 4.3.4.2.替换任意方法 4.3.5.引用其他...
4.7.2. Application context构造器中资源路径的通配符 4.7.2.1. Ant风格的pattern 4.7.2.2. classpath*: 前缀 4.7.2.3. 其他关于通配符的说明 4.7.3. FileSystemResource 提示 5. 校验,数据绑定,BeanWrapper,与...
主要介绍了Spring如何使用PropertyPlaceholderConfigurer读取文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
前几天搞 Spring + Hibernate + Struts 事务配置 ,网上找了好多资料,不过好无语,大多都是 Ctrl + V,浪费俺的宝贵时间 现在我总结配出一套,给大家参考参考,可能有不足,请大家多多交流。 附:内有弹出...
NULL 博文链接:https://chyy001.iteye.com/blog/1338488
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:/deploy.properties <!-- 配置sessionFactory ...
第一部分 Spring framework 概述..5 第1 章 Spring Framework 介绍..6 1.1 依赖注入和控制反转.6 1.2 模块6 1.2.1 核心容器.7 1.2.2 数据访问/ 整合..7 1.2.3 Web ..8 1.2.4 AOP...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:jdbc.properties ${driver}"/> ${url}"/> ${username}"/> ${password}"/> <!-- 配置...
与通过从属性文件读取其配置来进行自我配置相比,对于应用程序而言,有什么可能更简单? 好吧,对于业务应用程序而言,事实并非如此简单! 出现的问题如下:0.如何根据执行平台读取不同的属性? 0.如何保护敏感值,...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath*:jdbc.properties <!-- 数据源配置,主要用于开发测试...
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
PropertyPlaceholderConfigurer PropertyResourceConfigurer PropertyValue PropertyValues PropertyValuesEditor PrototypeAspectInstanceFactory PrototypeTargetSource ProxyConfig ProxyFactory ...