Reputation: 85
I have a Springboot application that has a @Scheduled method. I'd like to package this in a way where other applications that want my functionality can simply add it to their POM and automatically have the @Scheduled method running without making any code changes (excepting configurations).
Is this possible through Springboot? I'd prefer to not create a parent project and rather have it as a dependency. Thanks!
EDIT: I've exported my Springboot application as a Jar and imported it locally but the @Scheduled method never runs.
Upvotes: 3
Views: 1428
Reputation: 121
To make your own library, just create a separate standard java maven project and put your common classes in it. Make sure not to put classes with the @SpringBootApplication in it (Sure it is possible to do that but let's keep it like this for the sake of simplicity).
Your pom should be like:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.own</groupId>
<artifactId>my-own-utils</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-own-utils</name>
<description>My Own Spring Boot App Utilities</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- here you put your dependencies just as in a spring boot app. You can even use starters. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sns</artifactId>
<version>1.11.301</version>
</dependency>
</dependencies>
</project>
Note that this pom do not use the spring-boot-maven-plugin, so it will be built as a standard jar.
Next, to use it in an spring boot application, just add it as a normal dependency.
<dependency>
<groupId>com.my.own</groupId>
<artifactId>my-own-utils</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
Then, to make your spring boot application to process the annotations used in your library, you must use the @ComponentScan
annotation in your application's Main class so it also search classes in your library packages:
@SpringBootApplication
@ComponentScan("com.my.own")
public class MainApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MainApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
At this point, at startup your spring application will look up for beans and components of its own package, plus the com.my.own
package, over the complete classpath including your library, and instantiate them according to their annotations.
If you want to have control over what should your app instantiate (let's say your library have 5 @Component classes but you just want to use one), you can make use of the @ConditionalOnProperty
annotation.
Upvotes: 1