Back Forum Reply New

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=quot;myBeanquot;)
@Scope(quot;prototypequot;)
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(quot;myBeanquot;, 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, Classlt;Tgt;, Object[], boolean) line: 288
DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 190
DefaultListableBeanFactory.findAutowireCandidates(String, Class, DependencyDescriptor) line: 827
DefaultListableBeanFactory.doResolveDependency(DependencyDescriptor, Classlt;?gt;, String, Setlt;Stringgt;, TypeConverter) line: 769
DefaultListableBeanFactory.resolveDependency(DependencyDescriptor, String, Setlt;Stringgt;, TypeConverter) line: 686
ConstructorResolver.resolveAutowiredArgument(MethodParameter, String, Setlt;Stringgt;, 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, Classlt;Tgt;, 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       lt;lt;lt;explicit arguments disappear here gt;gt;gt;
--gt;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, Classlt;Tgt;, Object[], boolean) line: 310
DefaultListableBeanFactory(AbstractBeanFactory).getBean(String, Object...) line: 198
XmlWebApplicationContext(AbstractApplicationContext).getBean(String, Object...) line: 1057
OtherType.afterPropertiesSet() line: 806If 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); }
}
¥
Back Forum Reply New