it-swarm.com.de

Angeforderte Bean wird gerade erstellt: Gibt es einen unlösbaren Zirkelverweis?

ich benutze Frühling 3 und ich habe zwei Bohnen Sichtbereich:

1- Bean1:

@Component("bean1")
@Scope("view")
public class Bean1 {

@Autowired
private Bean2 bean2;

}

2- Bean2:

@Component("bean2")
@Scope("view")
public class Bean2 {

@Autowired
private Bean1 bean1;

}

die Ansicht ist ein benutzerdefinierter Bereich:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="view">
                    <bean class="${project.groupId}.utils.ViewScope" />
                </entry>
            </map>
        </property>
    </bean>

und hier ist der Code für den Bereich der benutzerdefinierten Ansicht:

public class ViewScope implements Scope {

    @SuppressWarnings("rawtypes")
    public Object get(String name, ObjectFactory objectFactory) {
        Map<String, Object> viewMap = FacesContext.getCurrentInstance()
                .getViewRoot().getViewMap();

        if (viewMap.containsKey(name)) {
            return viewMap.get(name);
        } else {
            Object object = objectFactory.getObject();
            viewMap.put(name, object);

            return object;
        }
    }

    public Object remove(String name) {
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap()
                .remove(name);
    }

    public String getConversationId() {
        return null;
    }

    public void registerDestructionCallback(String name, Runnable callback) {
        // Not supported
    }

    public Object resolveContextualObject(String key) {
        return null;
    }
}

beim Öffnen der Seite, die beide Beans verwendet, erhalte ich die Ausnahme:

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'agencyBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:252)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.Java:844)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.Java:786)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.Java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:474)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:282)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.Java:329)
    at com.myapp.utils.ViewScope.get(ViewScope.Java:20)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:325)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.Java:844)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.Java:786)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.Java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:474)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:282)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.Java:329)
    at com.xeno.xecamp.webManagement.utils.ViewScope.get(ViewScope.Java:20)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:325)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:190)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.Java:1075)
    at org.springframework.beans.factory.access.el.SpringBeanELResolver.getValue(SpringBeanELResolver.Java:56)
    at com.Sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.Java:176)
    at com.Sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.Java:203)
    at org.Apache.el.parser.AstIdentifier.getValue(AstIdentifier.Java:71)
    at org.Apache.el.parser.AstValue.getTarget(AstValue.Java:94)
    at org.Apache.el.parser.AstValue.invoke(AstValue.Java:244)
    at org.Apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.Java:278)
    at com.Sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.Java:105)
    at com.Sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.Java:128)
    at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.Java:2508)
    at javax.faces.event.SystemEvent.processListener(SystemEvent.Java:106)
    at com.Sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.Java:2139)
    at com.Sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.Java:2087)
    at com.Sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.Java:286)
    at com.Sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.Java:244)
    at org.springframework.faces.webflow.Jsf2FlowApplication.publishEvent(Jsf2FlowApplication.Java:94)
    at com.Sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.Java:108)
    at com.Sun.faces.lifecycle.Phase.doPhase(Phase.Java:101)
    at com.Sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.Java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.Java:594)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:304)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:210)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.Java:118)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:243)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:368)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:99)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:97)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:60)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.Java:78)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.Java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:35)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.Java:177)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.Java:187)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:57)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:169)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:167)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:243)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:210)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:70)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:243)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:210)
    at org.Apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.Java:684)
    at org.Apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.Java:471)
    at org.Apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.Java:402)
    at org.Apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.Java:329)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.Java:110)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:243)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:368)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:109)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:97)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:100)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.Java:78)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.Java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:35)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.Java:177)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.Java:187)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:79)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:380)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:169)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:167)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:243)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:210)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.Java:198)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:76)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:243)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:210)
    at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:224)
    at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:169)
    at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:472)
    at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:168)
    at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:100)
    at org.Apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.Java:929)
    at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:118)
    at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:405)
    at org.Apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.Java:964)
    at org.Apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.Java:515)
    at org.Apache.Tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.Java:304)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.Java:886)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:908)
    at Java.lang.Thread.run(Thread.Java:619)

