Skip to content

"NoSuchBeanDefinitionException: No bean named 'xxxxxxx' available" occurred when use together PropertyOverrideConfigurer #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ryou-ks opened this issue Dec 9, 2020 · 9 comments
Assignees

Comments

@ryou-ks
Copy link

ryou-ks commented Dec 9, 2020

there is an error happened when I used mybatis-spring-2.0.5/spring 5.2.6/mybatis3.5.5 as follow:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'targetDataSource' available

How to solve this problem

my spring xml code

<bean>
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
      <property name="locations" />
	<list>
           <value>classpath:mybatis/database.properties<value/>
        <list>
      <property/>
   </bean>

   <bean id="targetDataSource" class="org.springframework.jndi.jndiObjectFactoryBean">

 <beans/>


my properties (database.properties)
targetDataSource.jndiName=aa/bb
@ryou-ks
Copy link
Author

ryou-ks commented Dec 15, 2020

@kazuki43zoo さん
#552
上記のエラーについて、原因と対策がお分かりになりますか?
「class="org.springframework.jndi.jndiObjectFactoryBean"」は初期化されないようです。
PropertyOverrideConfigurerを使わず、下記のような設定する場合問題ないです。

<beans>
   <bean id="targetDataSource" class="org.springframework.jndi.jndiObjectFactoryBean">
      <property name="jndiName" value="aa/bb"></property>
   </bean>
<beans/>

@ryou-ks
Copy link
Author

ryou-ks commented Dec 15, 2020

@pboonphong @harawata @cbegin @h3adache
hello,
How to solve this problem?(#552)
thanks!

@harawata
Copy link
Member

Hello @ryou-ks ,

This seems to be an issue with Spring bean initialization order.
It happens, for example, if you specify sqlSessionFactoryBean instead of sqlSessionFactoryBeanName when configuring MapperScannerConfigurer.
http://mybatis.org/spring/mappers.html
It's difficult to investigate Spring related issues just by looking at a part of the config.
Please provide more information or a small demo project like [1].

PropertyOverrideConfigurer を使わなければ問題ない、ということなので、Spring bean の初期化順の問題だと思います。
ありがちなのは MapperScannerConfigurer の設定で sqlSessionFactoryBeanName ではなく sqlSessionFactoryBean を指定してるケースとかですかね。
http://mybatis.org/spring/ja/mappers.html
Spring 絡みの問題は設定の一部だけ見ても分からないことが多いので、最小限の再現プロジェクト [1] を作るか、できるだけ多くの設定情報をポストしてください。

ここは英語のみですが、ja.stackoverflow.com なら日本語 OK です(MyBatis の質問は少ないのでたまーにしか見てないですが)。teratail の方が活発みたいですね。

[1] https://github.com/harawata/mybatis-issues

@ryou-ks
Copy link
Author

ryou-ks commented Dec 23, 2020

@harawata
こんにちは!ご回答ありがとうございます。
mybatis-spring-2.0.1のjarを使った時に、問題が発生しないです(ほかの変更は無し)。
mybatis-spring-2.0.2以降のjarを使った時は問題が発生します。
2.0.1から2.0.2にリリースしたときに、何かの修正で問題が発生されるかを知りたいです。

できるだけ多くの設定情報をポストしてください。
⇒すみませんが、プロジェクトのセキュリティ管理で、多くの設定情報をポストするのは難しいと思います。
teratailにも投稿してみます。

@harawata
Copy link
Member

Changes in each version are listed here : https://github.com/mybatis/spring/releases

各バージョンの変更点は https://github.com/mybatis/spring/releases にあります。
Spring 側の変更が原因かも知れないので、mybatis-spring-2.0.1 + Spring 5.1.8 とか mybatis-spring-2.0.2 + Spring 5.1.6 の組み合わせでどうなるか試してみるとヒントが得られるかも知れません(互換性があるかどうか分かりませんが)。
あとは 2.0.1 以降で原因となったコミットを二分探索で探していくとかですかね…。

@ryou-ks
Copy link
Author

ryou-ks commented Jan 4, 2021

@harawata さん
ご回答ありがとうございます。
Spring 5.1.8にも同じエラーが発生しました。mybatis-springのコミットを探してみます。

@kazuki43zoo
Copy link
Member

@ryou-ks

Thanks for your report. Probably the following changes are affecting.

I will suggest following two solutions to avoid this issue.

  • Ignore error for property overriding using PropertyOverrideConfigurer#ignoreInvalidKeys=true
  • Disable the property placeholder feature at mapper scanning time using MapperScannerConfigurer#processPropertyPlaceHolders=false

Ignore error for property overriding

Add option to ignore error at property overriding time.

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
    <property name="locations">
        <array>
            <value>classpath:database.properties</value>
        </array>
    </property>
    <property name="ignoreInvalidKeys" value="true"/> <!-- Add this line -->
</bean>

Disable the property placeholder feature

Use the PropertyOverrideConfigurer instead of <mapper:scan> or @MapperScan .

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.example"/>
    <property name="processPropertyPlaceHolders" value="false"/> <!-- can omit this line because default is false -->
    <!-- ... -->
</bean>

@kazuki43zoo kazuki43zoo changed the title NoSuchBeanDefinitionException: No bean named 'xxxxxxx' available "NoSuchBeanDefinitionException: No bean named 'xxxxxxx' available" occurred when use together PropertyOverrideConfigurer Jan 4, 2021
@kazuki43zoo kazuki43zoo self-assigned this Jan 4, 2021
@ryou-ks
Copy link
Author

ryou-ks commented Jan 12, 2021

@kazuki43zoo
thanks for suggesting two solutions.

Ignore error for property overriding using PropertyOverrideConfigurer#ignoreInvalidKeys=true

using this solution can avoid this error, but message "NoSuchBeanDefinitionException: No bean named 'xxxxxxx' available" was still output to the debug level log .

Disable the property placeholder feature at mapper scanning time using
MapperScannerConfigurer#processPropertyPlaceHolders=false

this solution can avoid this error too, "NoSuchBeanDefinitionException: No bean named 'xxxxxxx' available" was no more output.

but I don't know the reason why this error occurred

@kazuki43zoo
Copy link
Member

@ryou-ks
We support new option for disabling the property placeholder feature via #829. You can prevent error with processPropertyPlaceholders option on mybatis:scan or @MapperScan.

but I don't know the reason why this error occurred

The MyBatis-Spring perform a replace placeholder internally at xml element attribute and annotation attribute using Spring's placeholder feature(PropertyResourceConfigurer). The PropertyOverrideConfigurer extended the PropertyResourceConfigurer, therefore MyBatis-Spring apply it to the internally BeanFactory that registered only MapperScannerConfigurer. Then the bean named as 'targetDataSource' that specified in properties file does not exist in the internally BeanFactory, the NoSuchBeanDefinitionException occurred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants