1

spring 3.1.2.RELEASE で spring mvc を使用して Web アプリを作成しようとしていますが、アプリケーションにアクセスしようとするとエラーが発生します。

アプリは tomcat 7 で実行されています。

ダオ:

@Repository
public class CustomerDao extends JdbcDaoSupport{
    @Autowired
    public CustomerDao(DataSource dataSource){
        super();
        setDataSource(dataSource);
    }
    public void teste(){
        System.out.println("teste");
    }
}

Spring MVC サーブレット

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org  /2001/XMLSchema-instance"
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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="org.samples.example1" />

<mvc:resources mapping="/resources/**" location="/resources/" />

<bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
              <value>/WEB-INF/views/</value>
           </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
    </bean>

</beans>

アプリケーションのコンテキスト:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

    <context:annotation-config />
    <context:component-scan base-package="org.samples.example1" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/exemplo" />
        <property name="username" value="root" />
        <property name="password" value="root"/>
    </bean>

    <bean id="customerDao" class="org.samples.example1.repository.CustomerDao">
        <constructor-arg ref="dataSource" />
    </bean>

</beans>

何が間違っている可能性がありますか?

エラー:

根本的な原因:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
4

2 に答える 2

1

その理由はおそらく、Web アプリケーション コンテキストの次の行 (DispatcherServlet に対して登録されたもの) です。

<context:component-scan base-package="org.samples.example1" />

これは CustomerDao インスタンスのインスタンスを作成しようとし、Datasource は Web アプリケーション コンテキストのレベルでは利用できません。

代わりに、Web コンテキストでのコンポーネント スキャンを次のように @Controllers のみに制限します。

<context:component-scan base-package="org.samples.example1" >
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
于 2013-02-15T04:26:14.237 に答える
0

ファイル web.xml にファイル applicationContext.xml のマッピングを追加するのを忘れていたことに気付きました

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>Exemplo 01</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>exemplo1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
    </servlet>

    <servlet-mapping>
        <servlet-name>exemplo1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

以下のタグが web.xml にありませんでした

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

この変更を行っても、Biju のアドバイスに従って、Web コントローラーの xml を次のように変更する必要がありました。

<!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
<context:component-scan base-package="org.samples.example1" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

applicationContext.xml で、次のことを許可します。

<context:component-scan base-package="org.samples.example1" />

今、アプリケーションは動作しています:)

于 2013-02-16T02:32:22.893 に答える