spring循环依赖问题分析
新搞了一个单点登录的项目,用的cas,要把源码的cas-webapp改造成适合我们业务场景的项目,于是新加了一些spring的配置文件。
但是在项目启动时报错了,错误日志如下:
一月 08, 2016 11:01:34 下午 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting filter springSecurityFilterChain org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'functionServiceImpl': Bean with name 'functionServiceImpl' has been injected into other beans [platformServiceImpl] 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 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:548) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) at org.jasig.cas.web.init.SafeContextLoaderListener.contextInitialized(SafeContextLoaderListener.java:75) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5003) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)
去stackoverflow上面搜,说是spring循环依赖导致的。但是问了组里一个技术牛,说如果是出现spring循环依赖,在启动项目的时候是不会报错的,spring的循环依赖在运行的时候才会报错。
循环依赖也可能出现在maven里,如果maven配置中出现了循环依赖,编译的时候就会报错。
那么,究竟是什么原因,我现在还不知道。
在我的web.xml文件中,引入了两次spring的applicationContext.xml文件。
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-configuration/*.xml /WEB-INF/deployerConfigContext.xml classpath:spring/applicationContext.xml </param-value> </context-param>
当我去掉我自己的spring配置时,项目启动就不报错了。
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-configuration/*.xml /WEB-INF/deployerConfigContext.xml </param-value> </context-param>
那是不是因为加载了两个applicationContext.xml,就相当于初始化了两个spring容器,有的单例类只允许存在一个实例,但是却出现了两个容器的bean,所以报错了呢?
具体问题待排查。
问题原因查到了:
由于刚开始配置spring的时候加载了2遍applicationContext.xml,其中有一个动态代理切面的配置在两个配置文件中都存在重复了。详情见:
http://www.cnphp6.com/archives/22031
其实,还是因为bean加载重复了。