码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot开发环境搭建

时间:2017-07-02 15:14:19      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:继承   config   结果   port   实现   执行   内容   test   username   

软件152 程永绩

1.什么是springboot:

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。

该框架使用了特定的方式(继承starter,约定优先于配置)来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

Spring Boot并不是一个框架,从根本上将,它就是一些库的集合,maven或者gradle项目导入相应依赖即可使用Spring Boot,而且无需自行管理这些库的版本。

 

 

2.为什么要用spring boot

Spring Boot是为简化Spring项目配置而生,使用它使得jar依赖管理以及应用编译和部署更为简单。Spring Boot提供自动化配置,使用Spring Boot,你只需编写必要的代码和配置必须的属性。

使用Spring Boot,只需20行左右的代码即可生成一个基本的Spring Web应用,并且内置了tomcat,构建的fat Jar包通过Java -jar就可以直接运行。

如下特性使得Spring Boot非常契合微服务的概念,可以结合Spring BootSpring CloudDocker技术来构建微服务并部署到云端:

?一个可执行jar即为一个独立服务

?很容易加载到容器,每个服务可以在自己的容器(例如docker)中运行

?通过一个脚本就可以实现配置与部署,很适合云端部署,并且自动扩展也更容易

简单而言,即Spring Boot使编码更简单,使配置更简单,使部署更简单,使监控更简单。

 

3.spring boot 各模块介绍

Spring Boot由一些模块构成,如spring-boot, spring-boot-autoconfigure, spring-boot-starters, spring-boot-cli, spring-boot-actuator等。下面简单介绍以下各模块:

 

a. spring-boot

主库,为其他模块提供特性支持。包括以下内容:

SpringApplication类,提供静态方法,方便编写独立运行的Spring应用。唯一的任务是创建和刷新一个合适的Spring ApplicationContext

嵌入式web应用,自带容器(Tomcat, Jetty)

 

b. spring-boot-autoconfigure

Spring Boot可以基于classpath下的内容配置通用应用的大部分模块。一个@EnableAutoConfiguration注解触发Spring上下文的自动配置。

自动配置尝试推测用户可能需要的bean。例如,如果H2DBclasspath中,但是用户没有配置任何db连接,那么spring-boot-autoconfigure推断用户需要一个in-memorydb,因此自动配置为用户配置(默认创建的h2dbdb名为testdb, 用户名为sa,密码无)。自动配置优先级低于用户自定义的bean

 

c. spring-boot-starters

starters是一系列便利的依赖描述,用户可以增加到应用中,并由此取得Spring和相关技术的 一站式配置体验,无需查看sample代码并贴来贴去。例如,若用户想要使用SpringJPA来访问db,则只需包含spring-boot-starter-data-jpa依赖到pom中即可。

 

d. spring-boot-cli

Spring的命令行应用,编译和运行Groovy源码,只需极少的代码就可以运行应用,Spring CLI还可以监视文件,在它们改变时自动重新编译和重启。

 

e.spring-boot-actuator

spring boot actuator提供额外的自动配置,为你的应用装饰一些特性,使应用在生产环境下也可以快速部署和支持。例如,若你正在编写一个JSON web服务,该模块会提供一个服务器,安全,日志,外部化配置,管理端点(management endpoints),评审等。关闭这些内建特性,或者扩展或替代它们都很容易。

 

f.spring-boot-loader

Spring Boot Loader提供秘籍允许你构建可用java jar直接运行的jar包。一般无需直接使用spring-boot-loader,而是通过GradleMaven插件使用。

 

4.怎么使用spring boot

maven构建项目pom代码;

