springboot如何实现自动装配源码解读

 更新时间:2020年12月10日 10:31:08   作者:码上代码  
这篇文章主要介绍了springboot如何实现自动装配源码赏析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

Spring Boot 自动装配

最重要的注解@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 {

	@AliasFor(annotation = EnableAutoConfiguration.class)
	Class<?>[] exclude() default {};

	@AliasFor(annotation = EnableAutoConfiguration.class)
	String[] excludeName() default {};

	@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
	String[] scanBasePackages() default {};

	@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
	Class<?>[] scanBasePackageClasses() default {};

	@AliasFor(annotation = Configuration.class)
	boolean proxyBeanMethods() default true;

}

EnableAutoConfiguration

是实现自动装配的核心注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
  
}

getAutoConfigurationMetadata()

@Override
public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {
	Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector,
			() -> String.format("Only %s implementations are supported, got %s",
					AutoConfigurationImportSelector.class.getSimpleName(),
					deferredImportSelector.getClass().getName()));
	AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector)
			.getAutoConfigurationEntry(
					// 加载配置元数据
					getAutoConfigurationMetadata(), annotationMetadata);
	this.autoConfigurationEntries.add(autoConfigurationEntry);
	for (String importClassName : autoConfigurationEntry.getConfigurations()) {
		this.entries.putIfAbsent(importClassName, annotationMetadata);
	}
}


private AutoConfigurationMetadata getAutoConfigurationMetadata() {
	if (this.autoConfigurationMetadata == null) {
		// 加载配置信息
		this.autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
	}
	return this.autoConfigurationMetadata;
}
static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader, String path) {
	try {

	  // 获取资源路径
		Enumeration<URL> urls = (classLoader != null) ? classLoader.getResources(path)
				: ClassLoader.getSystemResources(path);
		Properties properties = new Properties();
		while (urls.hasMoreElements()) {
			properties.putAll(PropertiesLoaderUtils.loadProperties(new UrlResource(urls.nextElement())));
		}
		return loadMetadata(properties);
	}
	catch (IOException ex) {
		throw new IllegalArgumentException("Unable to load @ConditionalOnClass location [" + path + "]", ex);
	}
}

protected static final String PATH = "META-INF/spring-autoconfigure-metadata.properties";

注意: 这个文件在target编译后的文件夹中
自动装配

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

该文件内存有:

Auto Configure

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\

EnableConfigurationProperties

@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

	/**
	 * Database index used by the connection factory.
	 */
	private int database = 0;

	/**
	 * Connection URL. Overrides host, port, and password. User is ignored. Example:
	 * redis://user:password@example.com:6379
	 */
	private String url;

	/**
	 * Redis server host.
	 */
	private String host = "localhost";

	/**
	 * Login password of the redis server.
	 */
	private String password;

	/**
	 * Redis server port.
	 */
	private int port = 6379;

	/**
	 * Whether to enable SSL support.
	 */
	private boolean ssl;

	/**
	 * Connection timeout.
	 */
	private Duration timeout;

	/**
	 * Client name to be set on connections with CLIENT SETNAME.
	 */
	private String clientName;	

}

希望通过本篇对于springboot自动装配的解读,让大家对springboot底层有了一个大致了解,只分析了主要方法,希望对大家有帮助

到此这篇关于springboot如何实现自动装配源码赏析的文章就介绍到这了,更多相关springboot自动装配内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android token过期刷新处理的方法示例

    Android token过期刷新处理的方法示例

    这篇文章主要介绍了Android token过期刷新处理的方法示例,本文详细的介绍了2种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • Mybatis多参数及实体对象传递实例讲解

    Mybatis多参数及实体对象传递实例讲解

    在使用Mybatis的时候,经常会有各种各样的参数传递,不同类型,不同个数的参数,下面小编通过例子给大家讲解下Mybatis多参数及实体对象传递,一起看看吧
    2016-12-12
  • Java向数据库插入中文出现乱码解决方案

    Java向数据库插入中文出现乱码解决方案

    这篇文章主要介绍了Java向数据库插入中文出现乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • Java 设计模式原则之迪米特法则详解

    Java 设计模式原则之迪米特法则详解

    这篇文章主要介绍了Java 设计模式原则之迪米特法则详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Springboot jpa使用sum()函数返回结果如何被接收

    Springboot jpa使用sum()函数返回结果如何被接收

    这篇文章主要介绍了Springboot jpa使用sum()函数返回结果如何接收,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • spring中aop的xml配置方法实例详解

    spring中aop的xml配置方法实例详解

    AOP的配置方式有2种方式:xml配置和AspectJ注解方式。下面这篇文章主要给大家介绍了关于spring中aop的xml配置方法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-07-07
  • Java自定义数组列表的实现操作

    Java自定义数组列表的实现操作

    这篇文章主要介绍了Java自定义数组列表的实现操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 基于java实现websocket代码示例

    基于java实现websocket代码示例

    这篇文章主要介绍了基于java实现websocket代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • IntelliJ IDEA 使用经验总结(推荐)

    IntelliJ IDEA 使用经验总结(推荐)

    这篇文章主要介绍了IntelliJ IDEA 使用经验总结,非常不错,具有参考价值,需要的朋友可以参考下
    2018-02-02
  • 解读Spring-boot的debug调试

    解读Spring-boot的debug调试

    这篇文章主要介绍了解读Spring-boot的debug调试,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12

最新评论