Home > OS >  How to create a spring/spring-boot library?
How to create a spring/spring-boot library?

Time:07-17

I would like to write a library that does a job that I always need in some way.

This library should be based on spring-boot and work within the spring environment. So it should be possible to just add the dependency in your pom file and then access the services.

Silly example: Functionality in the "Library"

@Service 
public class Foo {
   public String getTime() {...}
}

And now I could just do the following in my other project.

@Service 
public class TimeService {
   
   private Foo foo;

   public String foo() {
      return foo.getTime();
   }
}

CodePudding user response:

Just create your "library" code as a separate maven project, include it in the class path of your main application by adding a maven dependency and then - in your main application - tell Spring boot to scan the package where your Foo service is declared. It will work just as if the class was declared in the main project.

Note that if the imported library is more complex, you may very well create @Configuration classes to configure a full sub system, which can then conveniently be included in any other projects.

  • Related