如何在春季从外部Jar自动连线@service [英] How to autowire @service from external Jar in Spring

查看:110
本文介绍了如何在春季从外部Jar自动连线@service的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Maven完全在Java配置上开发两个基于Spring的应用程序(Ex app1和app2),并且没有XML配置. 通过Maven -WAR插件,我创建了app2的Jar引用,并使用mvn install:install file将app2的jar与app1绑定在一起. app2-用于从数据源获取信息.

I am developing two spring based application(Ex app1 and app2) fully on Java configuration with Maven and no XML config. Through Maven -WAR plugin , I have created Jar reference of app2 and using mvn install:install file I have binded the app2 jar with app1. app2 - Its used to fetch inforamtion from data source.

我已经自动将app1实现类中的app2服务连接起来,以获取在app2应用程序中用@Service注释的详细信息.

I have autowired the app2 serive in app1 implementation class to fetch the details which was annotated with @Service in app2 application.

我的第一个疑问是:

app1和app2都具有单独的AppConfig.java文件,是否可以自动装配Jar格式的@Service之一,否则我需要将App2的AppConfig java文件定义或导入到App1的AppConfig.jave文件中

Both app1 and app2 have separate AppConfig.java file.Is it possible to simply autowiring one of the @Service which is availble in Jar format or else I need to define or import App2's AppConfig java file into App1's AppConfig.jave file.

我尝试使用简单的自动连接外部JAR @Service类并以错误结束.

I have tried with the simple autowired the external JAR @Service class and ended with error.

请帮助我将外部Jar的@Service自动接线到 实现类.

Kindly help me on what needs to be done to autowire external Jar's @Service to the implementation class.

下面是我的App1存储库类

Below is my App1 repository class

@Repository
public class VehicleRepository {   

    @Autowired  
    VehicleService vehicleservice;   

    public Map<String, item> getAllTypes(String type) {     

            Vehicke vehicle = vehicleservice.getAllVehicle(type);

            // handle response here...
        } catch (Exception ex) {

            // handle exception here
        } finally {

        }
        return vehicleDetails;
    }

}

VehicleService在外部Jar中可用.

VehicleService is available in external Jar.

VehicleService类别:

VehicleService Class:

@Service
public class VehicleService {

    public VehicleService() {
    }

    @Autowired
    PortRepository portRepository;

    @Autowired
    MessageSource messageSource;


    public Vehicle getAllVehicles(String type) {
        List<Vehicle> cehicles = portRepository.getPorts();
        return vehicle;
    }

推荐答案

让我们简化一下.
您的App1取决于App2.
因此,您可以使用 @Import(App2Config.class)类App1Config {} .

Let's make it simple.
Your App1 depends on App2.
So you use @Import(App2Config.class) class App1Config {}, that's it.

顺便说一句,您可以仅将父pom.xml与模块app1和app2一起使用,并在模块中声明模块app1对模块app2的依赖性,而不必使用" mvn install:install file "的技巧. pom.xml< dependencies>部分.然后,您运行' mvn install '来构建您的项目.
在此处查看示例: http://books.sonatype.com/mvnex-book/reference/multimodule.html

And by the way, instead of tricks with 'mvn install:install file' you can just use parent pom.xml with modules app1 and app2, and declare dependency of module app1 on module app2 in pom.xml <dependencies> section. You then run 'mvn install' to build your project.
See an example here: http://books.sonatype.com/mvnex-book/reference/multimodule.html

这篇关于如何在春季从外部Jar自动连线@service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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