<projectxmlns="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>ygnet</groupId>  

    <artifactId>boot</artifactId>  

    <version>0.0.1-SNAPSHOT</version>  

    <packaging>jar</packaging>  

  

    <name>Springboot</name>  

    <url>http://maven.apache.org</url>  

      

    <properties>  

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  

        <java.version>1.7</java.version>  

    </properties>  

       

    <parent>  

        <groupId>org.springframework.boot</groupId>  

        <artifactId>spring-boot-starter-parent</artifactId>  

        <version>1.3.5.RELEASE</version>  

        <relativePath />  

    </parent>  

    <dependencies>  

        <dependency>  

            <groupId>junit</groupId>  

            <artifactId>junit</artifactId>  

            <version>4.12</version>  

            <scope>test</scope>  

        </dependency>  

        <dependency>  

          <groupId>org.springframework</groupId>  

          <artifactId>spring-test</artifactId>  

          <version>4.2.6.RELEASE</version>  

        </dependency>  

                

        <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-web</artifactId>  

        </dependency>  

        <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-jdbc</artifactId>  

        </dependency>  

        <dependency>  

            <groupId>org.postgresql</groupId>  

            <artifactId>postgresql</artifactId><scope>runtime</scope>  

        </dependency>  

        <dependency>  

            <groupId>org.springframework.boot</groupId>  

            <artifactId>spring-boot-starter-actuator</artifactId>  

        </dependency>  

    </dependencies>  

    <build>  

        <pluginManagement>    

            <plugins>    

              <plugin>    

                <groupId>org.eclipse.m2e</groupId>    

                <artifactId>lifecycle-mapping</artifactId>    

                <version>1.0.0</version>    

                <configuration>    

                  <lifecycleMappingMetadata>    

                    <pluginExecutions>    

                      <pluginExecution>    

                        <pluginExecutionFilter>    

                          <groupId>org.apache.maven.plugins</groupId>    

                          <artifactId>maven-dependency-plugin</artifactId>    

                          <versionRange>[2.0,)</versionRange>    

                          <goals>    

                            <goal>copy-dependencies</goal>    

                          </goals>    

                        </pluginExecutionFilter>    

                        <action>    

                          <ignore />    

                        </action>    

                      </pluginExecution>    

                    </pluginExecutions>    

                  </lifecycleMappingMetadata>    

                </configuration>    

              </plugin>    

            </plugins>    

        </pluginManagement>  

        <plugins>  

  

            <plugin>  

                <groupId>org.apache.maven.plugins</groupId>    

                <artifactId>maven-jar-plugin</artifactId>    

                <configuration>    

                    <archive>    

                        <manifest>    

                           <addClasspath>true</addClasspath>    

                           <classpathPrefix>lib/</classpathPrefix>    

                           <mainClass>yg.boot.App</mainClass>    

                        </manifest>    

                    </archive>    

                </configuration>    

            </plugin>  

      

            <plugin>    

                <groupId>org.apache.maven.plugins</groupId>    

                <artifactId>maven-resources-plugin</artifactId>    

                <version>2.5</version>    

                <executions>    

                    <execution>    

                        <phase>compile</phase>    

                    </execution>    

                </executions>    

                <configuration>    

                    <encoding>${project.build.sourceEncoding}</encoding>    

                </configuration>    

            </plugin>  

 

            <plugin>  

                <groupId>org.apache.maven.plugins</groupId>    

                <artifactId>maven-surefire-plugin</artifactId>  

                <version>2.17</version>   

                <configuration>  

                  <skipTests>true</skipTests>    

                </configuration>  

            </plugin>  

 

            <plugin>    

                <groupId>org.apache.maven.plugins</groupId>    

                <artifactId>maven-dependency-plugin</artifactId>    

                <version>2.8</version>    

                <executions>    

                    <execution>    

                        <phase>package</phase>    

                        <goals>    

                            <goal>copy-dependencies</goal>    

                        </goals>    

                    </execution>    

                </executions>  

                <configuration>  

                    <outputDirectory>${project.basedir}/lib</outputDirectory>  

                    <includeScope>compile</includeScope>    

                </configuration>    

            </plugin>  

  

            <plugin>  

                <groupId>org.springframework.boot</groupId>  

                <artifactId>spring-boot-maven-plugin</artifactId>  

            </plugin>  

        </plugins>  

    </build>  

</project>  

JAVA代码:

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  

import org.springframework.web.bind.annotation.RequestMapping;  

import org.springframework.web.bind.annotation.RestController;  

@RestController  

@EnableAutoConfiguration  

@RequestMapping("/test")  

public class AppController {      

    @RequestMapping("/sayhello")  

    public String sayHello(){  

        return "Hello World!";  

    }  

}  

Application.properties代码:

# DataSource settings  

spring.datasource.url=jdbc:postgresql://localhost:5432/jcbk  

spring.datasource.username=jcbk  

spring.datasource.password=123456  

spring.datasource.driver-class-name=org.postgresql.Driver  

  

# Tomcat Server settings (ServerProperties)  

server.port= 9080  

server.address= 127.0.0.1  

server.sessionTimeout= 30  

server.contextPath= /  

  

# Tomcat specifics  

tomcat.accessLogEnabled= false  

tomcat.protocolHeader= x-forwarded-proto  

tomcat.remoteIpHeader= x-forwarded-for  

tomcat.basedir=  

tomcat.backgroundProcessorDelay=30 \# secs

 

Java代码:

import org.springframework.boot.SpringApplication;  

import org.springframework.boot.autoconfigure.SpringBootApplication;  

/**

 * Hello world!

 */  

@SpringBootApplication  

public class App {    

    public static void main(String[] args ){  

        SpringApplication.run(App.class,args);  

    }  

}

直接运行App后,结果如下图所示。启动后访问http://localhost:9080/test/sayhello, 输出 Hello World!

技术分享

 

spring boot开发环境搭建

标签:继承   config   结果   port   实现   执行   内容   test   username   

原文地址:http://www.cnblogs.com/1226simeiren/p/7105912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!