起步依赖
自动配置
条件依赖
Bean参数获取
Bean的发现
bean的加载
总结
自定义starter案例一:hello-spring-boot-starter
pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<artifactId>spring-boot-starter-parentartifactId>
<groupId>org.springframework.bootgroupId>
<groupId>com.malguygroupId>
<artifactId>hello-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
配置属性类HelloProperties
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="hello")
public class HelloProperties {
public String getName() {
public void setName(String name) {
public String getAddress() {
public void setAddress(String address) {
public String toString() {
return "HelloProperties{" +
", address='" + address + '\'' +
服务类(到时boot会自动创建)HelloService
public class HelloService {
public HelloService(String name, String address) {
public String sayHello(){
return "你好,my name is "+name+",我 coming from "+address;
HelloServiceAutoConfiguration
@EnableConfigurationProperties(value=HelloProperties.class)
public class HelloServiceAutoConfiguration {
private HelloProperties helloProperties;
public HelloServiceAutoConfiguration(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
@ConditionalOnMissingBean
public HelloService helloService(){
helloProperties.getName(),
helloProperties.getAddress()
resources目录下创建META-INF/spring.factories , 可以加载自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.malguy.config.HelloServiceAutoConfiguration
这样就可以install再使用了
<project mlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<artifactId>spring-boot-starter-parentartifactId>
<groupId>org.springframework.bootgroupId>
<groupId>org.examplegroupId>
<artifactId>myappartifactId>
<version>1.0-SNAPSHOTversion>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<groupId>com.malguygroupId>
<artifactId>hello-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
@RequestMapping("/hello")
public class HelloController {
private HelloService helloService;
public String sayHello(){
return helloService.sayHello();
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
自定义starter案例二:案例一的基础上添加拦截器,实现记录日志功能
依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<artifactId>spring-boot-starter-parentartifactId>
<groupId>org.springframework.bootgroupId>
<groupId>com.malguygroupId>
<artifactId>hello-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
自定义MyLog注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {
String desc() default "";
创建拦截器对象MyLogInterceptor
public class MyLogInterceptor extends HandlerInterceptorAdapter {
private static final ThreadLocal startTimeThreadLocal=
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
MyLog myLog = method.getAnnotation(MyLog.class);
long startTime = System.currentTimeMillis();
startTimeThreadLocal.set(startTime);
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
MyLog myLog = method.getAnnotation(MyLog.class);
Long startTime = startTimeThreadLocal.get();
long endTime = System.currentTimeMillis();
long optTime=endTime-startTime;
String requestUri= request.getRequestURI();
String methodName=method.getDeclaringClass().getName()+"."+
String methodDesc=myLog.desc();
System.out.println("请求uri: "+requestUri);
System.out.println("请求方法名: "+methodName);
System.out.println("方法描述: "+methodDesc);
System.out.println("方法执行时间: "+optTime+"ms");
super.postHandle(request, response, handler, modelAndView);
自动配置类自动实例化拦截器对象(注册为bean)
public class MyLogAutoConfiguration implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyLogInterceptor());
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.malguy.config.HelloServiceAutoConfiguration,\
com.malguy.config.MyLogAutoConfiguration
重新打包install,在myapp的controller方法上加注解,测试
@RequestMapping("/hello")
public class HelloController {
private HelloService helloService;
@MyLog(desc="sayHello方法")
public String sayHello(){
return helloService.sayHello();