Announcement Announcement Module
Collapse
No announcement yet.
java config with parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • java config with parameters

    Hi,

    I'm attempting to use the java based wiring mechanism, but encountering a problem when the method requires a parameter.

    Here's a mock of my configuration:

    Code:
    	
    @Bean(name="myBean")
    @Scope("prototype")
    public BeanType myBean(OtherType argument) {
    	BeanType myBean = new BeanType(argument);  		
    	return myBean;
    }
    In my code for OtherType(not yet converted to java config, so therefore I'm doing something a tad ugly):

    Code:
    public final void afterPropertiesSet() throws Exception {
         BeanType myBean = (BeanType)getApplicationContext().getBean("myBean", this);
    }
    The problem seems to be that the argument ('this' referencing an OtherType instance) is getting carried along by the bean factory, but does something.. bad. It drops the args in the ConfigurationClassEnhancer$BeanMethodInterceptor.i ntercept(Object, Method, Object[], MethodProxy) line: 210. and goes on to try to create my prototype using some random OtherType entity. That other random OtherType, then, of course, attempts to use 'MyBean' and I get a 'bean under construction, circular reference' problem.

    My problem is two fold.

    1. ConfigurationClassEnhancer$BeanMethodInterceptor.i ntercept is not honoring my args, nor the scope of the bean. But maybe it shouldn't have to because...

    2. This offending method shouldn't be being called at all. The bean is declared as a prototype, so why are we calling a method to (quote from javadocs) 'to check the supplied BeanFactory for the existence of this bean object'?

    Code:
    	DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 288	
    	DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 190	
    	DefaultListableBeanFactory.findAutowireCandidates(String, Class, DependencyDescriptor) line: 827	
    	DefaultListableBeanFactory.doResolveDependency(DependencyDescriptor, Class<?>, String, Set<String>, TypeConverter) line: 769	
    	DefaultListableBeanFactory.resolveDependency(DependencyDescriptor, String, Set<String>, TypeConverter) line: 686	
    	ConstructorResolver.resolveAutowiredArgument(MethodParameter, String, Set<String>, TypeConverter) line: 788	
    	ConstructorResolver.createArgumentArray(String, RootBeanDefinition, ConstructorArgumentValues, BeanWrapper, Class[], String[], Object, boolean) line: 708	
    	ConstructorResolver.instantiateUsingFactoryMethod(String, RootBeanDefinition, Object[]) line: 449	
    	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).instantiateUsingFactoryMethod(String, RootBeanDefinition, Object[]) line: 973	
    	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBeanInstance(String, RootBeanDefinition, Object[]) line: 879	
    	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 485	
    	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 456	
    	DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 310	
    	DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 190	
    	ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 206	
    	AbstractControllerConfiguration$$EnhancerByCGLIB$$20815de4.moduleBasedServiceLocator(ModuleAware) line: not available	
    	AbstractControllerConfiguration$$EnhancerByCGLIB$$20815de4(AbstractControllerConfiguration).moduleBasedServiceInvoker(ModuleAware) line: 59	
    	AbstractControllerConfiguration$$EnhancerByCGLIB$$20815de4.CGLIB$moduleBasedServiceInvoker$2(ModuleAware) line: not available	
    	AbstractControllerConfiguration$$EnhancerByCGLIB$$20815de4$$FastClassByCGLIB$$22e8327.invoke(int, Object, Object[]) line: not available	
    	MethodProxy.invokeSuper(Object, Object[]) line: 215	
            <<<explicit arguments disappear here >>>
    -->	ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 210	
    	AbstractControllerConfiguration$$EnhancerByCGLIB$$20815de4.moduleBasedServiceInvoker(ModuleAware) line: not available	
    	NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]	
    	NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39	
    	DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25	
    	Method.invoke(Object, Object...) line: 597	
    	CglibSubclassingInstantiationStrategy(SimpleInstantiationStrategy).instantiate(RootBeanDefinition, String, BeanFactory, Object, Method, Object[]) line: 146	
    	ConstructorResolver.instantiateUsingFactoryMethod(String, RootBeanDefinition, Object[]) line: 557	
    	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).instantiateUsingFactoryMethod(String, RootBeanDefinition, Object[]) line: 973	
    	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBeanInstance(String, RootBeanDefinition, Object[]) line: 879	
    	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 485	
    	DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 456	
    	DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 310	
    	DefaultListableBeanFactory(AbstractBeanFactory).getBean(String, Object...) line: 198	
    	XmlWebApplicationContext(AbstractApplicationContext).getBean(String, Object...) line: 1057	
    	OtherType.afterPropertiesSet() line: 806

  • #2
    If OtherType is a managed bean by the Spring container, then just autowire that in the @Configuration class:

    Code:
    @Configuration
    public class AppConfig
    {
      @Autowired
      private OtherType otherType;
    
      @Bean
      public BeanType myBean()
      {
        return new BeanType(otherType);
      }
    }

    Comment

    Working...
    X