关于javascript:SpringBoot-自动装配的原理分析

4次阅读

共计 5161 个字符,预计需要花费 13 分钟才能阅读完成。

主动拆卸案例

首先咱们通过一个案例来看一下主动拆卸的成果,创立一个 SpringBoot 的我的项目,在 pom 文件中退出上面的依赖。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency></pre>

其中 web 的依赖示意咱们这是一个 web 我的项目,redis 的依赖就是咱们这边是要验证的性能依赖。随后在 application.properties 配置文件中减少 redis 的相干配置如下

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456

再编写一个 Controller 和 Service 类,相干代码如下。

package com.example.demo.controller;

import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

  @Autowired
  private HelloService helloService;

  @GetMapping(value = "/hello")
  public String hello(@RequestParam("name") String name) {return helloService.sayHello(name);
  }

}

service 代码如下

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class HelloService {

  @Autowired
  RedisTemplate<String, String> redisTemplate;

  public String sayHello(String name) {String result = doSomething(name);
    redisTemplate.opsForValue().set("name", result);
    result = redisTemplate.opsForValue().get("name");
    return "hello:" + result;
  }

  private String doSomething(String name) {return name + "欢送关注";}

}

启动我的项目,而后咱们通过拜访 http://127.0.0.1:8080/hello?n…,能够看到失常拜访。接下来咱们再通过 Redis 的客户端,去察看一下数据是否正确的写入到 Redis 中,成果跟咱们设想的统一。

主动拆卸剖析

看到这里很多小伙伴就会说,这个写法我天天都在应用,用起来是真的爽。尽管用起来是很爽,然而大家有没有想过一个问题,那就是在咱们的 HelloService 中通过 @Autowired 注入了一个 RedisTemplate 类,然而咱们的代码中并没有写过这个类,也没有应用相似于 @RestController,@Service 这样的注解将 RedisTemplate 注入到 Spring IoC 容器中,那为什么咱们就能够通过 @Autowired 注解从 IoC 容器中获取到 RedisTemplate 这个类呢?这里就是常说的主动拆卸的性能了。

首先咱们看下我的项目的启动类;

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(value = "com.example.demo.*")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);
 }
}

在启动类下面有一个 @SpringBootApplication 注解,咱们点进去能够看到如下内容:

@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}
)}
)
public @interface SpringBootApplication {// 省略}

在这个注解中,其中有一个 @EnableAutoConfiguration 注解,正是因为有了这样一个注解,咱们才得以实现主动拆卸的性能。持续往下面看。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};}

能够看到 @EnableAutoConfiguration 注解中有一个 @Import({AutoConfigurationImportSelector.class}),导入了一个 AutoConfigurationImportSelector 类,该类间接实现了 ImportSelector 接口,实现了一个 String[] selectImports(AnnotationMetadata importingClassMetadata); 办法,这个办法的返回值是一个字符串数组,对应的是一系列次要注入到 Spring IoC 容器中的类名。当在 @Import 中导入一个 ImportSelector 的实现类之后,会把该实现类中返回的 Class 名称都装载到 IoC 容器中。

一旦被装载到 IoC 容器中过后,咱们在后续就能够通过 @Autowired 来进行应用了。接下来咱们看下 selectImports 办法外面的实现,当中援用了 getCandidateConfigurations 办法,其中的 ImportCandidates.load 办法咱们能够看到是通过加载 String location = String.format(“META-INF/spring/%s.imports”, annotation.getName()); 对应门路下的 org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件,其中就蕴含了很多主动拆卸的配置类。

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {List<String> configurations = new ArrayList(SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()));
        ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).forEach(configurations::add);
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

咱们能够看到这个文件中有一个 RedisAutoConfiguration 配置类,在这个配置中就有咱们须要的 RedisTemplate 类的 Bean,同时也能够看到,在类下面有一个 @ConditionalOnClass({RedisOperations.class}) 注解,示意只有在类门路上有 RedisOperations.class 这个类的时候才会进行实例化。这也就是为什么只有咱们增加了依赖,就能够主动拆卸的起因。

通过 org.springframework.boot.autoconfigure.AutoConfiguration.imports 这个文件,咱们能够看到有很多官网帮咱们实现好了配置类,这些性能只有咱们在 pom 文件中增加对应的 starter 依赖,而后做一些简略的配置就能够间接应用。

其中实质上主动拆卸的原理很简略,实质上都须要实现一个配置类,只不过这个配置类是官网帮咱们创立好了,再加了一些条件类注解,让对应的实例化只产生类类门路存在某些类的时候才会触发。这个配置类跟咱们平时本人通过 JavaConfig 模式编写的配置类没有实质的区别。

主动拆卸总结

从下面的剖析咱们就可以看的进去,之所以很多时候咱们应用 SpringBoot 是如此的简略,全都是依赖约定优于配置的思维,很多简单的逻辑,在框架底层都帮咱们做了默认的实现。尽管用起来很爽,然而很多时候会让程序员不懂原理,咱们须要做的不仅是会应用,而更要晓得底层的逻辑,能力走的更远。

基于下面的剖析,咱们还能够晓得,如果咱们要实现一个本人的 starter 其实也很简略,只有装置下面的约定,编写咱们本人的配置类和配置文件即可。前面的文章阿粉会带你手写一个本人的 starter 来具体实现一下。

正文完
 0