springboot 开发入门,及问题汇总

1 . springboot简单介绍(http://projects.spring.io/spring-boot/)java

        如今的web项目几乎都会用到spring框架,而要使用spring不免须要配置大量的xml配置文件,而springboot的出现解   决了这一问题,一个项目甚至不用部署到服务器上直接开跑,真像springboot所说:“just run”。web

        springboot的不少默认编码方式都是utf-8,真是福利啊。
spring

    org.spring 2013年新开发的框架springboot , 它让一个单独项目的建立变得更加的简单,让全部依赖spring的程序能够作到“just run”。springboot提供大量第三方libraries让咱们能够很是轻松的开始建立一个spring工程,甚至不须要再去配置一些繁琐的xml配置文件apache

    框架特色:浏览器

    1:建立独立的spring应用。tomcat

    2:嵌入Tomcat, Jetty Undertow 并且不须要部署他们。springboot

    3:提供的“starters”poms来简化Maven配置服务器

    4:尽量自动配置spring应用。mvc

    5:提供生产指标,健壮检查和外部化配置app

    6:绝对没有代码生成和XML配置要求

2 . 简单实例演示 

    本文全程使用Springboot当前版本1.2.2(当前,推荐)

      

    一个简单的helloworld     直接开始run  main方法就能够了  控制台我也不知道都干了什么,好像是开始部署了,

    可是没有关联到个人tomcat。

   

    浏览器就能直接访问了。

   

3 . 步骤详解

    *注意事项:

    1.开发第一个springboot程序最好使用maven来搭建,文档全程也是maven构建。

    2.springboot由于是一个最新开发的框架,因此只支持java6以上,java7最好,官方推荐java8。

    3.须要maven3.2以上版本支持。

    4.支持如下servlet容器                 

                        也能够将springboot程序部署到全部支持servlet3.0以上的容器

     #开发第一个springboot应用  HelloWorld

        我这里用的MyEclipse10   java 6  maven 3.2.3   tomcat  7.0.55

        新建web project  并添加maven支持。

      

      next以后后面要选择javaee 6.0的library   记住不要选上,由于里面的slf4j会跟springboot自带的产生冲突。

      #配置pom.xml

        配置pom的时候碰到了不少问题。这里提醒你们一下:

<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>
    <groupId>springboot</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>springboot</name>
    <description />

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 这里必定要配置上java的版本,若是是1.7版本的可不用配置 -->
        <java.version>1.6</java.version>
        <!-- 配置你的tomcat版本 -->
        <tomcat.version>7.0.55</tomcat.version>
    </properties>
    <build>
        <plugins>
            <!--若是是经过parent方式继承spring-boot-starter-parent则不用此插件
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            -->
        </plugins>
    </build>
    <!-- 父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <!-- 导入jar包 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

1.    springboot 的logback-classes-1.1.2.jar的包下有一个org.slf4j.impl包 这是springboot真正须要的包而MyEclipse自带的javaEE6.0library里也有一个slf4j包但它不是springboot所须要的,会一直报 NoSuchMethod异常getSingleton()。搞了半天才找到,因此一开始暂时不添加javaEE6.0Library。

2.这里教你们一个快速找到class文件真正所处包的方法。

当没法肯定某个类属于哪一个包是   能够经过Test.class.getProtectionDomain();来查看

例如:发生noSuchMethod异常时,可是确实有该方法,通常就是重复加载了jar包。

3.官方文档的例子都是用java7运行的。不配置<java.version>1.6</java.version>的话可能 会报版本异常的错误。具体是啥忘了  相似mirro.minor什么51.0的   50表示jdk1.6   51是jdk1.7

4.若是也不配置tomcat版本的话springboot默认会使用8.x版本的tomcat。因此要加一个

<tomcat.version>7.0.55</tomcat.version>来指定你所使用的tomcat版本(视你CATALINA_HOME配 置的所定)。

      

 #编写java代码

        

package com.i.springboot.controller;

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
@EnableAutoConfiguration
public class FirstController {
    
    @RequestMapping(value="/")//是springmvc中的注解
    String home(){
        return "helloworld";
    }
    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(FirstController.class, args);
    }
    
}

@EnableAutoConfiguration注解用来自动配置,咱们pom中配置了    spring-boot-starter-web因此spring会来建立一 个web应用来配置程序

  完成后运行main程序无报错则运行成功。在浏览器输入相应的路径便可得到@RequestMapping返回的数据。

4 .  一个标准的springboot程序结构应该是什么样?

   1. spring一般建议咱们将main方法所在的类放到一个root包下,@EnableAutoConfiguration(开启自动配置)注解一般都放到main所在类的上面,下面是一个典型的结构布局,供参考

   

com
 +- example
     +- myproject
         +- Application.java//main方法所在类在最底层
         |
         +- bean            //bean类
         |   +- Customer.java
         |   +- CustomerRepository.java
         |
         +- service         //service层
         |   +- CustomerService.java
         |
         +- web             //controller层
             +- CustomerController.java

    从总体看去跟咱们平时的布局差很少,就是将main方法放到了最底层。

    这样@EnableAutoConfiguration能够从逐层的往下搜索各个加注解的类,例如,你正在编写一个JPA程序(若是你的pom里进行了配置的话),spring会自动去搜索加了@Entity注解的类,并进行调用。

    一般咱们只须要一个@EnableAutoConfiguration类

    2. spring一般建议咱们在进行配置的时候尽可能使用@注解的方式,尽管如今网上有各类各样成熟的xml配置方式,若是你实在不想用注解(我目前还不会怎么用注解配置。。。)能够经过@ImportResource方式导入xml文件。

    下面是一个加载xml文件配置的例子(官方实例)。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import sample.xml.service.HelloWorldService;
public class SampleSpringXmlApplication implements CommandLineRunner {
    @Autowired
    private HelloWorldService helloWorldService;
    @Override
    public void run(String... args) {
        System.out.println(this.helloWorldService.getHelloMessage());
    }
    public static void main(String[] args) throws Exception {
        //run的时候加载xml的配置
        SpringApplication.run("classpath:/META-INF/application-context.xml", args);
    }
}
<!-- xml中与咱们平时见到的同样。 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    <context:property-placeholder/>
    <bean id="helloService" class="sample.xml.service.HelloWorldService"/>
    <bean id="application" class="sample.xml.SampleSpringXmlApplication"/>
</beans>


  3. 自动配置对程序没有影响,咱们随时能够修改本身的配置来替代自动配置。例如,若是咱们添加本身的数据源,那么spring默认的将再也不使用。若是你必定要消除某些特定配置能够这样来,以下所示:

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {

}

    4.  使用@SpringbootApplication注解  能够解决根类或者配置类(我本身的说法,就是main所在类)头上注解过多的问题,一个@SpringbootApplication至关于@Configuration,@EnableAutoConfiguration@ComponentScan 并具备他们的默认属性值。

package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //等同于 @Configuration @EnableAutoConfiguration @ComponentScanpublic
class Application {    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}


5. springboot功能全解.

    1.SpringApplication  程序入口

        SpringApplication.run(MySpringConfiguration.class, args);

    2.自定义打印项(就是控制台输出内容)    

        在classpath下加入banner.txt 来定义程序启动时要输出的内容,例如我这样

        

        

    banner的变量:

Variable

Description

${application.version}

MANIFEST.MF中的版本号例如1.0

${application.formatted-version}

格式化后的版本号,就是加了个V,例如V1.0…

${spring-boot.version}

springboot版本号,例如1.2.2.RELEASE.

${spring-boot.formatted-version}

格式化后的Springboot版本号,例如V1.2.2.RELEASE………

    3. 自定义SpringApplication

        能够自定义一些应用的配置,以下关闭banner输出:

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(MySpringConfiguration.class);
    app.setShowBanner(false);
    app.run(args);
}

        SpringApplication的一些方法:

        

        SpringApplication的构造器参数每每是一个类.class,而这个类必定是加有@Configuration注解的,另外还能够换成xml的配置路径哦,前面有写出来过,SpringApplication.run("classpath:/META-INF/application-context.xml",args);

     4. 流畅的建立API

经过SpringApplicationBuilder构建
    new SpringApplicationBuilder()
    .showBanner(false)
    .sources(Parent.class)
    .child(Application.class)
    .run(args);

    5.  程序的事件和监听

除了一般的Spring框架的事件,ContextRefreshedEvent SpringApplication发送一些额外的应用程序事件。触发一些事件其实是ApplicationContext以前建立。

        除了一些常见的spring时间,像ContextRefreshedEvent  SpringApplication会产生一些额外的事件,某些事件甚至会再ApplicationContext建立之间触发。你能够经过不少方式建立监听器,通常最经常使用的就是以下:

public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(FirstController.class);
        app.addListeners(new TestListener());
        app.run(args);
}

付一个自定义的listener。

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
public class TestListener implements ApplicationListener<ApplicationStartedEvent>{
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        /*do something*/
    }
}

    程序事件运行顺序:

An ApplicationStartedEvent is sent at the start of a run, but before any processing except the registration of listeners and initializers.

An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known, but before the context is created.

An ApplicationPreparedEvent is sent just before the refresh is started, but after bean definitions have been loaded.

An ApplicationFailedEvent is sent if there is an exception on startup.

相关文章
相关标签/搜索