如何自动连接另一个项目中的课程 [英] How to autowire a class which is in another project

查看:69
本文介绍了如何自动连接另一个项目中的课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two java projects as epService and epEntity (factory classes for db access). And there is another Spring project as epWeb which contains controllers and this is a Rest API. Now, I want to autowire a class which is inside epEntity to the spring project epWeb. I have successfully autowired classes within that epWeb project but I was unable to autowire a class from another project Anyone have a suggestion for doing that, please let me know. If this is an irrelevant question for stackoverflow, please delete this.

我自动接线的班级 公共类Mapper {

The class where I autowire public class Mapper {

@Autowired
private SessionFactory sessionFactory;

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

mvc-dispatcher-servlet.xml

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>

推荐答案

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

You can use @ComponentScan annotation in your main application epWeb in order to register in Spring context classes which live in epEntity project as Beans (Controllers, Services etc).

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

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

Also, make sure that Service implementations are annotated with @Service in epEntity project.

这篇关于如何自动连接另一个项目中的课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