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

产品少的电商网站怎么做织梦网站首页文章

产品少的电商网站怎么做,织梦网站首页文章,网站建设 中企动力 扬州,无锡模板网站设计公司前言 前面已经写过循环以来的分析#xff0c;对循环依赖有一些了解#xff0c;【源码解析】Spring循环依赖和三级缓存。简单回顾一下#xff1a; Spring可以解决Autowired注入的循环依赖 Spring解决不了构造器注入的循环依赖 使用Aysnc注解会导致循环依赖。提前暴露的Bea…前言 前面已经写过循环以来的分析对循环依赖有一些了解【源码解析】Spring循环依赖和三级缓存。简单回顾一下 Spring可以解决Autowired注入的循环依赖 Spring解决不了构造器注入的循环依赖 使用Aysnc注解会导致循环依赖。提前暴露的Bean是基础的切面类拦截生成的代理而Bean生成后会调用后置处理器包含AsyncSpring判断两者不一致会报错 问题发现 SpringBoot版本升级到2.6.13后准确来说升级到2.6之后使用如下代码也会报错。 Service public class ServiceA {Autowiredprivate ServiceB serviceB; }Service public class ServiceB {Autowiredprivate ServiceA serviceA; }报错信息如下 Description:The dependencies of some of the beans in the application context form a cycle:┌─────┐ | serviceA (field private com.example.demospring2.cmp.ServiceB com.example.demospring2.cmp.ServiceA.serviceB) ↑ ↓ | serviceB (field private com.example.demospring2.cmp.ServiceA com.example.demospring2.cmp.ServiceB.serviceA) └─────┘Action:Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.需要添加配置spring.main.allow-circular-referencestrue 源码分析 在ServiceA需要ServiceB实例创建ServiceB。ServiceB创建又需要ServiceA核心就在第二次获取ServiceA的时候报错。 AbstractBeanFactory#doGetBean再次获取ServiceA调用DefaultSingletonBeanRegistry#getSingleton(String)方法获取不到进行了第二次的创建。 protected T T doGetBean(String name, Nullable ClassT requiredType, Nullable Object[] args, boolean typeCheckOnly)throws BeansException {String beanName transformedBeanName(name);Object beanInstance;// Eagerly check singleton cache for manually registered singletons.Object sharedInstance getSingleton(beanName);if (sharedInstance ! null args null) {if (logger.isTraceEnabled()) {if (isSingletonCurrentlyInCreation(beanName)) {logger.trace(Returning eagerly cached instance of singleton bean beanName that is not fully initialized yet - a consequence of a circular reference);}else {logger.trace(Returning cached instance of singleton bean beanName );}}beanInstance getObjectForBeanInstance(sharedInstance, name, beanName, null);}else {// Fail if were already creating this bean instance:// Were assumably within a circular reference.if (isPrototypeCurrentlyInCreation(beanName)) {throw new BeanCurrentlyInCreationException(beanName);}// Check if bean definition exists in this factory.BeanFactory parentBeanFactory getParentBeanFactory();if (parentBeanFactory ! null !containsBeanDefinition(beanName)) {// Not found - check parent.String nameToLookup originalBeanName(name);if (parentBeanFactory instanceof AbstractBeanFactory) {return ((AbstractBeanFactory) parentBeanFactory).doGetBean(nameToLookup, requiredType, args, typeCheckOnly);}else if (args ! null) {// Delegation to parent with explicit args.return (T) parentBeanFactory.getBean(nameToLookup, args);}else if (requiredType ! null) {// No args - delegate to standard getBean method.return parentBeanFactory.getBean(nameToLookup, requiredType);}else {return (T) parentBeanFactory.getBean(nameToLookup);}}if (!typeCheckOnly) {markBeanAsCreated(beanName);}StartupStep beanCreation this.applicationStartup.start(spring.beans.instantiate).tag(beanName, name);try {if (requiredType ! null) {beanCreation.tag(beanType, requiredType::toString);}RootBeanDefinition mbd getMergedLocalBeanDefinition(beanName);checkMergedBeanDefinition(mbd, beanName, args);// Guarantee initialization of beans that the current bean depends on.String[] dependsOn mbd.getDependsOn();if (dependsOn ! null) {for (String dep : dependsOn) {if (isDependent(beanName, dep)) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,Circular depends-on relationship between beanName and dep );}registerDependentBean(dep, beanName);try {getBean(dep);}catch (NoSuchBeanDefinitionException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, beanName depends on missing bean dep , ex);}}}// Create bean instance.if (mbd.isSingleton()) {sharedInstance getSingleton(beanName, () - {try {return createBean(beanName, mbd, args);}catch (BeansException ex) {// Explicitly remove instance from singleton cache: It might have been put there// eagerly by the creation process, to allow for circular reference resolution.// Also remove any beans that received a temporary reference to the bean.destroySingleton(beanName);throw ex;}});beanInstance getObjectForBeanInstance(sharedInstance, name, beanName, mbd);}else if (mbd.isPrototype()) {// Its a prototype - create a new instance.Object prototypeInstance null;try {beforePrototypeCreation(beanName);prototypeInstance createBean(beanName, mbd, args);}finally {afterPrototypeCreation(beanName);}beanInstance getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);}else {String scopeName mbd.getScope();if (!StringUtils.hasLength(scopeName)) {throw new IllegalStateException(No scope name defined for bean beanName );}Scope scope this.scopes.get(scopeName);if (scope null) {throw new IllegalStateException(No Scope registered for scope name scopeName );}try {Object scopedInstance scope.get(beanName, () - {beforePrototypeCreation(beanName);try {return createBean(beanName, mbd, args);}finally {afterPrototypeCreation(beanName);}});beanInstance getObjectForBeanInstance(scopedInstance, name, beanName, mbd);}catch (IllegalStateException ex) {throw new ScopeNotActiveException(beanName, scopeName, ex);}}}catch (BeansException ex) {beanCreation.tag(exception, ex.getClass().toString());beanCreation.tag(message, String.valueOf(ex.getMessage()));cleanupAfterBeanCreationFailure(beanName);throw ex;}finally {beanCreation.end();}}return adaptBeanInstance(name, beanInstance, requiredType);}DefaultSingletonBeanRegistry#getSingleton(String, ObjectFactory?)创建实例会进行判断 public Object getSingleton(String beanName, ObjectFactory? singletonFactory) {Assert.notNull(beanName, Bean name must not be null);synchronized (this.singletonObjects) {Object singletonObject this.singletonObjects.get(beanName);if (singletonObject null) {if (this.singletonsCurrentlyInDestruction) {throw new BeanCreationNotAllowedException(beanName,Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!));}if (logger.isDebugEnabled()) {logger.debug(Creating shared instance of singleton bean beanName );}beforeSingletonCreation(beanName);boolean newSingleton false;boolean recordSuppressedExceptions (this.suppressedExceptions null);if (recordSuppressedExceptions) {this.suppressedExceptions new LinkedHashSet();}try {singletonObject singletonFactory.getObject();newSingleton true;}catch (IllegalStateException ex) {// Has the singleton object implicitly appeared in the meantime -// if yes, proceed with it since the exception indicates that state.singletonObject this.singletonObjects.get(beanName);if (singletonObject null) {throw ex;}}catch (BeanCreationException ex) {if (recordSuppressedExceptions) {for (Exception suppressedException : this.suppressedExceptions) {ex.addRelatedCause(suppressedException);}}throw ex;}finally {if (recordSuppressedExceptions) {this.suppressedExceptions null;}afterSingletonCreation(beanName);}if (newSingleton) {addSingleton(beanName, singletonObject);}}return singletonObject;}}DefaultSingletonBeanRegistry#beforeSingletonCreation这是循环依赖的报错。 protected void beforeSingletonCreation(String beanName) {if (!this.inCreationCheckExclusions.contains(beanName) !this.singletonsCurrentlyInCreation.add(beanName)) {throw new BeanCurrentlyInCreationException(beanName);}}这个流程在低版本中肯定是不会报错的。区别在于 SpringApplication#prepareContext会重新设置allowCircularReferences为false。 private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {context.setEnvironment(environment);this.postProcessApplicationContext(context);this.applyInitializers(context);listeners.contextPrepared(context);bootstrapContext.close(context);if (this.logStartupInfo) {this.logStartupInfo(context.getParent() null);this.logStartupProfileInfo(context);}ConfigurableListableBeanFactory beanFactory context.getBeanFactory();beanFactory.registerSingleton(springApplicationArguments, applicationArguments);if (printedBanner ! null) {beanFactory.registerSingleton(springBootBanner, printedBanner);}if (beanFactory instanceof AbstractAutowireCapableBeanFactory) {((AbstractAutowireCapableBeanFactory)beanFactory).setAllowCircularReferences(this.allowCircularReferences);if (beanFactory instanceof DefaultListableBeanFactory) {((DefaultListableBeanFactory)beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);}}if (this.lazyInitialization) {context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());}context.addBeanFactoryPostProcessor(new SpringApplication.PropertySourceOrderingBeanFactoryPostProcessor(context));SetObject sources this.getAllSources();Assert.notEmpty(sources, Sources must not be empty);this.load(context, sources.toArray(new Object[0]));listeners.contextLoaded(context);}AbstractAutowireCapableBeanFactory#doCreateBean。earlySingletonExposure该值为false不会进行三级缓存。 protected Object doCreateBean(String beanName, RootBeanDefinition mbd, Nullable Object[] args)throws BeanCreationException {// Instantiate the bean.BeanWrapper instanceWrapper null;if (mbd.isSingleton()) {instanceWrapper this.factoryBeanInstanceCache.remove(beanName);}if (instanceWrapper null) {instanceWrapper createBeanInstance(beanName, mbd, args);}Object bean instanceWrapper.getWrappedInstance();Class? beanType instanceWrapper.getWrappedClass();if (beanType ! NullBean.class) {mbd.resolvedTargetType beanType;}// Allow post-processors to modify the merged bean definition.synchronized (mbd.postProcessingLock) {if (!mbd.postProcessed) {try {applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,Post-processing of merged bean definition failed, ex);}mbd.postProcessed true;}}// Eagerly cache singletons to be able to resolve circular references// even when triggered by lifecycle interfaces like BeanFactoryAware.boolean earlySingletonExposure (mbd.isSingleton() this.allowCircularReferences isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) {if (logger.isTraceEnabled()) {logger.trace(Eagerly caching bean beanName to allow for resolving potential circular references);}addSingletonFactory(beanName, () - getEarlyBeanReference(beanName, mbd, bean));}// Initialize the bean instance.Object exposedObject bean;try {populateBean(beanName, mbd, instanceWrapper);exposedObject initializeBean(beanName, exposedObject, mbd);}catch (Throwable ex) {if (ex instanceof BeanCreationException beanName.equals(((BeanCreationException) ex).getBeanName())) {throw (BeanCreationException) ex;}else {throw new BeanCreationException(mbd.getResourceDescription(), beanName, Initialization of bean failed, ex);}}if (earlySingletonExposure) {Object earlySingletonReference getSingleton(beanName, false);if (earlySingletonReference ! null) {if (exposedObject bean) {exposedObject earlySingletonReference;}else if (!this.allowRawInjectionDespiteWrapping hasDependentBean(beanName)) {String[] dependentBeans getDependentBeans(beanName);SetString actualDependentBeans new LinkedHashSet(dependentBeans.length);for (String dependentBean : dependentBeans) {if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {actualDependentBeans.add(dependentBean);}}if (!actualDependentBeans.isEmpty()) {throw new BeanCurrentlyInCreationException(beanName,Bean with name beanName has been injected into other beans [ StringUtils.collectionToCommaDelimitedString(actualDependentBeans) ] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using getBeanNamesForType with the allowEagerInit flag turned off, for example.);}}}}// Register bean as disposable.try {registerDisposableBeanIfNecessary(beanName, bean, mbd);}catch (BeanDefinitionValidationException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, Invalid destruction signature, ex);}return exposedObject;}DefaultSingletonBeanRegistry#getSingleton(String, boolean)在第二次获取实例的时候获取不到元素调用的是三级缓存中的ObjectFactory来创建。如果allowCircularReferences为false三级缓存为空不会创建。 Nullableprotected Object getSingleton(String beanName, boolean allowEarlyReference) {// Quick check for existing instance without full singleton lockObject singletonObject this.singletonObjects.get(beanName);if (singletonObject null isSingletonCurrentlyInCreation(beanName)) {singletonObject this.earlySingletonObjects.get(beanName);if (singletonObject null allowEarlyReference) {synchronized (this.singletonObjects) {// Consistent creation of early reference within full singleton locksingletonObject this.singletonObjects.get(beanName);if (singletonObject null) {singletonObject this.earlySingletonObjects.get(beanName);if (singletonObject null) {ObjectFactory? singletonFactory this.singletonFactories.get(beanName);if (singletonFactory ! null) {singletonObject singletonFactory.getObject();this.earlySingletonObjects.put(beanName, singletonObject);this.singletonFactories.remove(beanName);}}}}}}return singletonObject;}
http://www.zqtcl.cn/news/615764/

相关文章:

  • 易语言编程可以做网站么网站备案流程
  • 我想接加工单seo搜索引擎优化工资
  • 西宁做网站君博推荐wordpress如何管理
  • 个人建一个网站多少钱怎样优化网络速度
  • 网站建设项目进度表长春百度seo代理
  • 购物网站排名哪家好免费做房产网站
  • 手机免费建设网站制作南通网站建设排名公司哪家好
  • 做商城网站哪里买企业官网招聘
  • 网站自己做流量互联网营销培训平台
  • 如何查看网站备案官方网站建设状况
  • 做什麽网站有前景软件 开发 公司
  • 淘宝做短视频网站好建设银行代发工资网站
  • 北京建商城网站网站做指向是什么意思
  • 定制网站开发介绍图移动网站适配
  • 青海网站建设怎么建设腾云建站官网
  • 怎样自己做企业的网站gif制作软件app
  • 阿里云建站后台网站建设多少钱合适
  • 自媒体图片素材网站景区网站怎么做的
  • 模块化网站建设江宁做网站
  • 电视网站后台管理系统漏洞淘客推广怎么做
  • 网站建设基础大纲文案丽江网站建设 莱芜
  • 程序员找工作的网站怎么给搞笑网站做文案
  • 网站flsh怎么做能被百度收录的建站网站
  • 娄底网站seo建平台网站费用
  • seo优化网站的注意事项WordPress伪静态公告404
  • 手机网站自动适应沈阳网站建设公司电话
  • 备案号网站下边苏州广告公司招聘
  • 企业网站设计模板js做网站
  • 福州最好的网站建设公司网络策划
  • 威宁做网站西部数码网站管理助手 没有d盘