2017-12-06 12 views
0

私はepServiceとepEntity(dbアクセス用のファクトリクラス)として2つのJavaプロジェクトを持っています。また、コントローラを含むepWebという別のSpringプロジェクトがあり、これはRest APIです。 今、epEntityの中にある春のプロジェクトepWebのクラスをautowireしたいと思います。 私は正常にepWebプロジェクト内のクラスをautowiredしましたが、私は別のプロジェクトからクラスをautowireできませんでした。 誰かがこれを行うことを提案しています。 これは、stackoverflowに関する無関係の質問であれば、これを削除してください。別のプロジェクトにあるクラスをオートワイヤする方法

私は私の暗示が両方のプロジェクトでは、スプリング-config.xmlファイルを作成することです パブリッククラスマッパー{

@Autowired 
private SessionFactory sessionFactory; 

public void setSessionFactory(SessionFactory sessionFactory) { 
    this.sessionFactory = sessionFactory; 
} 
@Autowired 
private AppointmentFactory af; 
@Autowired 
private AppointmentController ac; 
} 

MVC-ディスパッチャ-servlet.xml

<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.2.xsd"> 

    <context:component-scan base-package="com.mobios.ep.web.controllers" /> 
    <!-- <context:component-scan base-package="com.mobios.ep.services" /> 
    <context:component-scan base-package="com.ombios.ep.entity.factory" /> --> 
    <mvc:annotation-driven /> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/pages/"/> 
     <property name="suffix" value=".jsp"/>  
    </bean> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="maxUploadSize" value="1048576"/> 
    </bean> 
</beans> 
+0

同じスプリングアプリケーションコンテキスト内に存在するBeanをオートワイヤすることができます。 2つのプロジェクトが独立して配備されている場合、それぞれに独自のアプリケーションコンテキストがあるため、実行しようとしていることを達成する方法がありません。 – lzagkaretos

+0

答えをありがとう。 epWebプロジェクトは、epServiceとepEntityの両方に依存します。だから、私はepWebだけを配備しました。これは、epWeb(Spring)プロジェクトコードがepEntity – Nishantha

+0

の中のクラスにアクセスできることを意味します。次に、コントローラなどを含む関連パッケージの@ComponentScan(basePackages = {"... package ...."})そこにはたくさんありません、あなたは '@Configuration'クラスでそれらをBeanとして宣言することができます。 – lzagkaretos

答えて

1

あなたは豆としてepEntityプロジェクトに住ん春コンテキスト・クラス(に登録するために、メインアプリケーションepWeb@ComponentScanアノテーションを使用することができますコントローラ、サービスなど)。

@ComponentScan({"com.example.service", "com.example.controlle‌​r"}) 

また、サービスの実装はepEntityプロジェクトに@Serviceでアノテートされていることを確認してください。

+0

それは動作します。助けてくれてありがとう – Nishantha

0

をautowireクラス(あなたはmvc-dispatcher-servlet.xmlという名前を使用しました)とweb.xmlにcontextConfigLocationの両方のファイルをマップして、両方のプロジェクトが同じスプリングコンテキストになるようにしました。

また、スプリングコンフィグファイルには指定のパターンを使用し、contextConfigLocationには正規表現を使用することもできます。

web.xmlのフラグメント:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
     <param-value> 
     classpath:view-spring-config.xml 
     classpath:service-spring-config.xml 
     </param-value> 
    </context-param> 

または

<context-param> 
    <param-name>contextConfigLocation</param-name> 
     <param-value> 
     classpath:*-spring-config.xml 
     </param-value> 
    </context-param> 
関連する問題