springboot学习(1)springboot介绍及入门例子

来源:互联网 发布:php addoption 编辑:程序博客网 时间:2024/05/21 22:25
1.介绍

  微服务架构

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

  该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。  

  通过这种方式,Spring Boot 致力于在蓬勃发展的快速应用开发领域(rapidapplication development)成为领导者。



2.优点

  创建独立的 Spring 应用程序

  嵌入的 Tomcat,无需部署 WAR 文件

  简化 Maven 配置

  自动配置 Spring

  提供生产就绪型功能,如指标,健康检查和外部配置

  开箱即用,没有代码生成,也无需 XML 配置。

 

3.特性

  为基于 Spring 的开发提供更快的入门体验

  开箱即用,没有代码生成,也无需 XML 配置。同时也可以修改默认值来满足特定的需求。

  提供了一些大型项目中常见的非功能特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。

  Spring Boot 并不是对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式。



入门例子


首先新建一个Maven工程,版本选择如下图:


选择上图版本创建Maven项目之后,修整项目结构,如下图:





首先配置springboot依赖,pom.xml中使其继承 spring-boot-starter-parent,并声明一个或多个 Starter POMs依赖,配置信息如下:

<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>org.amuxia</groupId>  <artifactId>demo</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.0.RELEASE</version>    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>


这里稍作解释

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


springboot官方推荐我们使用spring-boot-starter-parent,spring-boot-starter-parent帮我们实现了:

1、使用java6编译级别
2、使用utf-8编码
3、实现了通用的测试框架 (JUnit, Hamcrest, Mockito).
4、智能资源过滤
5、智能的插件配置(exec plugin, surefire, Git commit ID, shade).


使用它省去了自己动手配置,当然,也可以自己灵活的选择配置。pom.xml信息配置成功之后,如果要使用jdk7+,换掉类库之后项目上会出现红叉,但是运行一切正常。


这是项目更改jdk版本导致的,解决办法是在上面pom.xml的build--plugins中加上这么一段代码:

<plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-compiler-plugin</artifactId>        <configuration>           <source>1.8</source>           <target>1.8</target>         </configuration>     </plugin>


然后右击项目——Maven——update project,就可以看到红叉消失了。


接着我们创建一个App.java类

package org.amuxia.start;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@EnableAutoConfigurationpublic class App {      @RequestMapping("/hello")      public String home() {        return "this is hello page";      }          @RequestMapping("/world")      public String word() {        return "this is world page";      }          public static void main( String[] args )    {        System.out.println( "start....." );        SpringApplication.run(App.class, args);    }}


这里解释两个注解:

@RestControlle:

            @RestController被称为一个构造型(stereotype)注解。它为阅读代码的开发人员提供建议。对于Spring,该类扮演了一个特殊角色。它继承自@Controller注解。4.0之前的版本,spring MVC的组件都使用@Controller来标识当前类是一个控制器servlet。使用这个特性,我们可以开发REST服务的时候不需要使用@Controller而使用专门的@RestController。


@EnableAutoConfiguration:

            @EnableAutoConfiguration注解告诉SpringBoot根据添加的jar依赖猜测你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假设正在开发一个web应用并相应地对Spring进行设置。



因为springboot内嵌了tomcat,此时我们直接运行Java application,然后在浏览器输入http://localhost:8080/world




而输入http://localhost:8080/hello时,就会切换到 this is hello page界面,非常简单方便。


springboot默认端口就是8080,如果我们想多开几个服务,继续运行就会报端口冲突的错误,springboot有很好的解决办法,此时我想访问端口为8011

package org.amuxia.start;import java.util.concurrent.TimeUnit;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@EnableAutoConfigurationpublic class App {      @RequestMapping("/hello")      public String home() {        return "this is hello page";      }          @RequestMapping("/world")      public String word() {        return "this is world page";      }          public static void main( String[] args )    {        System.out.println( "start....." );        SpringApplication.run(App.class, args);    }        /**     * @return     * 更改启动项目的端口号     */    @Bean    public EmbeddedServletContainerFactory servletFactory(){        TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();        tomcatFactory.setPort(8011);        tomcatFactory.setSessionTimeout(10,TimeUnit.SECONDS);        return tomcatFactory;    }}


此时访问8080和8011都是可以的,挺好的。



原创粉丝点击