【问题标题】:How to autowire a class which is in another project如何自动装配另一个项目中的类
【发布时间】:2017-12-06 05:15:28
【问题描述】:

我有两个 java 项目,分别是 epService 和 epEntity(用于数据库访问的工厂类)。还有另一个 Spring 项目为 epWeb,其中包含控制器,这是一个 Rest API。 现在,我想将 epEntity 内部的一个类自动连接到 spring 项目 epWeb。 我已经在那个 epWeb 项目中成功地自动装配了类,但是我无法从另一个项目中自动装配一个类 任何人有这样做的建议,请告诉我。 如果这与 stackoverflow 无关,请删除它。

我自动装配的类 公共类映射器 {

@Autowired
private SessionFactory sessionFactory;

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

mvc-dispatcher-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>

【问题讨论】:

  • 您可以自动装配存在于同一 Spring 应用程序上下文中的 bean。如果两个项目是独立部署的,每个项目都有自己的应用程序上下文,因此,没有办法实现您想要做的事情。
  • 感谢您的回答。 epWeb 项目同时依赖于 epService 和 epEntity。所以,我只部署了 epWeb。这意味着 epWeb (Spring) 项目代码可以访问 epEntity 中的类
  • 好的,那么你可以使用包含Controller等相关包的@ComponentScan(basePackages = {"...package...."}),或者如果数量不多,你可以将它们声明为@Configuration类中的Bean。
  • 我尝试将 @ComponentScan(basePackages = {"...package...."}) 添加到 epService 内部的类中,但它仍然无法正常工作。它给了我以下错误。创建名为“appointmentController”的 bean 时出错:通过字段“iService”表达的不满足依赖关系:没有符合条件的 bean 类型等
  • 你确定了iService(实现)所在的包吗?您也必须将此添加到@ComponentScan@ComponentScan({"com.example.service","com.example.controller"})

标签: spring hibernate spring-mvc


【解决方案1】:

您可以在主应用程序epWeb 中使用@ComponentScan 注释,以便在epEntity 项目中作为Bean(控制器、服务等)的Spring 上下文类中注册。

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

另外,请确保在 epEntity 项目中使用 @Service 注释服务实现。

【讨论】:

    【解决方案2】:

    我的建议是在两个项目中为自己的 spring bean 配置创建一个 spring-config.xml 文件(您使用名称 mvc-dispatcher-servlet.xml)并在 web.xml 上为 contextConfigLocation 映射两个文件,所以两者项目将在相同的春季环境中。

    您还可以对 spring 配置文件使用指定的模式,并为 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2022-01-23
      • 2012-07-08
      相关资源
      最近更新 更多