Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we implement a Java library by using Spring Boot?

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.

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".

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>

like image 779
xlives Avatar asked Mar 04 '16 07:03

xlives


People also ask

Is it possible to use Spring Boot in the same way as any standard Java library?

You can use Spring Boot in the same way as any standard Java library. To do so, include the appropriate spring-boot-*. jar files on your classpath. Spring Boot does not require any special tools integration, so you can use any IDE or text editor.

Is Spring Boot a framework or library?

A framework is a known programing environment, such as Spring Boot. When a software lead sets out to build a new enterprise application, they must decide which set of libraries and frameworks they want to use.

Can I run Spring Boot application as Java application?

You can run a Spring Boot application from your IDE as a simple Java application. However, you first need to import your project.


1 Answers

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.

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

Library Project

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
}

Other Project using the library

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
}

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.

like image 134
mh-dev Avatar answered Oct 25 '22 20:10

mh-dev