Spring propertyConfigurer not working

I had the following problem: it seems that the injection properties of the propertyConfigurer property are not working.

The error I get is ...

Caused by: java.lang.NumberFormatException: For input string: "${db.maxactive}" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:449) at java.lang.Integer.valueOf(Integer.java:554) at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:155) at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115) at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:434) at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:406) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:163) at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470) 

From stacktrace, he tries to enter the value "$ {db.maxactive}" into the dbcp driver. If I turn on logging, I see the following (this is a stacktrace when I do not insert this property) ...

 [INFO] Refreshing org .springframework.context.support.ClassPathXmlApplicationContext@ 19bd03e: startup date [Thu Jun 27 08:58:08 BST 2013]; root of context hierarchy [INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-StandAlone.xml] [INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-monitoring.xml] [INFO] Loading XML bean definitions from class path resource [conf/spring/dataAccessContext-local.xml] [INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-jdbc.xml] [INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-beans.xml] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/Users/porterj/.m2/repository/org/slf4j/slf4j-jdk14/1.6.1/slf4j-jdk14-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/C:/Users/porterj/.m2/repository/org/slf4j/slf4j-nop/1.6.1/slf4j-nop-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 

**** An exception is thrown HERE before jdbc.properties loads. [INFO] Loading a properties file from a class path resource [jdbc.properties]

This makes me think that the Configurer property is loaded after it tries to enter a value, so it enters the parameter name, not the parameter value.

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="false"/> <property name="locations"> <list> <value>classpath:/jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> <property name="maxActive" value="${db.maxactive}"/> </bean> 

Then my jdbc.properties file ...

 db.driver=com.ibm.db2.jcc.DB2Driver db.url=jdbc:db2://dbserver:51000/db db.username=dm db.password=pass db.maxactive=20 

Java class ...

 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext-StandAlone.xml"); 

Can someone give me some advice, is it because I am doing this from a standalone application? I have done this many times in a web application, and propertyConfigurer works fine.

Note: Spring 3.1.2.RELEASE

  <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> 
+4
source share
4 answers

I saw this before in the application I was developing; I found that the problem is with mybatis and there was a problem with SqlSessionFactoryBean. I haven’t dug too deeply, but if you want to learn to start a little more with this one, it can be something like that, even if you are not using mybatis.

I could see that my beans were getting an instance and the properties set for them before loading the properties from the file.

Instead of using the Spring PropertyPlaceholderConfigurer, I had to use the Spring "util: properties" mechanism to load properties - as soon as I got to this, it all started to work. Give something like the code below try - may work for you ...

 <?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <util:properties id="dataSourceProps" location="classpath:jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="#{dataSourceProps['db.driver']}" /> <property name="url" value="#{dataSourceProps['db.url']}" /> <property name="username" value="#{dataSourceProps['db.username']}" /> <property name="password" value="#{dataSourceProps['db.password']}" /> <property name="maxActive" value="#{dataSourceProps['db.maxactive']}" /> </bean> .... </beans> 
+2
source

I think this may be a problem finding the jdbc.properties file. He complains about ${db.maxactive} , but probably if you had hardcoded this value, he started complaining about the following.

Try using classpath*: in locating the properties file:

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="false"/> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> </list> </property> </bean> 

You can find more information about using wildcards in the resource path here .

+2
source

The following code worked for me. See if it helps you.

 <!-- Remove id of bean --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"><value>${db.driver}</value></property> </bean> 
+1
source
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。可指定多个包,包与包之间用逗号或分号分隔--> <property name="basePackage" value="com.hebeu.persist" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> 

the attribute of the element must be a value not ref I solve this on the same problem

0
source

Source: https://habr.com/ru/post/1488441/


All Articles