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>
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.
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.
You can run a Spring Boot application from your IDE as a simple Java application. However, you first need to import your project.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With