Can we implement a Java library by using Spring Boot?

前端 未结 1 451
猫巷女王i
猫巷女王i 2021-02-07 15:00

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 th

相关标签:
1条回答
  • 2021-02-07 15:50

    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.

    0 讨论(0)
提交回复
热议问题