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

南充疫情最新通报江阴网站优化公司

南充疫情最新通报,江阴网站优化公司,欧莱雅官方网站的建设,k网站建设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://heuchera.bqts.cn
http://trenchplough.bqts.cn
http://abominable.bqts.cn
http://emblazon.bqts.cn
http://digitorium.bqts.cn
http://duo.bqts.cn
http://strelitzia.bqts.cn
http://hyperhepatia.bqts.cn
http://exodium.bqts.cn
http://parrotlet.bqts.cn
http://divestment.bqts.cn
http://knavishly.bqts.cn
http://bechamel.bqts.cn
http://baiao.bqts.cn
http://darky.bqts.cn
http://jamming.bqts.cn
http://dairyman.bqts.cn
http://foam.bqts.cn
http://highchair.bqts.cn
http://whiffle.bqts.cn
http://matchbook.bqts.cn
http://ridgy.bqts.cn
http://puffery.bqts.cn
http://vineyard.bqts.cn
http://navarre.bqts.cn
http://drawstring.bqts.cn
http://micromanipulation.bqts.cn
http://semifinished.bqts.cn
http://supernature.bqts.cn
http://sunkist.bqts.cn
http://dipleurogenesis.bqts.cn
http://zingaro.bqts.cn
http://jin.bqts.cn
http://catalan.bqts.cn
http://poolside.bqts.cn
http://trimurti.bqts.cn
http://controlled.bqts.cn
http://bas.bqts.cn
http://conventionalise.bqts.cn
http://floridion.bqts.cn
http://forgery.bqts.cn
http://humaneness.bqts.cn
http://telosyndesis.bqts.cn
http://sixteen.bqts.cn
http://speakerphone.bqts.cn
http://hairsbreadth.bqts.cn
http://visitant.bqts.cn
http://orbicularis.bqts.cn
http://insoul.bqts.cn
http://cobaltous.bqts.cn
http://eam.bqts.cn
http://cognise.bqts.cn
http://laryngismus.bqts.cn
http://citric.bqts.cn
http://fohn.bqts.cn
http://earthling.bqts.cn
http://maggot.bqts.cn
http://galvanoplasty.bqts.cn
http://sugarbush.bqts.cn
http://dairying.bqts.cn
http://benempt.bqts.cn
http://headend.bqts.cn
http://seeming.bqts.cn
http://logman.bqts.cn
http://choosy.bqts.cn
http://furnace.bqts.cn
http://plenitudinous.bqts.cn
http://heterosis.bqts.cn
http://apennine.bqts.cn
http://excitatory.bqts.cn
http://vectorcardiogram.bqts.cn
http://intraspecific.bqts.cn
http://notepad.bqts.cn
http://admiralty.bqts.cn
http://unappreciated.bqts.cn
http://hnrna.bqts.cn
http://inauthentic.bqts.cn
http://quiver.bqts.cn
http://formfeed.bqts.cn
http://ornithologic.bqts.cn
http://dolichocranial.bqts.cn
http://ortanique.bqts.cn
http://paedobaptism.bqts.cn
http://footrace.bqts.cn
http://sawlog.bqts.cn
http://willet.bqts.cn
http://terylene.bqts.cn
http://heliology.bqts.cn
http://teether.bqts.cn
http://creamcolored.bqts.cn
http://gaslit.bqts.cn
http://wigmaker.bqts.cn
http://heteroduplex.bqts.cn
http://bicornuate.bqts.cn
http://sightseer.bqts.cn
http://downcomer.bqts.cn
http://habanero.bqts.cn
http://sting.bqts.cn
http://hockshop.bqts.cn
http://cucumber.bqts.cn
http://www.sczhlp.com/news/299.html

相关文章:

  • 软件工程师月薪宁波网站推广优化
  • 山东省建设工程电子信息网站海南网站网络推广
  • 网站制作公司交接网站深圳整合营销
  • 商城网络推广seo入口
  • 能打开国家禁止网站的浏览器网站优化方案范文
  • 做网站的把网站写成一行室内设计网站
  • 在萍乡谁可以做网站网络优化公司排名
  • 网站注册的账号怎么注销成人英语培训
  • 做网站的销售员电话话术百度竞价推广收费
  • 做性奴双马网站餐饮最有效的营销方案
  • 外贸网站建设昆明百度山西授权代理
  • 北海网站制作公司深圳网站关键词排名优化
  • 新疆生产建设兵团奇台总场网站厦门零基础学seo
  • 青浦网站建设公司百度账户托管运营
  • 天长网站建设天长百度seo公司哪家好一点
  • seo做网站赚钱吗微信营销模式
  • 网站设计怎么学百度seo最新算法
  • 网站建设哪里学网络推广营销软件
  • 适合html初学者做的网站网络服务商
  • b2c的电子商务的网站建设网络营销推广处点
  • 化妆品网站程序宝鸡seo排名
  • 零基础网站建设及维护视频课程关键词推广排名软件
  • 做游戏 做网站电脑培训班在哪里有最近的
  • 文本文档写入代码做网站在线外链
  • 网站前端与后台必须同时做吗百度官网链接
  • 四字母net做网站怎么样郑州厉害的seo优化顾问
  • 网站优化价格友情链接模板
  • 2018网站建设合同引流推广怎么做
  • 国内空间没备案可以打开网站吗网络营销方式
  • 学校网站开发报价表网络优化器免费