一.认识SpringBoot
通常经常听到的有Spring ,SpringBoot ,SpringCloud,其实SpringBoot是Spring的一套快速开发的脚手架,SpringCloud是基于SpringBoot实现的云应用开发工具,三者的关系属于Spring>Spring Boot >Spring Cloud。
Spring Boot相对于Spring节省了大量的配置工作,他的理念就是默认大于配置,它主要通过注解来实现各种配置和方法的实现。
二.创建SpringBoot项目
创建SpringBoot的项目有两种,一种是通过idea自己创建,但是还得手动添加poml引入,所以推荐用Springboot项目创建网创建项目
然后下载Zip然后解压导入项目
三.Spring Boot的启动
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
SpringBootApplication就是SpringBoot的入口类的注解,我们可以通过查看SpringBootApplication的源代码发现里面包含8个原注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
所以我们可以发现ComponentScan组件扫描是包含在SpringBootApplication启动类这个注解里面的,所以我们可以大概得知
@SpringBootApplication =@Configuration+@EnableAutoConfiguration+@ComponentScan
@Configuration: 主要标注在某个类上,⽤于spring扫描注⼊,⼀般结合@Bean使⽤
@EnableAutoConfiguration: 启⽤Spring的⾃动加载配置,⾃动载⼊应⽤程序所需的所有Bean
@ComponentScan:告诉spring扫描包的范围,默认是Applocation类所在的全部⼦包,可以指定其他包
所以如果我们要指定包启动来提高性能,可以
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
//@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan({"com.example.demo.controller"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
四.注解
Spring Boot的注解就相当于封装好的各种类,方便开发者直接调用
常用注解
五.目录结构
src/main/java:存放代码
src/main/resources
static: 存放静态⽂件,⽐如 css、js、image
templates:存放静态⻚⾯jsp,html,tpl
config:存放配置⽂件,application.properties
resources:
同个⽂件的加载顺序,静态资源⽂件 Spring Boot 默认会挨个从
META/resources >
resources >
static >
public
⾥⾯找是否存在相应的资源,如果有则直接返回,不在默认加载的⽬录,则找不到
可以通过Spring Boot的源代码发现这些
public static class Resources {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
/**
* Whether to enable default resource handling.
*/
private boolean addMappings = true;
private boolean customized = false;
private final Chain chain = new Chain();
private final Cache cache = new Cache();
public String[] getStaticLocations() {
return this.staticLocations;
}
如果需要指定特定的静态目录,在resources/application.properties里添加,后面添加你自己的指定目录
spring.resources.static-locations = classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
六.启动方式和部署
6.1 IDEA启动修改端口
在application.properties中添加属性server.port=8080,修改成自己需要更改的端口
6.2 jar包方式启动
添加maven插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
执行mvn install
如果报错
No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize
则在build添加compile
install成功之后cd到target目录 然后执行 java -jar 你的jar包.jar