Announcement Announcement Module
Collapse
No announcement yet.
Requested bean is currently in creation: Is there an unresolvable circular reference? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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).

        Comment


        • #5
          i need an example or reference please .

          Comment


          • #6
            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)

            Comment

            Working...
            X