`
jahu
  • 浏览: 58122 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

spring NoSuchBeanDefinitionException

    博客分类:
  • java
阅读更多

1。这个问题偶尔遇到。再起启动就不报错了。而然昨天报错了很久。spring吞掉异常了。只好一步步调试。发现在创建bean的时候,

会调用 AbstractApplicationContext 类的

protected void assertBeanFactoryActive() {
		if (!this.active.get()) {
			if (this.closed.get()) {
				throw new IllegalStateException(getDisplayName() + " has been closed already");
			}
			else {
				throw new IllegalStateException(getDisplayName() + " has not been refreshed yet");
			}
		}
}

 active的值改成false了。

 

修改方法是

	protected void cancelRefresh(BeansException ex) {
		this.active.set(false);
	}

 

调用方法是

@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}

 原因是在初始化异常bean的时候,有其他的bean创建错误。然后没有终止程序。

 

分享到:
评论

相关推荐

    spring源码解决

    解析了spring的底层实现机制。public interface BeanFactory { //这里是对FactoryBean的转义定义,因为如果使用bean的名字检索FactoryBean得到的对象是工厂生成的对象, //如果需要得到工厂本身,需要转义 String ...

    SPRING API 2.0.CHM

    NoSuchBeanDefinitionException NoSuchMessageException NoSuchRequestHandlingMethodException NotAnAtAspectException NotificationListenerBean NotificationPublisher NotificationPublisherAware ...

    SpringMybatis:springMybatis多数据源

    SpringMybatis Spring / Mybatis多... org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[com.proto.net.aron.aronMapper]的合​​格Bean作为依赖项:至少应有1个有资格作为此依赖

    a road map through nachos

    a road map through nachos,分为 Introduction to Nacho、Nachos Machine、Nachos Threads、User-Level Processes、Nachos Filesystem、Experience With Nachos Assignments、MIPS Architecture等七个部分

    No bean named 'user' available

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'user' available

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    catch (NoSuchBeanDefinitionException nex) { ... } catch (BeansException bex) { ... } catch (Exception ex) { ... } } } FlexFactoryImpl 负责实例化 SpringFactoryInstance 并通过 ...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    catch (NoSuchBeanDefinitionException nex) { ... } catch (BeansException bex) { ... } catch (Exception ex) { ... } } } FlexFactoryImpl 负责实例化 SpringFactoryInstance 并通过 ...

Global site tag (gtag.js) - Google Analytics