当前位置: 首页 > news >正文

开发公司个人工作总结衡阳seo优化首选

开发公司个人工作总结,衡阳seo优化首选,整形美容网站源码,网站开发用哪些技术ImportSelector接口是至spring中导入内部类或者外部类的核心接口,只需要其定义的方法内返回需要创建bean的class字符串就好了,比如:当我们引入一个外部share包,我们拿到里面的Class返回出去,就能得到这个bean,是多么神…

ImportSelector接口是至spring中导入内部类或者外部类的核心接口,只需要其定义的方法内返回需要创建bean的class字符串就好了,比如:当我们引入一个外部share包,我们拿到里面的Class返回出去,就能得到这个bean,是多么神奇的事情,前提是这个类不是接口哦。

ImportSelector往往结合@Import注解一起使用,可以参考我的这篇文章@Import注解介绍

public interface ImportSelector {//返回类的字符串数组,也就是要创建的bean的className,比如userService.class.getName()//被创建的bean是在其他bean(@Component、@Service等注解修饰的Bean)创建之前创建的String[] selectImports(AnnotationMetadata importingClassMetadata);
}

二、使用案例

通过返回Class的字符串来创建bean

//定义一个业务类
public class UserServiceTest {public String getUserName(){return "测试";}
}//实现ImportSelector接口
public class MyImportSelect implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {//返回要注册到Spring容器的Class集合return new String[]{UserServiceTest.class.getName()};}
}//配置类
@Configuration
@Import(MyImportSelect.class) //导入我们实现ImportSelector的类。
public class AppConfigClassTest {
}

测试

public class MainTest {public static void main(String[] args) {AnnotationConfigApplicationContext AnnotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfigClassTest.class);//这里我们不能通过“userServiceTest”来获取bean,因为这个bean的name不是userServiceTest,而是userServiceTest.getClass().getName()//因为bean的别名成功器,只是针对像注解@Service等注解,会生成一个首字母小写的BeanNameUserServiceTest userServiceTest = AnnotationConfigApplicationContext.getBean(UserServiceTest.class);System.out.println(((UserServiceTest)userServiceTest).getUserName());}
}

下面说下源码是怎么实现的,可以跳转到@Import注解介绍

//这里就直接跳到ConfigurationClassPostProcessor处理@Import注解的逻辑上
private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass, Collection<SourceClass> importCandidates, Predicate<String> exclusionFilter, boolean checkForCircularImports) {//如果importCandidates为空直接return,为什么会有这个,因为下面代码可能会递归调用processImports,就比如Import一个类,这个类也带了@Import注解,那就会在调用一次processImports方法if (importCandidates.isEmpty()) {return;}for (SourceClass candidate : importCandidates) {if (candidate.isAssignable(ImportSelector.class)) {//1、import的类,实现了ImportSelector接口Class<?> candidateClass = candidate.loadClass();//利用反射Class实例化对象,这个对象不是代理对象不要搞混了。ImportSelector selector = ParserStrategyUtils.instantiateClass(candidateClass, ImportSelector.class,this.environment, this.resourceLoader, this.registry);Predicate<String> selectorFilter = selector.getExclusionFilter();if (selectorFilter != null) {exclusionFilter = exclusionFilter.or(selectorFilter);}if (selector instanceof DeferredImportSelector) {this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector);} else {//调用ImportSelector接口里面的selectImports方法,拿到返回值Class集合。在通过递归的方式嗲用processImports挨个解析每一个ClassString[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());//转成SourceClass集合Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames, exclusionFilter);//再次调用processImports方法processImports(configClass, currentSourceClass, importSourceClasses, exclusionFilter, false);}} else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {。。。。。。} else {//3、ImportSelector和ImportBeanDefinitionRegistrar都没有实现this.importStack.registerImport(currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());//进一步解析其他注解,比如@Component @Import等最后会把configClass注册到Spring容器中。processConfigurationClass(candidate.asConfigClass(configClass), exclusionFilter);}}
}

看下instantiateClass方法做了什么

//创建实例对象
static <T> T instantiateClass(Class<?> clazz, Class<T> assignableTo, Environment environment, ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ? ((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());T instance = (T) createInstance(clazz, environment, resourceLoader, registry, classLoader);ParserStrategyUtils.invokeAwareMethods(instance, environment, resourceLoader, registry, classLoader);return instance;
}//调用createInstance方法创建实例对象
private static Object createInstance(Class<?> clazz, Environment environment, ResourceLoader resourceLoader, BeanDefinitionRegistry registry, @Nullable ClassLoader classLoader) {Constructor<?>[] constructors = clazz.getDeclaredConstructors();。。。。。。。return BeanUtils.instantiateClass(clazz);//通过Bean的工具类生成实例对象
}
http://www.ds6.com.cn/news/81137.html

相关文章:

  • 如何组织公司做网站设计网站大全
  • 门户网站整改情况报告推广引流工具
  • 网站开发工资高么精准推广
  • 网站开发摊销年限涟源网站seo
  • 小红书关键词排名怎么做班级优化大师免费下载学生版
  • 陕西网站开发最新足球赛事
  • 网站策划模板福州seo技巧培训
  • 富阳网站建设找哪家网页制作app
  • 动态网站开发实例教程8大营销工具
  • 医疗网站的在线聊天怎么做的鄂尔多斯seo
  • 大型网站seo企业网站建设需求分析
  • wordpress streamango上海最大的seo公司
  • 武汉注册公司流程win10优化大师好用吗
  • 优化大师电脑版官网新网站排名优化怎么做
  • 代做论文毕业设计网站靠谱不如何提升百度关键词排名
  • 做商城网站的公司下载百度语音导航地图
  • 网站建设的开发方式和费用注册网站平台要多少钱
  • 涿州城乡建设委员会的网站长沙官网seo技术
  • 专门做优惠券的网站好的推广平台
  • 南京响应式网站制作建立网站的主要步骤
  • 优化政府门户网站建设百度关键词搜索怎么收费
  • 查看网站是用什么做的北京seo优化wyhseo
  • 临汾网站建设 吕梁网站建设手机百度账号登录入口
  • 徐州做网站建设公司小时seo
  • 求网站建设方法长春网站建设定制
  • 做外贸如何建立网站平台百度电话怎么转人工
  • 郑州 中原区seo包括哪些方面
  • 网络营销服务的种类超级seo外链工具
  • 推广怎么推河南平价的seo整站优化定制
  • 网站首页图怎么做怎样去推广自己的网店