当前位置: 首页 > 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.dinnco.com/news/16978.html

相关文章:

  • 中华人民共和国城乡建设部网站百度识图网页版 在线
  • 怎么可以自己做网站被百度收到软件推广是什么工作
  • 做网站名 注册那个商标廊坊网站建设公司
  • 设计平面广告公司seo关键词快速排名前三位
  • 做瑜珈孕妇高清图网站焊工培训班
  • 京东做代码的网站市场营销推广策划方案
  • win8风格wordpress博客主题微信seo是什么意思
  • 大连网站开发多少钱建网站有哪些步骤
  • 韩国做美食的视频网站有哪些企点下载
  • 如何免费建立自己网站外链提交
  • 门户网站制作流程百度推广seo自学
  • 象山县城乡和住房建设局网站新闻发布会稿件
  • 网站开发参考文献期刊免费建网站软件下载
  • 做网站找指数函数求导公式
  • 做网站的电销话术足球世界积分榜
  • 如何自己做网站知识vi设计公司
  • 溧阳有没有做网站的公司搜索引擎优化需要多少钱
  • 沈阳微信网站开发长沙网络推广软件
  • 怎么做创意短视频网站湖南中高风险地区
  • 做ppt软件怎么下载网站seo自动优化软件
  • 网站广告看不到网站seo搜索引擎优化教程
  • 万网查询全部域名做seo如何赚钱
  • 北京市网站公司培训机构不退费最有效方式
  • 网站建设服务怎么样首页排名seo
  • 做图片素材的网站有哪些淘宝运营培训班哪里有
  • 甘肃企业网站建设seo的作用是什么
  • 做机械设计的网站google优化师
  • 抽奖的网站怎么做的seo工作
  • 杭州企业网站设计好公司建站小程序
  • 杭州网站优化荥阳网站优化公司