bitte geben Sie an, wie Sie diese Ausnahme beheben können.

[~ # ~] edit [~ # ~]: Ich habe versucht, die Setterinjektion zu verwenden, auf die hier in der Antwort verwiesen wird

Grundlegendes zur Spring-Kontextinitialisierungsreihenfolge

aber es funktioniert auch nicht.

1- Bean1:

@Component("bean1")
@Scope("view")
public class Bean1 {


private Bean2 bean2;

@Autowired
public void setBean2 (Bean2 bean2) {
    this.bean2= bean2;
}

}

2- Bean2:

@Component("bean2")
@Scope("view")
public class Bean2 {


private Bean1 bean1;

@Autowired
public void setBean1 (Bean1 bean1) {
    this.bean1= bean1;
}

}

aber es gibt mir die ausnahme:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.myapp.beans.Bean1] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
51
Mahmoud Saleh

Spring verwendet eine spezielle Logik, um diese Art von zirkulären Abhängigkeiten mit Singleton-Beans aufzulösen. Dies gilt jedoch nicht für andere Bereiche. Es gibt keine elegante Möglichkeit, diese zirkuläre Abhängigkeit zu durchbrechen. Eine ungeschickte Option könnte jedoch die folgende sein:

@Component("bean1")
@Scope("view")
public class Bean1 {

    @Autowired
    private Bean2 bean2;

    @PostConstruct
    public void init() {
        bean2.setBean1(this);
    }
}

@Component("bean2")
@Scope("view")
public class Bean2 {

    private Bean1 bean1;

    public void setBean1(Bean1 bean1) {
        this.bean1 = bean1;
    }
}

Auf jeden Fall sind kreisförmige Abhängigkeiten normalerweise ein Symptom für schlechtes Design. Sie würden noch einmal darüber nachdenken, ob es eine bessere Möglichkeit gibt, Ihre Klassenabhängigkeiten zu definieren.

64
sinuhepop

Ich denke, du könntest Spring's gebrauchen

@Faul

anmerkung in einem der automatisch verdrahteten Felder, um die zirkuläre Abhängigkeit zu unterbrechen.

Ich bin mir nicht sicher, ob dies in der genannten Spring-Version funktioniert/existiert.

63
FazoM

In meinem Fall habe ich eine Bean definiert und im Konstruktor derselben Klassendatei automatisch verkabelt.

@SpringBootApplication
public class MyApplication {
    private MyBean myBean;

    public MyApplication(MyBean myBean) {
        this.myBean = myBean;
    }

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

Meine Lösung bestand darin, die Bean-Definition in eine andere Klassendatei zu verschieben.

@Configuration
public CustomConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}
3
Renato Back

ich bin auf ziemlich dumme Weise auf diesen Fehler gestoßen

@Autowired
// private Bean bean;

public void myMethod() {
  return;
}

was passiert ist, ist, dass ich aus irgendeinem Grund eine Zeile kommentiert und die Anmerkung hinterlassen habe, die Spring denken ließ, dass die Methode automatisch verdrahtet werden muss

2
masadwin

Im Allgemeinen können Sie mit zirkulären Abhängigkeiten umgehen, indem Sie Setter-Injection verwenden.

Ich habe den von Ihnen veröffentlichten Setter-Injektionscode ausprobiert, und er hat bei mir funktioniert. Ich würde mir vorstellen, dass Sie die Ausnahme erhalten, weil Bean1 und Bean2 im Paket com.myapp.beans enthalten sind und Sie für dieses Paket keine Komponentenprüfung aktiviert haben.

Sie müssen Ihrer Federkonfiguration Folgendes hinzufügen:

<context:component-scan base-package="com.bullethq.accounts.web"/>

oder verschieben Sie die Bohnen in ein Paket, das von Spring automatisch gescannt wird.

1
John Farrelly