在 EAR 项目中集成 Spring Boot
Integrate Spring Boot in an EAR project
我有一个使用 spring 引导创建的现有 war 项目。如何将其打包到具有 EJB 模块的 EAR 中?
有什么方法可以将 model 和 dao 包移动到 EJB 模块并用 WAR 模块注入它?
您需要一个包含 war 项目的父项目,这将是您的 spring 启动项目,以及一个专门用于制作耳朵的耳朵项目。
父级需要将 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>
你必须使用依赖管理系统。
它允许您设置不同于 spring-boot-starter-parent
的 Spring 引导 WAR
模块项目的父项目。它可以像任何其他项目一样将 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>
...那么您将以通常的方式使用入门依赖项:
<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>
共享依赖项可以由根级 pom 指定,而单独的依赖项可能是一个模块边界。
我创建了一个多模块 gradle 项目,包括一个 spring RESTFul 网络服务 -
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 项目结构是 -
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
}
所有依赖项都是从公共 libraries.gradle 导入的。 common libraries.gradle is user ear 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 中,我创建了三个基本文件来测试示例 rest 消息 -
- spring 配置名为 - BlueRestConfiguration.java
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 {
}
- 初始化文件 - 命名为 - 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} —
* for example {@code "/"}, {@code "/app"}, etc.
*
* @see #registerDispatcherServlet(ServletContext)
*/
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
- 休息服务 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 应用程序上,然后使用邮递员调用此服务 -
如果需要更多详细信息,请告诉我。我会将此代码推送到 git 并将在此处共享 git link。
我有一个使用 spring 引导创建的现有 war 项目。如何将其打包到具有 EJB 模块的 EAR 中?
有什么方法可以将 model 和 dao 包移动到 EJB 模块并用 WAR 模块注入它?
您需要一个包含 war 项目的父项目,这将是您的 spring 启动项目,以及一个专门用于制作耳朵的耳朵项目。
父级需要将 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>
你必须使用依赖管理系统。
它允许您设置不同于 spring-boot-starter-parent
的 Spring 引导 WAR
模块项目的父项目。它可以像任何其他项目一样将 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>
...那么您将以通常的方式使用入门依赖项:
<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>
共享依赖项可以由根级 pom 指定,而单独的依赖项可能是一个模块边界。
我创建了一个多模块 gradle 项目,包括一个 spring RESTFul 网络服务 - 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 项目结构是 -
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
}
所有依赖项都是从公共 libraries.gradle 导入的。 common libraries.gradle is user ear 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 中,我创建了三个基本文件来测试示例 rest 消息 -
- spring 配置名为 - BlueRestConfiguration.java
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 {
}
- 初始化文件 - 命名为 - 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} —
* for example {@code "/"}, {@code "/app"}, etc.
*
* @see #registerDispatcherServlet(ServletContext)
*/
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
- 休息服务 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 应用程序上,然后使用邮递员调用此服务 -
如果需要更多详细信息,请告诉我。我会将此代码推送到 git 并将在此处共享 git link。