Spring boot war部署

来源:互联网 发布:大平面铣削编程 编辑:程序博客网 时间:2024/05/17 23:41

一:介绍

     spring boot 能够将应用程序作为独立的jar启动是很好的,但是有时可能不可能运行一个应用程序,比如jar环境限制、公司范围的法规等等,您必须构建一个WAR,以便部署到一个传统的web/应用服务器上。Spring Boot帮助我们创建使用SpringBootServletInitializer 。

二: 新建项目 

     

三: 创建Maven文件 

        找到本例中使用的Maven文件。


<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/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.xph</groupId>  <artifactId>egl-web</artifactId>  <packaging>war</packaging>  <version>1.0-SNAPSHOT</version>  <name>egl-web Maven Webapp</name>  <url>http://maven.apache.org</url>  <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.4.5.RELEASE</version>  </parent>  <properties>    <java.version>1.7</java.version>    <guava.version>19.0</guava.version>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>    <jackson.version>2.2.1</jackson.version>  </properties>  <dependencies>  <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>    <scope>test</scope>    </dependency>    <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>      <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-aop</artifactId>      </dependency>    <!-- Guava -->    <dependency>      <groupId>com.google.guava</groupId>      <artifactId>guava</artifactId>      <version>${guava.version}</version>    </dependency>    <!-- Java EE -->    <dependency>      <groupId>javax.inject</groupId>      <artifactId>javax.inject</artifactId>      <version>1</version>    </dependency>    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>      <version>5.1.26</version>    </dependency>    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>druid</artifactId>      <version>1.0.24</version>    </dependency>    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>fastjson</artifactId>      <version>1.2.14</version>    </dependency>    <!--  junit    -->    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.10</version>    </dependency>   <dependency>      <groupId>javax.servlet</groupId>      <artifactId>jstl</artifactId>    </dependency>   <dependency>      <groupId>javax.servlet</groupId>      <artifactId>javax.servlet-api</artifactId>    </dependency>  <dependency>          <groupId>org.apache.tomcat.embed</groupId>          <artifactId>tomcat-embed-jasper</artifactId>        <scope>provided</scope>      </dependency>    <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-tomcat</artifactId>          <scope>provided</scope>      </dependency>     <dependency>          <groupId>org.projectlombok</groupId>          <artifactId>lombok</artifactId>          <version>1.16.12</version>      </dependency>
     <dependency>            <groupId>com.github.xphsc</groupId>            <artifactId>eglsc-helper</artifactId>            <version>1.2.0</version>        </dependency>
</dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.xph.web.Application</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warSourceExcludes></warSourceExcludes> <overlays> </overlays> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> <resources> <resource> <directory>${project.basedir}/src/main/webapp</directory> <includes> <include>**/**</include> </includes> </resource> <resource> <directory>${project.basedir}/src/main/resources</directory> </resource> </resources> </build></project>

找到spring boot启动器的描述。

spring-boot-starter-parent:父项目POM用于依赖关系管理。

spring-boot-starter-web::构建web、REST应用程序的启动器。它使用tomcat服务器作为默认的嵌入式服务器。

Spring-stardata-data JPA:使用hibernate的Spring数据JPA启动器

spring-boot-starter-tomcat: spring boot部署到外部容器(tomcat)


3。创建application.properties,application-dev.yml

在Spring boot中,为了配置与数据库相关的属性、Hibernate和日志,我们需要使用application.properties。我们正在使用JPA规范的Hibernate实现。

application-dev.yml

server:  port: 8081spring:      datasource:        default:              name: test_mysql              url: jdbc:mysql://192.168.211.130:3306/test_mysql?useUnicode=true&characterEncoding=UTF-8              username: root              password: 12345              type: com.alibaba.druid.pool.DruidDataSource              driver-class-name: com.mysql.jdbc.Driver              filters: stat              maxActive: 20              initialSize: 1              maxWait: 60000              minIdle: 1              timeBetweenEvictionRunsMillis: 60000              minEvictableIdleTimeMillis: 300000              validationQuery: select 'x'              testWhileIdle: true              testOnBorrow: false              testOnReturn: false              poolPreparedStatements: true              maxOpenPreparedStatements: 20
application.properties
spring.mvc.view.prefix = /WEB-INF/jsp/spring.mvc.view.suffix = .jspspring.profiles.active=dev
四 代码的具体实现

1:spring boot启动类
 
@SpringBootApplication@ComponentScan(basePackages = "com.xph")public class Application extends SpringBootServletInitializer {    public static void main(String[] args) {        new SpringApplicationBuilder()                .sources(Application.class)                .bannerMode(Banner.Mode.CONSOLE)                .run(args);    }   @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {        return applicationBuilder.sources(Application.class);    }}

 SpringBootServletInitializer是一个抽象类的实现WebApplicationInitializer接口,servlet 3.0 +的主要的抽象环境来配置ServletContext编程。它结合Servlet、过滤和ServletContextInitializer bean从Servlet容器的应用程序上下文。

创建一个类,扩展了SpringBootServletInitializer,覆盖它的配置方法。你可以让你的应用程序的主类扩展SpringBootServletInitializer:

2:创建jsp页面 index.jsp

   

<%@ page language="java" contentType="text/html; charset=UTF-8"         pageEncoding="UTF-8"%><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>Insert title here</title></head><body><h2>        鹅,鹅,鹅</br>    鹅,鹅,鹅,曲项向天歌。</br>    白毛浮绿水,红掌拨清波。</br></h2></body></html>

3:创建controller控制层

@Controllerpublic class ViewController {        @RequestMapping("/index")    public String view(){        return "/index";    }    @RequestMapping("/html")    public String html(){        return "/view";    }}

3 :启动本地的tomcat就可以访问自己定义好的页面(如图)