为什么仅在生产环境中出现Autowire问题的Spring循环引用?

mepcadol  于 2022-11-21  发布在  Spring
关注(0)|答案(3)|浏览(163)

#所有人请阅读我的问题直到我已经解决这个

Spring循环引用在本地、UAT、演示、STAGGING环境中工作,但不限于生产环境。所有环境都具有相同的配置
1个JDK版本1.7.0.79
2. Tomcat 7.0系统
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="in.test.server">
    </context:component-scan>

    <context:annotation-config />
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="applicationProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath*:mail.properties</value>
                <value>classpath*:document_storage.properties</value>
                <value>classpath*:ekyc.properties</value>
                <value>classpath*:application_config.properties</value>
                <value>classpath*:messages.properties</value>
                <value>classpath*:invitation_codes.properties</value>
                <value>classpath*:capitalraise.properties</value>               
            </list>
        </property>
    </bean>

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath*:mail.properties</value>
                <value>classpath*:application_config.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/testdb_trunk" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        <property name="minIdle" value="10" />
        <property name="initialSize" value="10" />
        <property name="validationQuery" value="SELECT 1 FROM DUAL"/>
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="in.grex.server.datamodel" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="587" />
        <property name="username" value="${mail.mailAccount}" />
        <property name="password" value="${mail.mailAccountPassword}" />

        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>

    <bean id="mailUtil" class="in.grex.server.common.util.MailUtil">
        <property name="mailSender" ref="mailSender" />
    </bean>

    <!-- Bean for dozer mapper. -->
    <bean id="mapper" class="org.dozer.DozerBeanMapper" scope="singleton">
        <property name="mappings">
            <list>
                <bean class="in.grex.server.dto.mapper.GrexDozerMapper" />
            </list>
        </property>
    </bean>

    <!-- Bean for dozer searchmapper. -->
    <bean id="searchMapper" class="org.dozer.DozerBeanMapper" scope="singleton">
        <property name="mappings">
            <list>
                <bean class="in.grex.server.dto.mapper.GrexDozerSearchMapper" />
            </list>
        </property>
    </bean>

    </beans>

堆栈跟踪

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSearchService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.dto.mapper.CustomGrexMapper in.grex.server.search.DataSearchServiceImpl.customGrexMapper; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customGrexMapper': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organizationassociation.OrganizationAssociationService in.grex.server.dto.mapper.CustomGrexMapperImpl.organizationAssociationService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationAssociationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organization.OrganizationService in.grex.server.services.organizationassociation.OrganizationAssociationServiceImpl.organizationService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'organizationService': Bean with name 'organizationService' has been injected into other beans [utilityService,organizationUserService,companyService,messageService,investorService] 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    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:410)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1081)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1877)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    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:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.dto.mapper.CustomGrexMapper in.grex.server.search.DataSearchServiceImpl.customGrexMapper; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customGrexMapper': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organizationassociation.OrganizationAssociationService in.grex.server.dto.mapper.CustomGrexMapperImpl.organizationAssociationService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationAssociationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organization.OrganizationService in.grex.server.services.organizationassociation.OrganizationAssociationServiceImpl.organizationService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'organizationService': Bean with name 'organizationService' has been injected into other beans [utilityService,organizationUserService,companyService,messageService,investorService] 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 26 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customGrexMapper': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organizationassociation.OrganizationAssociationService in.grex.server.dto.mapper.CustomGrexMapperImpl.organizationAssociationService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationAssociationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organization.OrganizationService in.grex.server.services.organizationassociation.OrganizationAssociationServiceImpl.organizationService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'organizationService': Bean with name 'organizationService' has been injected into other beans [utilityService,organizationUserService,companyService,messageService,investorService] 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    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.findAutowireCandidates(DefaultListableBeanFactory.java:912)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 28 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organizationassociation.OrganizationAssociationService in.grex.server.dto.mapper.CustomGrexMapperImpl.organizationAssociationService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationAssociationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organization.OrganizationService in.grex.server.services.organizationassociation.OrganizationAssociationServiceImpl.organizationService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'organizationService': Bean with name 'organizationService' has been injected into other beans [utilityService,organizationUserService,companyService,messageService,investorService] 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 39 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organizationAssociationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organization.OrganizationService in.grex.server.services.organizationassociation.OrganizationAssociationServiceImpl.organizationService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'organizationService': Bean with name 'organizationService' has been injected into other beans [utilityService,organizationUserService,companyService,messageService,investorService] 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    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.findAutowireCandidates(DefaultListableBeanFactory.java:912)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 41 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.grex.server.services.organization.OrganizationService in.grex.server.services.organizationassociation.OrganizationAssociationServiceImpl.organizationService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'organizationService': Bean with name 'organizationService' has been injected into other beans [utilityService,organizationUserService,companyService,messageService,investorService] 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 52 more
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'organizationService': Bean with name 'organizationService' has been injected into other beans [utilityService,organizationUserService,companyService,messageService,investorService] 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.findAutowireCandidates(DefaultListableBeanFactory.java:912)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 54 more
nbysray5

nbysray51#

我不久前就遇到了这个问题。Spring在连接依赖关系的顺序上有点随机,所以在某些情况下,应用程序会启动...而在生产中,它会因为这个错误而失败。
问题是一个非常糟糕的做法,由 Spring 推广,直到一两年前...问题是什么?使用setter / @Autowired对字段进行注入,而不是构造函数注入。我们遇到的问题是,我们正在缓慢地将配置迁移到构造函数注入,我们向这个问题敞开了自己,因为我们混合了这两种情况。这个问题取决于先示例化哪个bean。我现在记不清了,但我认为如果先示例化使用构造函数注入的bean,就会出现错误,但是如果先示例化使用setter注入的bean,就会发生错误。在这种情况下,我们应该一次性完成所有工作。当所有东西都使用构造函数注入时,这个问题会在所有环境中重现。
解决方案很简单:依赖循环是类设计问题的一个症状。你必须找出循环是什么并切断它。这不是Spring的问题,而是你的应用程序的问题。

kq4fsx7k

kq4fsx7k2#

刚才我已经遇到了这个问题,通过将所需的方法移动到一个类中并使用@service注解将其公开来解决这个问题,因此这个问题已经解决了。这种方法可能很幼稚,但解决了问题。
使用所需的所有方法/函数初始化:

@ Service
public class Class1
{
    /% Method 1 %/
    /% Method 2 %/
    /% Method 3 %/
    /% Method 4 %/
}

自动接线1类。

public class Class2
{
    @ Autowired
    private Class1 class1;
    /% class1.Method1() %/
    /% class1.Method1() %/
    .
    .
    .
}
htzpubme

htzpubme3#

对构造函数依赖注入使用@Lazy注解。

相关问题