如何打包Springboot应用程序以充当另一个Springboot应用程序中的操作依赖项

时间:2018-08-07 15:24:54

标签: java maven spring-boot

我有一个带有@Scheduled方法的Springboot应用程序。我希望将其打包,以便其他需要我功能的应用程序可以简单地将其添加到其POM中,并自动运行@Scheduled方法,而无需进行任何代码更改(配置除外)。

通过Springboot可以做到吗?我宁愿不创建父项目,而将其作为依赖项。谢谢!

编辑:我已经将Springboot应用程序导出为Jar并导入到本地,但是@Scheduled方法从不运行。

1 个答案:

答案 0 :(得分:1)

要创建自己的库,只需创建一个单独的标准java maven项目,然后将您的公共类放入其中。确保在其中放置带有@SpringBootApplication的类(当然可以这样做,但是为了简单起见,让它保持这种样子)。

您的pom应该像:

<?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>

请注意,此pom 请勿使用spring-boot-maven-plugin ,因此它将作为标准jar构建。

接下来,要在Spring Boot应用程序中使用它,只需将其添加为普通依赖项即可。

<dependency>
  <groupId>com.my.own</groupId>
  <artifactId>my-own-utils</artifactId>
  <version>1.0.0-SNAPSHOT</version>
</dependency>

然后,要使您的Spring Boot应用程序处理库中使用的注释,必须在应用程序的Main类中使用@ComponentScan注释,以便它还可以在库包中搜索类:

@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);
    }
}

这时,在启动时,spring应用程序将在包括您的库在内的完整类路径上查找其自己的包以及com.my.own包的bean和组件,并根据其注释实例化它们。

如果您想控制应用程序实例化的内容(假设您的库有5个@Component类,但您只想使用其中的一个),则可以使用@ConditionalOnProperty批注。

相关问题