🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 一、概述 SpringBoot核心思想是**约定大于配置**,它是和Spring框架紧密结合用于提升Spring开发者体验的工具。同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),SpringBoot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box),大部分的SpringBoot应用都只需要非常少量的配置代码,开发者能够更加专注于业务逻辑。 ## 二、spring-boot-starter SpringBoot能够在配置上相比Spring要简单许多, 其核心在于spring-boot-starter, 在使用SpringBoot来搭建一个项目时,只需要引入官方提供的starter, 就可以直接使用,免去了各种配置; SpringBoot存在很多开箱即用的 Starter依赖,使得我们在开发业务代码时能够不需要过多关注框架的配置,而只需要关注业务即可;举例来说,SpringBoot项目中集成 Redis,只需要加入 spring-data-redis-starter 的依赖,并简单配置一下连接信息以及 Jedis 连接池配置就可以,省去了之前很多的配置操作。甚至有些功能的开启只需要在启动类或配置类上增加一个注解即可完成; ## **原理** 在SpringBoot应用开发中,当我们在pom文件中引入starter组件依赖坐标时,启动Spring容器,此时应用会去依赖的Starter包中查找resources/META-INF/spring.factories文件,根据文件中配置去加载相应的自动配置类,类似于Java的SPI机制。简单的说自动配置是根据约定来的,也就是说SpringBoot要求必须按照他给的规则开发starter才会帮你自动配置。 ## **自定义** 1、新建一个SpringBoot工程,命名为spring-boot-starter-hello,pom.xml依赖; ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2、新建HelloProperties类,定义一个hello.msg参数(默认值World!); ``` @ConfigurationProperties(prefix = "hello") public class HelloProperties { /** * 打招呼的内容,默认为“World!” */ private String msg = "World!"; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } ``` 3、新建HelloService类,使用HelloProperties类的属性; ``` @Service public class HelloService { @Autowired private HelloProperties helloProperties; /** * 打招呼方法 * @param name 人名,向谁打招呼使用 * @return */ public String sayHello(String name) { return "Hello " + name + " " + helloProperties.getMsg(); } } ``` 4、自动配置类,可以理解为实现自动配置功能的一个入口; ``` //定义为配置类 @Configuration //在web工程条件下成立 @ConditionalOnWebApplication //启用HelloProperties配置功能,并加入到IOC容器中 @EnableConfigurationProperties({HelloProperties.class}) //导入HelloService组件 @Import(HelloService.class) //@ComponentScan public class HelloAutoConfiguration { } ``` 5、在resources目录下新建META-INF目录,并在META-INF下新建spring.factories文件,写入: ``` org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.springbootstarterhello.HelloAutoConfiguration ``` 6、收尾 ``` 1、删除自动生成的启动类SpringBootStarterHelloApplication。 2、删除resources下的除META-INF目录之外的所有文件目录。 3、删除spring-boot-starter-test依赖并且删除test目录。 ``` 7、执行mvn install将spring-boot-starter-hello安装到本地; 当你直接执行时应该会报错,因为我们还需要在pom.xml去掉spring-boot-maven-plugin,也就是下面这段代码: ``` <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` ## **使用** 随便新建一个SpringBoot工程,引入spring-boot-starter-hello依赖。 ``` <dependency> <groupId>com.example</groupId> <artifactId>spring-boot-starter-hello</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> ``` .在新工程中使用spring-boot-starter-hello的sayHello功能。 ``` @SpringBootApplication @Controller public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Autowired private HelloService helloService; @RequestMapping(value = "/sayHello") @ResponseBody public String sayHello(String name){ System.out.println(helloService.sayHello(name)); return helloService.sayHello(name); } } ```