Results 1 to 6 of 6

Thread: Requested bean is currently in creation: Is there an unresolvable circular reference?

  1. #1
    Join Date
    Oct 2011
    Posts
    18

    Default Requested bean is currently in creation: Is there an unresolvable circular reference?

    i am using spring 3, and i have two beans of view scope:

    1- Bean1:

    Code:
       @Component("bean1")
        @Scope("view")
        public class Bean1 {
        
        @Autowired
        private Bean2 bean2;
        
        }
    2- Bean2:


    Code:
    @Component("bean2")
        @Scope("view")
        public class Bean2 {
        
        @Autowired
        private Bean1 bean1;
        
        }
    the view is a custom scope:

    Code:
        <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>
    and here's the code for the custom view scope:

    Code:
    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;
        	}
        }
    when opening the page that uses both beans, i get the exception:

    Code:
      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)

    please advise how to fix this exception.

    EDIT: i tried to use the **setter injection** as referenced in the answer here

    http://stackoverflow.com/questions/5...lization-order

    but it doesn't work too.


    1- Bean1:

    Code:
    @Component("bean1")
        @Scope("view")
        public class Bean1 {
        
        
        private Bean2 bean2;
    
        @Autowired
        public void setBean2 (Bean2 bean2) {
            this.bean2= bean2;
        }
        
        }
    2- Bean2:


    Code:
    @Component("bean2")
        @Scope("view")
        public class Bean2 {
        
        
        private Bean1 bean1;
    
        @Autowired
        public void setBean1 (Bean1 bean1) {
            this.bean1= bean1;
        }
        
        }
    but it gives me the exception:

    Code:
     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: {}

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,630

    Default

    You need to make it an AOP scoped proxy which it currently isn't. Add cglib to your classpath and set proxymode to class on the Scope annotation.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2011
    Posts
    18

    Default

    i have cglib on classpath, so you are saying to change the scope of the beans to be proxy instead of view ?
    remember that this custom view scope equivalent to view scope in jsf.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,630

    Default

    No I'm not saying that. In order for it to properly work you need a proxy which delegates to the view. You do that by specifying a scoped proxy (that is also the way request and session scope work in spring mvc).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Oct 2011
    Posts
    18

    Default

    i need an example or reference please .

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,630

    Default

    I already explained that basically word for word basically. Reference is here, the section for annotations is a bit lower but xml or annotations the sample applies.

    Code:
    @Scope("view", proxyMode = ScopedProxyMode.TARGET_CLASS)
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •