spring boot基础 入门

spring boot基础

spring boot 的简单搭建

spring boot 的基本用法

spring boot 基本用法
自动配置
技术集成
性能监控	
源码解析	

工程的构建

创建一个maven项目

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

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

<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>

将这个代码添加到pom.xml中

创建 Application.Java

package com.lkl.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

运行main方法 ,访问你的http://localhost:8080/hello 就可以访问你的Tomcat了。是不是很简单,以前要配置的很多的spring文件,现在不需要了。 就是那么的简洁

**@SpringBootApplication 注解 **

SpringBootApplication注解源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration 表示Application作为sprig配置文件存在 
@EnableAutoConfiguration启动spring boot内置的自动配置 
@ComponentScan扫描bean,路径为Application类所在package以及package下的子路径,这里为 com.lkl.springboot,在spring boot中bean都放置在该路径已经子路径下。
public @interface SpringBootApplication {

    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     * @return the classes to exclude
     */
    Class<?>[] exclude() default {};

}

2 spring boot 属性配置和使用

  • Spring Boot 支持多种外部配置方式
  • 命令行参数
  • Java系统属性
  • 操作系统环境变量
  • RandomValuePropertySource
  • 应用配置文件(.properties或.yml)
    • 普通配置文件中可以这样写
      1. name=Isea533
      2. server.port=8080
    • .yml格式的配置文件如
      1. name: Isea533
      2. server:
      3.   port: 8080
        

注意:使用.yml时,属性名的值和冒号中间必须有空格,如name: Isea533正确,name:Isea533就是错的。

属性配置文件的位置
spring会从classpath下的/config目录或者classpath的根目录查找application.properties或application.yml。

  • @PropertySource

  • SpringApplication.setDefaultProperties

  • 应用(使用)属性

  • @Value(“${xxx}”)

  • @ConfigurationProperties

  • 在@Bean方法上使用@ConfigurationProperties

  • 属性占位符

  • 通过属性占位符还能缩短命令参数

  • 属性名匹配规则

  • 属性验证

Spring Boot 集成MyBatis

下次介绍

posted on 2017-09-14 18:49  王守昌  阅读(157)  评论(0编辑  收藏  举报

导航