spring类加载到容器的各种方法和类关联关系的注册

3 篇文章 0 订阅
1 篇文章 0 订阅

1. 不需要注册到spring容器

万能的map

从配置文件加载k-v,比如配置完整的接口名称和实现类名称

private static final String DEFAULT_CONFIGURERS_PATH = "CodecConfigurer.properties";

private static final Map<Class<?>, Class<?>> defaultCodecConfigurers = new HashMap<>(4);

static {
   try {
      Properties props = PropertiesLoaderUtils.loadProperties(
            new ClassPathResource(DEFAULT_CONFIGURERS_PATH, CodecConfigurerFactory.class));
      for (String ifcName : props.stringPropertyNames()) {
         String implName = props.getProperty(ifcName);
         Class<?> ifc = ClassUtils.forName(ifcName, CodecConfigurerFactory.class.getClassLoader());
         Class<?> impl = ClassUtils.forName(implName, CodecConfigurerFactory.class.getClassLoader());
         defaultCodecConfigurers.put(ifc, impl);
      }
   }
   catch (IOException | ClassNotFoundException ex) {
      throw new IllegalStateException(ex);
   }
}

public static <T extends CodecConfigurer> T create(Class<T> ifc) {
		Class<?> impl = defaultCodecConfigurers.get(ifc);
		if (impl == null) {
			throw new IllegalStateException("No default codec configurer found for " + ifc);
		}
		return (T) BeanUtils.instantiateClass(impl);
	}
Class<?> instanceClass = ClassUtils.forName(name, classLoader);
				Assert.isAssignable(type, instanceClass);
				Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
				T instance = (T) BeanUtils.instantiateClass(constructor, args);

2. spring容器

spring提供了类实例化,主要包括在xml文件中使用component-scan配置包路径,并使用@Service @Controller @Configuration @Bean等注解,以及spring-boot的@ConditionalOnBean、@ConditionalOnClass、@ConditionalOnProperty等按照条件配置注入的类。spring-boot使用约定大于配置的理念解放了spring繁琐的xml配置。

  • @component

1. org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#postProcessBeanFactory执行doScan

2. org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#findCandidateComponents找到beandefinition之后生成beandefinitionholder并注册到spring容器

  • @configuration

 

  • 第三方jar包中的类注册到spring容器

spring-boot提供了更多的方式,

  • @Import 

Enable*   使用该注解引入配置类

ImportSelector接口   根据不同的入参判断返回bean名称列表

动态注册Bean(实现 ImportBeanDefinitionRegistrar 接口)

一般只要用户确切知道哪些Bean需要放入容器的话,自己可以通过spring 提供的注解来标识就可以了,比如@Component,@Service,@Repository,@Bean等。 如果是不确定的类,或者不是spring专用的,所以并不想用spring的注解进行侵入式标识,那么就可以通过@Import注解,实现ImportBeanDefinitionRegistrar接口来动态注册Bean

 

 

 

参考:

https://www.jianshu.com/p/7317626794fa

https://www.cnblogs.com/wolf-bin/p/11667208.html

https://juejin.im/post/5c761c096fb9a049b41d2299

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值