如何自动装配另一个项目中的类

Nis*_*tha 3 spring hibernate spring-mvc

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

我自动装配 public class Mapper {

@Autowired
private SessionFactory sessionFactory;

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

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>
Run Code Online (Sandbox Code Playgroud)

lza*_*tos 5

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

@ComponentScan({"com.example.service", "com.example.controlle??r"})
Run Code Online (Sandbox Code Playgroud)

此外,请确保@ServiceepEntity项目中对服务实现进行了注释。