文章目录
1.如何理解starter
1.1.模块管理,自动加载Bean到IOC
1.Starter 是 Spring Boot 中的一个非常重要的概念,Starter相当于模块,
它能将模块所需的依赖整合起来并对模块内的Bean根据环境(条件)进行自动配置。
使用者只需要依赖相应功能的 Starter,无需做过多的配置和依赖,SpringBoot 就能自动扫描并加载相应的模块。
通俗理解
1.比如我们进行spring的web项目开发,这个时候,可能我们没办法知道需要加载那些jar或者没法十分清楚需要的jar包
这个时候spring-bootstarter-web这个starter就给我们一键式的整理了所有的需要jar
这样能够更快捷的简化开发,提升开发进度,避免不必要的开发调试jar包依赖的工作,将需要的Bean自动加载到IOC容器之中
1.2.通过spring-boot-starter-web去理解starter概念
从右边我们可以看出来,spring-boot-starter-web 本质上就是将web开发需要的所有的jar包依赖给导入进来了
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.6.RELEASE</version>
<name>Spring Boot Web Starter</name>
<description>Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container</description>
<url>https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-web</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>https://spring.io</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<developers>
<developer>
<name>Pivotal</name>
<email>info@pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
<developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
<url>https://github.com/spring-projects/spring-boot</url>
</scm>
<issueManagement>
<system>Github</system>
<url>https://github.com/spring-projects/spring-boot/issues</url>
</issueManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.2.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.2.6.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
2.自动装载[案例]
2.1.创建简单的maven项目:demo-spring-boot-starter-example-01
2.1.1.定义接口类FormatProcessor以及接口方法format()
package com.gaoxinfu.demo.spring.boot.format;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-04-22 08:23
*/
public interface FormatProcessor {
//主要是一个格式化
<T> String format(T obj);
}
2.1.2.定义实现类类StringFormatProcessor以及实现接口方法format()
这个字符串的一个传统格式化
package com.gaoxinfu.demo.spring.boot.format;
import java.util.Objects;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-04-22 08:25
*/
public class StringFormatProcessor implements FormatProcessor{
@Override
public <T> String format(T obj) {
return "StringFormatProcessor ="+ Objects.toString(obj) ;
}
}
2.1.3.定义实现类类JsonFormatProcessor以及实现接口方法format()
主要是用了阿里巴巴的fastjson的json格式化的方法
2.1.3.1.引入阿里巴巴的fastjson的jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
<optional>true</optional> -- 意味着引用该jar包可以对包的版本进行选择,不加版本默认1.2.61
</dependency>
2.1.3.4.接口实现与方法
package com.gaoxinfu.demo.spring.boot.format;
import com.alibaba.fastjson.JSONObject;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-04-22 08:25
*/
public class JsonFormatProcessor implements FormatProcessor{
@Override
public <T> String format(T obj) {
return "JsonFormatProcessor ="+JSONObject.toJSONString(obj);
}
}
2.1.4.打出jar包
1. 我们这里是要打出一个可以让别的项目可以直接引用使用的jar包
这里注意下,我们的pom中的package类型为jar,
2.2.在demo-spring-boot-example-06项目中进行引用上面的jar包测试
2.2.1.创建一个springboot的web项目 demo-spring-boot-example-06
示例截图,创建项springboot的其他步骤不在详细赘述
2.2.2.引入demo-spring-boot-starter-example-01-1.0-SNAPSHOT.jar测试
2.2.3.创建FormatController进行测试
package com.gaoxinfu.demo.spring.boot.controller;
import com.gaoxinfu.demo.spring.boot.format.HelloFormatTemplate;
import com.gaoxinfu.demo.spring.boot.format.StringFormatProcessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-04-23 07:38
*/
@RestController
public class FormatController {
@GetMapping("/oldInvokeWay")
public String oldInvokeWay(){
HelloFormatTemplate helloFormatTemplate=new HelloFormatTemplate(new StringFormatProcessor());
return helloFormatTemplate.doFormat(helloFormatTemplate);
}
}
2.2.4.启动springboot项目,页面请求验证
2.2.4.1.启动
2.2.4.1.页面验证
2.2.5.总结:无法自动装载bean
2.2.5.1.FormatController中转载说明
可以发现,我们的对象的创建是new出来,并不是引用starter这个项目之后,自动将Bean加载到IOC容器中的,所以我们希望怎么样呢?
2.2.5.2.我们的希望:自动注入,如下方式
但是我们在启动的过程中,会发现实际上spring的IOC容器中并没有这个Bean,所以是无法注入的
那么我们如何做到引入jar之后,自动注入呢?
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
2020-04-23 08:25:00.918 INFO 9652 --- [ main] d.s.b.DemoSpringBootExample06Application : Starting DemoSpringBootExample06Application on localhost with PID 9652 (/Users/gaoxinfu/demo-spring-boot/demo-spring-boot-example-06/target/classes started by gaoxinfu in /Users/gaoxinfu/demo-spring-boot)
2020-04-23 08:25:00.925 INFO 9652 --- [ main] d.s.b.DemoSpringBootExample06Application : No active profile set, falling back to default profiles: default
2020-04-23 08:25:02.291 INFO 9652 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-04-23 08:25:02.301 INFO 9652 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-23 08:25:02.301 INFO 9652 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-23 08:25:02.364 INFO 9652 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-23 08:25:02.365 INFO 9652 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1299 ms
2020-04-23 08:25:02.426 WARN 9652 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'formatController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.gaoxinfu.demo.spring.boot.format.HelloFormatTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
2020-04-23 08:25:02.429 INFO 9652 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-04-23 08:25:02.442 INFO 9652 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-23 08:25:02.574 ERROR 9652 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean of type 'com.gaoxinfu.demo.spring.boot.format.HelloFormatTemplate' that could not be found.
Action:
Consider defining a bean of type 'com.gaoxinfu.demo.spring.boot.format.HelloFormatTemplate' in your configuration.
Disconnected from the target VM, address: '127.0.0.1:60209', transport: 'socket'
Process finished with exit code 1