2013-08-28 3 views
2

저는 매우 단순한 요구 사항이 복잡해졌으며, 행운이없이 하루를 보냈습니다. DB 연결 세부 정보가있는 jdbc.properties라는 속성 파일이 있습니다. 속성 파일의 값을 사용하여 데이터 소스 연결을 만들어야합니다. 속성 값이 전달되어 DB 연결 오류 메시지가 나타납니다. 빈에서 속성 값을 하드 코딩하면 작동합니다. 내 봄 설정 파일 인 myBatis.DataSource.config.xml은 다음과 같다. $ {ENV}에 대한스프링 PropertyPlaceholderConfigurer가 빈에 속성 값을 전달하지 않습니다.

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

값은 클래스에 시스템 속성으로 전달됩니다. 클래스의 코드는 다음과 같습니다 :

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

답변

0

이 시도 :

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

안녕 알렉스에

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="basePackage" value="com.automation.config" /> </bean> 

에서 MapperScannerConfigurer 콩을 변경 시도하고 일을 일부러 할 때, 나는 위로 변경되었습니다. –

+0

@Vinod Madan'lazy-init = "true"'도 제거하십시오. – Alex

+0

lazy-init가 제거되었습니다. 아직도 운이 없다. –

1

은 나뿐만 아니라이 건너 달렸다. mybatis 문서 here에서 나는 이것이 일어나는 이유를 발견했습니다. 소스에서

: "주 sqlSessionFactoryBean 및 sqlSessionTemplateBean 속성은 MyBatis로 - 봄 1.0.2까지 사용할 수 있지만 MapperScannerConfigurer 이전 PropertyPlaceholderConfigurer와 자주 오류가 있었다 시작 프로세스에서 실행 주어진 유일한 선택이었다 그 목적을 위해. 해당 속성은 더 이상 사용되지 않으며 새 속성 인 sqlSessionFactoryBeanName 및 sqlSessionTemplateBeanName은을 권장합니다. "

내가 그 처음에하고 있던,

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
    <property name="basePackage" value="com.automation.config" /> 
</bean> 
관련 문제