gpt4 book ai didi

Spring PropertyPlaceholderConfigurer 没有将属性值传递给 bean

转载 作者:行者123 更新时间:2023-12-01 15:17:55 25 4
gpt4 key购买 nike

我有一个非常简单的要求,但它变得复杂了,我花了一天时间却没有任何运气。我有一个名为 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>
${env} 的值作为系统属性传递给类。该类的代码如下:
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MybatisTest {


public static void main(String[] args) {
MybatisTest mybatisTest = new MybatisTest();
mybatisTest.testSelectQuery();
}

public void testSelectQuery(){

String resource = "oneValidation-config.xml";
SqlSession session = null;
try {
ApplicationContext context = new ClassPathXmlApplicationContext("myBatis.DataSource.config.xml");
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)context.getBean("sqlSessionFactory");
session = sqlSessionFactory.openSession(ExecutorType.BATCH);
System.out.println("Test" +
session.selectOne("com.automation.config.PostingMapper.
countByExample",null));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(session != null)
session.close();
}
}
}

我得到的错误如下所示,并且是因为 ${DBUSER} , ${DBPASSWORD}未从属性文件 jdbc.properties 中检索字段:
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).)

最佳答案

我也跑过这个。在 mybatis 文档中 here我找到了为什么会发生这种情况。

来自来源:“注意 sqlSessionFactoryBean 和 sqlSessionTemplateBean 属性是 MyBatis-Spring 1.0.2 之前唯一可用的选项,但鉴于 MapperScannerConfigurer 在启动过程中更早运行,PropertyPlaceholderConfigurer 经常出现错误。为此目的属性已被弃用,并推荐 新属性 sqlSessionFactoryBeanName 和 sqlSessionTemplateBeanName 。”

尝试将您的 MapperScannerConfigurer bean 从

<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>

关于Spring PropertyPlaceholderConfigurer 没有将属性值传递给 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18477336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com