繁体   English   中英

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

[英]Spring PropertyPlaceholderConfigurer not passing property values to bean

我有一个非常简单的要求,它变得很复杂,并且我花了一天的时间没有任何运气。 我有一个名为jdbc.properties的属性文件,其中包含数据库连接详细信息。 我需要使用属性文件中的值创建一个数据源连接。 该属性值当前未传递,从而导致数据库连接错误消息。 如果我在Bean中对属性值进行硬编码,那么它将起作用。 我的春季配置文件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();
        }
    }
}

我收到的错误如下所示,是由于未从属性文件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).)

我也遇到了这个问题。 这里的mybatis文档中我找到了发生这种情况的原因。

来源:“注意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>

尝试这个:

<property name="locations" value="classpath:/yourFolderName/jdbc.properties"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM