【发布时间】: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