我们可以使用Spring Boot实现Java库吗? [英] Can we implement a Java library by using Spring Boot?

查看:106
本文介绍了我们可以使用Spring Boot实现Java库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在线程名称的指导下,我想使用Spring Boot创建一个JAVA库。我找到了这个帖子:使用Spring启动创建库jar 。但是,该线程的目标似乎是通过将其实现为REST API来解决的。

As guided by the thread's name, I would like to create a JAVA library by using Spring Boot. I have found this thread: Creating a library jar using Spring boot. However, that thread's objectives seem to be solved by implementing it as a REST API.

目前,我正在使用Spring Boot开发基于Spring的JAVA库。并且,我已经尝试打包为jar文件,让其他JAVA应用程序在JAVA库中使用它。不幸的是,我发现当调用者应用程序调用添加的库的某些方法时,库中定义的配置根本不起作用
它还显示 CommandLineRunner不存在之类的错误。

Currently, I am developing a Spring-based JAVA library by using Spring Boot. And, I have tried to package as a jar file and let another JAVA application to use it in term of a JAVA library. Unfortunately, I found that when the caller application invokes some methods of the added library, configurations which are defined inside the library do not work at all. It also shows an error like "CommandLineRunner does not exist".

有关详细信息,请参阅pom.xml的片段文件如下所示。根据配置,我不包含Web应用程序的依赖项。

For more information, a snippet of the pom.xml file is shown below. According to the configuration, I do not include dependencies for web applications.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>2.3.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

推荐答案

如果以正确的方式设计,这应该不是问题。但具体而言,它取决于您使用的功能。由于Spring支持外部库,如JPA,Websocket,..

When designed in the correct way this should not be a problem at all. But in detail it depends which features you're using. Since Spring supports external library like JPA, Websocket,..

开发库并在另一个项目中使用它有两个重要的注释。

There are two important annotations to develop a library and use it in another project.

第一个是 @Configuration ,另一个是 @Import

The first one is simply @Configuration and the other one is @Import.

图书馆计划

在根包中放一个看起来像这个。

Put a class in the root package which looks something like this.

@Configuration // allows to import this class
@ComponentScan // Scan for beans and other configuration classes
public class SomeLibrary {
    // no main needed here
}

使用该库的其他项目

通常将一个类放在项目的根包中。

As usually put a class in the root package of your project.

@SpringBootApplication
@Import(SomeLibrary.class) // import the library
public class OtherApplication {
    // just put your standard main in this class
}

重要的是要记住,其他根据您在其他框架方面的使用情况,可能需要采取一些措施。
例如,如果您使用 spring-data @EntityScan 注释扩展了休眠扫描。

It is important to keep in mind, that other things might be necessary depending on what your using in terms of other frameworks. For example if your using spring-data the @EntityScan annotation extends the hibernate scan.

这篇关于我们可以使用Spring Boot实现Java库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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