在Spring项目中集成Spring Boot

时间:2017-04-07 11:27:31

标签: spring spring-boot ear

我有一个使用spring boot创建的war项目。如何在具有EJB模块的EAR中打包它?

有没有办法将模型和dao包移动到EJB模块并使用WAR模块注入它?

3 个答案:

答案 0 :(得分:4)

你需要一个包含战争项目的父项目,这个项目将是你的春季启动项目,还有一个专门用于吸引人的耳朵项目。

父级需要将spring引导作为其父级:

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

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
  </parent>

  <groupId>com.greg</groupId>
  <artifactId>ear-example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
        <myproject.version>1.0-SNAPSHOT</myproject.version>
  </properties>

  <name>ear-example</name>
  <modules>
    <module>example-ear</module>
    <module>example-war</module>
  </modules>

</project>

您的耳朵项目是:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.greg</groupId>
    <artifactId>ear-example</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>example-ear</artifactId>
  <packaging>ear</packaging>

  <dependencies>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>example-war</artifactId>
      <version>${project.version}</version>
      <type>war</type>
    </dependency>
  </dependencies>

  <build>
   <plugins>
     <plugin>
        <artifactId>maven-ear-plugin</artifactId>
        <version>2.10.1</version>
        <configuration>
                <modules>
                        <webModule>
                                <groupId>com.greg</groupId>
                                <artifactId>example-war</artifactId>
                                <contextRoot>/appname</contextRoot>
                        </webModule>
                </modules>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

答案 1 :(得分:0)

您必须使用依赖关系管理系统。

它允许您将Spring Boot WAR模块项目的父项设置为与spring-boot-starter-parent不同的项目。然后就可以像其他任何项目一样将WAR项目包含到EAR项目中。

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

...现在您可以通常的方式使用所有Spring Boot启动器依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
</dependencies>

您要在模块项目级别指定的入门依赖项,而依赖项管理配置可以在两个项目中指定 - 围绕整个EAR项目或单独指定每个项目,具体取决于应用程序要求。

Using Spring Boot without the parent POM

答案 2 :(得分:0)

我创建了一个包含春季RESTFul Web服务的多模块gradle项目- EAR应用程序的名称是-bluestone bluestone / settings.gradle-

rootProject.name = 'bluestone'
include ':bluestone-web'
include ':bluestone-core'
include ':bluestone-rest'

project (':bluestone-web').projectDir = new File('bluestone-web')
project (':bluestone-core').projectDir = new File('bluestone-core')
project (':bluestone-rest').projectDir = new File('bluestone-rest')

bluestone-rest项目结构为-

enter image description here

bluestone-rest / build.gradle

plugins {
    id 'war'
}

group 'com.bluestone.smart.rest'
version '1.0-SNAPSHOT'



dependencies {
    compile library.spring_context
    compile library.spring_web
    compile library.spring_beans
    compile library.spring_mvc
    providedCompile library.servlet_api
    testCompile library.junit

}

所有依赖项均从常见的library.gradle导入。常见的librarys.gradle是用户的耳朵bluestone / libraries.gradle

/* ============================================================================
   Library definitions for project 'Bluestone'
   ============================================================================
   Define here library dependencies and use them inside sub-modules build.gradle.

   Included from: "${rootProject.projectDir}/build.gradle"
   ============================================================================

 */
ext {

    library = [
            /* testing */
            junit: "junit:junit:4.12",
            log4j: "log4j:log4j:1.2.17",

            /* Spring libraries*/
            spring_context:					"org.springframework:spring-context:${spring_lib_version}",
            spring_aop:						"org.springframework:spring-aop:${spring_lib_version}",
            spring_beans:					"org.springframework:spring-beans:${spring_lib_version}",
            spring_orm:						"org.springframework:spring-orm:${spring_lib_version}",
            spring_web:						"org.springframework:spring-web:${spring_lib_version}",
            spring_mvc:                     "org.springframework:spring-webmvc:${spring_lib_version}",
            servlet_api:                     "javax.servlet:javax.servlet-api:4.0.1"

    ]
}

在bluestone-rest内,我创建了三个基本文件来测试示例休息消息-

  1. 命名为-BlueRestConfiguration.java的spring配置

package com.bluestone.smart.rest.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.bluestone.smart.rest.resources", "com.bluestone.smart.rest.controller"})
public class BlueRestConfiguration {
}

  1. 初始化文件-名为-RestInit.java

package com.bluestone.smart.rest.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.ServletContext;

public class RestInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    /**
     * Specify {@code @Configuration} and/or {@code @Component} classes for the
     * {@linkplain #createRootApplicationContext() root application context}.
     *
     * @return the configuration for the root application context, or {@code null}
     * if creation and registration of a root context is not desired
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {BlueRestConfiguration.class};
    }

    /**
     * Specify {@code @Configuration} and/or {@code @Component} classes for the
     * {@linkplain #createServletApplicationContext() Servlet application context}.
     *
     * @return the configuration for the Servlet application context, or
     * {@code null} if all configuration is specified through root config classes.
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    /**
     * Specify the servlet mapping(s) for the {@code DispatcherServlet} &mdash;
     * for example {@code "/"}, {@code "/app"}, etc.
     *
     * @see #registerDispatcherServlet(ServletContext)
     */
    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
}

  1. 休息服务API-名为-GreetingsController.java

package com.bluestone.smart.rest.resources;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingsController {
    @RequestMapping(path = "/greeting", method = RequestMethod.GET)
    public String greetings(){
        return "Welcome Spring Rest!";
    }
}

最终使用-

构建此EAR应用程序

gradlew clean build 

并在WildFly应用程序上进行部署,然后使用邮递员调用此服务- enter image description here

请让我知道是否需要更多详细信息。我将在git上推送此代码,并在此处共享git链接。