LoginSignup
1
1

More than 5 years have passed since last update.

Spring4 mybatis連携 property-placeholderが動作しない

Last updated at Posted at 2017-08-09

環境

windows10
tomcat8
java8
spring4.1.6
mybatis3.3.0
mybatis-spring1.2.3

現象

aplicationContext.xmlを以下のように記述して、tomcat起動しDB接続を行った。

aplicationContext.xml
<context:property-placeholder location="classpath:xxx.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp2.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}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="jp.co.ricoh.rj.embs.mapper" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

結果、DB接続部でエラー
ドライバ 「${db.driver}」が見つからない旨のログが出ているので、placeholderが正常に動作せず
文字が置換されてないと思われる。placeholderに指定したファイルが読み込めているのは確認済み。
javaのconfigで@Valueで値は取得できた。

いろいろ試しても解決できないのでMapperScannerConfigureの記述を変更した

aplicationContext.xml
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="jp.co.ricoh.rj.embs.mapper" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean><mybatis:scan base-package="jp.co.ricoh.rj.embs.mapper" />

解決、、、

別の解決方法についてこちらにヒントがあった
https://stackoverflow.com/questions/17512596/contextproperty-placeholder-doesnt-resolve-references

default-autowire="byName"を外すとMapperScannerConfigurerで問題なく動く

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1