0

私はSpringの使用にかなり慣れていません。自動配線の概念は理解していますが、実装について混乱しています。Maven マルチモジュール プロジェクトがあり、モジュール B で使用できるようにモジュール A のマネージャー クラスを Autowire しようとしています。webapp を実行しようとすると、次の行に沿ってエラーが発生します。

No matching bean of type [com..Manager] found for               dependency: expected at least 1 bean which qualifies as             autowire candidate for this dependency.

モジュール A (jar): Manager.java

public interface Manager
{...}

ManagerImpl.java

@Service(value="manager")
public class ManagerImpl implements Manager
{...}

モジュール B (war): WebController.java

@Controller
public class WebController
{

@Autowired
private Manager manager;

..}

applicationContext.xml (モジュール A 内)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <context:component-scan base-package="com....si" />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</beans>
4

3 に答える 3

1

<context:annotation-config/>applicationContext.xml に追加してみてください。プロジェクトの完全なスキャンを確実にするために、常に両方を含めてきました。さらに、コンポーネント スキャンの基本パッケージ内でオートワイヤしようとしているフィールドはありますか?

于 2013-06-27T15:56:49.800 に答える
0

モジュール B にはモジュール A に対する依存関係があり、モジュール A には applicationContext.xml という独自のアプリケーション コンテキスト ファイルがあるため、applicationContext.xml がモジュール A jar にバンドルされてクラスパスに配置されていることを確認します。

ここで、モジュール B の web.xml に ContextLoaderListener を追加する必要があります。

   <context-param>
      <param-name>contextConfigLocation</param-name>
          <param-value>
              classpath*:/<package folder structure in jar>/applicationContext.xml      
          </param-value>
   </context-param>

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

また、xmls 、 applicationContext.xml 、および mvc-dispatcher-servlet.xml の両方に次の行を追加する必要があります

<context:component-scan base-package="<packages for stereotype>" />

これで、豆が注入されるはずです。

于 2013-06-27T16:52:59.777 に答える
0

インターフェイスと実装の間のマッピングを実行するために、最初に「マネージャー」をインスタンス化する必要があります。

<bean id="ManagerBean" class="[package.].ManagerImpl/>

次に、マネージャーと ManagerImpl クラスの間のマッピングを実行するために、「コントローラー」をインスタンス化する必要があります。

<bean id="Controller" class="[package.]WebController.java">
    <property name="manager" ref="ManagerBean" />
</bean>

編集:両方のコードをxmlに配置する必要があります

于 2013-06-27T15:57:56.867 に答える