Spring PropertyPlaceHolderConfigure未将属性值传递给bean
我有一个非常简单的要求,它已经变得复杂,我花了一天时间在它上面,没有任何运气。我有一个名为Spring PropertyPlaceHolderConfigure未将属性值传递给bean,spring,mybatis,Spring,Mybatis,我有一个非常简单的要求,它已经变得复杂,我花了一天时间在它上面,没有任何运气。我有一个名为jdbc.properties的属性文件,其中包含数据库连接详细信息。我需要使用属性文件中的值创建一个数据源连接。当前未传递属性值,导致数据库连接错误消息。如果我对bean中的属性值进行硬编码,它就会工作。我的spring配置文件myBatis.DataSource.config.xml如下所示 <?xml version="1.0" encoding="UTF-8"?> <beans d
jdbc.properties
的属性文件,其中包含数据库连接详细信息。我需要使用属性文件中的值创建一个数据源连接。当前未传递属性值,导致数据库连接错误消息。如果我对bean中的属性值进行硬编码,它就会工作。我的spring配置文件myBatis.DataSource.config.xml
如下所示
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="newProperty"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-
init="true">
<property name="locations" value="jdbc.properties.${env}"/>
</bean>
<bean id="newDataSource" class="org.apache.commons.dbcp.BasicDataSource" depends-
on="newProperty" destroy-method="close">
<property name="username" value="${DBUSER}" />
<property name="password" value="${DBPASSWORD}" />
<property name="url" value="${DBURL}" />
<property name="driverClassName" value="${DRIVER}" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="defaultTransactionIsolation" value="2" />
<property name="timeBetweenEvictionRunsMillis" value="10000" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation"
value="com/automation/config/oneValidation-config.xml" />
<property name="dataSource" ref="newDataSource" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.automation.config" />
</bean>
</beans>
我得到的错误如下所示,原因是没有从属性文件jdbc.properties检索${DBUSER}
,${DBPASSWORD}
字段:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC
Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot
create PoolableConnectionFactory (JZ00L: Login failed. Examine the SQLWarnings chained
to this exception for the reason(s).)
试试这个:
<property name="locations" value="classpath:/yourFolderName/jdbc.properties"/>
我也运行过这个。在mybatis文档中,我找到了发生这种情况的原因 来源:“注意sqlSessionFactoryBean和sqlSessionTemplateBean属性是MyBatis Spring 1.0.2之前可用的唯一选项,但鉴于MapperScannerConfigure在启动过程的早期运行,PropertyPlaceHolderConfigure经常出现错误。为此,已弃用属性,建议使用新属性sqlSessionFactoryBeanName和sqlSessionTemplateBeanName 尝试将MapperScannerConfigurer bean从
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.automation.config" />
</bean>
到
嗨,Alex,我刚开始是这样做的,当它不起作用时,我改为上面的。@Vinod Madan试图删除lazy init=“true”
。删除了lazy init。仍然没有运气
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.automation.config" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.automation.config" />
</bean>