springboot项⽬层次结构_SpringBoot(⼀)——项⽬结构SpringBoot初始教程之项⽬结构
1 简介
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Features
Create stand-alone Spring applications
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
Provide opinionated ‘starter’ POMs to simplify your Maven configuration
Automatically configure Spring whenever possible
Provide production-ready features such as metrics, health checks and externalized configuration
Absolutely no code generation and no requirement for XML configuration
上述是官⽅的英⽂讲解、⼤概翻译后如下:
SpringBoot在建⽴⽣产中的独⽴程序上⾮常简便、只需要⼀些简便的配置就能运⾏起来。⼤致有如下特点:
创建独⽴的Spring applications
能够使⽤内嵌的Tomcat, Jetty or Undertow,不需要部署war
提供starter pom来简化maven配置
⾃动配置Spring
提供⼀些⽣产环境的特性,⽐如metrics, health checks and externalized configuration
绝对没有代码⽣成和XML配置要求
2. 快速开始
xmlns:xsi="/2001/XMLSchema-instance"
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
4.0.0
springboot-1
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
1.4.1.RELEASE
这块统⼀⽤spring-boot-starter-parent做为parent 所以spring-boot-starter-web不⽤填写版本号
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
1
2
3
4
5
6
7
下⾯这个插件是⽤来运⾏Springboot的,通常有两种⽅式可以运⾏SpringBoot,⼀种是通过直接运⾏main⽅法,另外⼀种是通过使⽤下⾯的插件运⾏。
两种⽅式有差别,⼀旦项⽬中需要访问资源的时候就需要通过插件运⾏,否则⽆法访问到资源
org.springframework.boot
spring-boot-maven-plugin
1.4.1.RELEASE
上⾯是maven配置,接下来需要配置整个程序的⼊⼝,AppApplication
@SpringBootApplication
public class AppApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(AppApplication.class, args);
}
}
下⾯写了⼀个简易的controller,只要运⾏main⽅法或者插件,就能够正常访问了。这块需要注意,controller跟AppApplication放在⼀个包下⾯,
如果不再⼀个下⾯需要配置扫描的包
@RestController
public class IndexController {
@GetMapping("/index")
public ResponseEntity helloWord() {
return ResponseEntity.ok("hello word");
}
}
3.配置详解
@SpringBootApplication 这个注解是⼀个组合注解,聚合了多个注解的功能,具体源码如下
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))
public @interface SpringBootApplication {
//排除⾃启动项
Class>[] exclude() default {};
springboot架构图//排除⾃动启动的beanName
String[] excludeName() default {};
//扫描包
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
//扫描类
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class>[] scanBasePackageClasses() default {};
}
@EnableAutoConfiguration这个注解是⽤来启动SpringBoot中的⾃动配置项⽬,这个注解是必须加上的,否则⽆法正常使⽤因为SpringBoot
默认配置了很多配置项,如下图所⽰配置在这⾥⾯的配置项都会⾃动进⾏配置,⼀部分需要引⽤其他jar包配置才会⽣效
application.yaml ⽂件详解
application.yaml是整个应⽤程序的配置⽂件,SpringBoot⾃动加载,SpringBoot提供针对各种组件的都可以通过application.yaml进⾏配置,后续再进⾏详解。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。