Java

【SpringMVC】エラー対処「No qualifying bean of type」や「expected at least 1 bean which … for this dependency」

SpringMVCでTomcat起動時に[No qualifying bean of type]や[expected at least 1 bean which qualifies as autowire candidate for this dependency]のエラーが出たのでその原因を調査。

対象のバージョンは「springframework-version:3.2.2」なので化石みたいなもので需要はないかもしれないが、自分はまだバリバリ使っている環境にいるので一様メモ。

 

原因

アノテーション(@)の設定が漏れていたのが原因!

  • 各@Component,@Controller,@Service,@Repositoryの記載が間違っている
  • アノテーション関連のパッケージ構成が間違っている

SpringMVCのが場合は上記の2つのどちらかだと思う。

設定ファイル「service-context.xml」に記載されいてる、アノテーションの読み込み対象にいれるよう変更。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<bean id="passwordEncoder"
class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
<constructor-arg value="256" />
</bean>

<tx:annotation-driven />
<context:component-scan base-package="jp.co.×××.service.spi,jp.co.×××.repository.spi" />
・・・[アノテーション対象のパッケージを追記する]・・・
</beans>

以上です。

-Java