|
From: Dmitriy K. <dko...@ru...> - 2004-07-13 14:59:02
|
Ross,
I investigated the issue and found that FactoryBean and
BeanPostProcessor do not work well together. I've created a very simple
test case to prove that. The reason is that in
AbstractApplicationContext.registerBeanPostProcessors() it calls the
*getBean()* to populate the internal Collection of BeanPostProcessors
and then register them "globally" by casting to ... well,
BeanPostProcessor. In case of FactoryBean, getBean() call actually gets
the underlying instance of the bean, not the FactoryBean itself,
therefore resulting in ClastCastException:
private void registerBeanPostProcessors() throws BeansException {
String[] beanNames =
getBeanDefinitionNames(BeanPostProcessor.class);
if (beanNames.length > 0) {
List beanProcessors = new ArrayList();
for (int i = 0; i < beanNames.length; i++) {
beanProcessors.add(getBean(beanNames[i]));
}
Collections.sort(beanProcessors, new OrderComparator());
for (Iterator it = beanProcessors.iterator(); it.hasNext();) {
getBeanFactory().addBeanPostProcessor((BeanPostProcessor)it.next());
}
}
}
I've changed this code to take into consideration the
BeanPostProcessor-FactoryBean combination:
private void registerBeanPostProcessors() throws BeansException {
String[] beanNames =
getBeanDefinitionNames(BeanPostProcessor.class);
if (beanNames.length > 0) {
List beanProcessors = new ArrayList();
for (int i = 0; i < beanNames.length; i++) {
try{
beanProcessors.add(getBean("&" + beanNames[i]));
//Check for FactoryBean
}
catch(BeanIsNotAFactoryException e){
beanProcessors.add(getBean(beanNames[i])); //It's
not the FactoryBean
}
}
Collections.sort(beanProcessors, new OrderComparator());
for (Iterator it = beanProcessors.iterator(); it.hasNext();) {
getBeanFactory().addBeanPostProcessor((BeanPostProcessor)it.next());
}
}
}
Works well.
Juergen, if there is a compelling need for this feature (BPP/FB
combination) I'd be happy to commit this.
Regards,
Dmitriy.
Mason, Ross wrote:
>Dmitriy,
>
>sorry for the delay :)
>
>appCtx -
>http://cvs.sourceforge.net/viewcvs.py/mule/mule-extras/spring/src/test/conf/mule-events-app-context.xml?rev=1.3&view=auto
>
>BPP
>
>http://cvs.sourceforge.net/viewcvs.py/mule/mule-extras/spring/src/java/org/mule/extras/spring/config/AutowireUMOManagerFactoryBean.java?rev=1.1&view=auto
>
>Note that this factory bean now doesn't implement BPP because of the ClassCastException. however I tried implementating FactoryBean and BeanPostProcessor in an isolated test and I still got the exception.
>
>Cheers,
>
>Ross
>
>
>
>
>
>
>>-----Original Message-----
>>From: spr...@li...
>>[mailto:spr...@li...]On Behalf
>>Of Dmitriy Kopylenko
>>Sent: Friday, 9 July 2004 1:27 AM
>>To: spr...@li...
>>Subject: Re: [Springframework-developer] Intercepting bean creation?
>>
>>
>>Ross,
>>
>>can you please attach this BPP and the corresponding appCtx descriptor?
>>
>>Thanks,
>>Dmitriy.
>>
>>Mason, Ross wrote:
>>
>>
>>
>>>Cheers guys. I'm using the BeanPostProcessor.
>>>
>>>When I try using the BeanPostProcessor on a FactoryBean I get
>>>
>>>
>>a ClassCastException. Which at first made sense because I
>>figured it was trying to cast my actual bean instance (not
>>FactoryBean) to a BeanPostProcessor, but then why wouldn't it
>>use the actual factory instance. Anyway here is the stack trace -
>>
>>
>>>java.lang.ClassCastException
>>>at
>>>
>>>
>>org.springframework.context.support.AbstractApplicationContext.
>>registerBeanPostProcessors(AbstractApplicationContext.java:328)
>>
>>
>>>at
>>>
>>>
>>org.springframework.context.support.AbstractApplicationContext.
>>refresh(AbstractApplicationContext.java:264)
>>
>>
>>>at
>>>
>>>
>>org.springframework.context.support.ClassPathXmlApplicationCont
>>ext.<init>(ClassPathXmlApplicationContext.java:58)
>>
>>
>>>at
>>>
>>>
>>org.mule.extras.spring.config.SpringConfigurationBuilder.config
>>ure(SpringConfigurationBuilder.java:40)
>>
>>
>>>at
>>>
>>>
>>org.mule.tck.AbstractConfigBuilderTestCase.setUp(AbstractConfig
>>BuilderTestCase.java:53)
>>
>>
>>>at
>>>
>>>
>>com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter
>>.java:31)
>>
>>
>>>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>at
>>>
>>>
>>sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccesso
>>rImpl.java:39)
>>
>>
>>>at
>>>
>>>
>>sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMetho
>>dAccessorImpl.java:25)
>>
>>
>>>at
>>>
>>>
>>com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
>>
>>
>>>Cheers,
>>>
>>>Ross
>>>
>>>-----Original Message-----
>>>From: spr...@li...
>>>
>>>
>>on behalf of jürgen höller [werk3AT]
>>
>>
>>>Sent: Thu 8/07/2004 10:45 AM
>>>To: spr...@li...
>>>Cc:
>>>Subject: Re: [Springframework-developer] Intercepting bean creation?
>>>
>>>
>>>
>>> I recommend BeanNameAware too, if you just want to
>>>
>>>
>>become aware of the bean name.
>>
>>
>>>
>>> For general post-processing of a newly created bean
>>>
>>>
>>instance, you could define a BeanPostProcessor in the context,
>>which will automatically intercept all beans that this context creates:
>>
>>
>>>
>>>
>>>
>>>
>>http://www.springframework.org/docs/api/org/springframework/bea
>>ns/factory/config/BeanPostProcessor.html
>>
>>
>>>
>>> Juergen
>>>
>>>
>>> -----Original Message-----
>>> From: spr...@li...
>>>
>>>
>>>
>>[mailto:spr...@li...]On Behalf
>>
>>
>>> Of Chistophe Vanfleteren
>>> Sent: Thursday, July 08, 2004 11:26 AM
>>> To: spr...@li...
>>> Subject: Re: [Springframework-developer] Intercepting
>>>
>>>
>>bean creation?
>>
>>
>>>
>>>
>>> On Thursday 08 July 2004 11:12, Mason, Ross wrote:
>>> > Hi,
>>> > I create a lot of different types of object in an
>>>
>>>
>>applicationContext, all
>>
>>
>>> > of which have a name property i.e.
>>> >
>>> > <bean id="jmsConnector"
>>>
>>>
>>className="org.mule.providers.jms.JmsConnector">
>>
>>
>>> > <property
>>>
>>>
>>name="name"><value>jmsConnector</value></property>
>>
>>
>>> > </bean>
>>> >
>>> > I don't like having to include the name property if
>>>
>>>
>>there is a chance I
>>
>>
>>> > could use the bean id instead. Is there any way I can
>>>
>>>
>>uniformly intercept
>>
>>
>>> > bean creation in the context so that straight after
>>>
>>>
>>the constructor is
>>
>>
>>> > called I can get hold of the bean id and set it on my object?
>>> >
>>> > I can't wait until the applicationContext is
>>>
>>>
>>configured and then set the
>>
>>
>>> > names as some objects have a collection of other
>>>
>>>
>>objects which are keyed
>>
>>
>>> > against the name.
>>> >
>>>
>>> You could implement BeanNameAware:
>>>
>>>
>>>
><http://www.springframework.org/docs/api/org/springframework/beans/factory/BeanNameAware.html>
>
>
>>
>> --
>> Kind regards,
>> Christophe Vanfleteren
>>
>>
>> -------------------------------------------------------
>> This SF.Net email sponsored by Black Hat Briefings & Training.
>> Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
>> digital self defense, top technical experts, no vendor pitches,
>> unmatched networking opportunities. Visit www.blackhat.com
>> _______________________________________________
>> Springframework-developer mailing list
>> Spr...@li...
>> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>>
>>
>> -------------------------------------------------------
>> This SF.Net email sponsored by Black Hat Briefings & Training.
>> Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
>> digital self defense, top technical experts, no vendor pitches,
>> unmatched networking opportunities. Visit www.blackhat.com
>> _______________________________________________
>> Springframework-developer mailing list
>> Spr...@li...
>> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>>
>>
>>
>>
>>
>>
>
>
>
>-------------------------------------------------------
>This SF.Net email sponsored by Black Hat Briefings & Training.
>Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
>digital self defense, top technical experts, no vendor pitches,
>unmatched networking opportunities. Visit www.blackhat.com
>_______________________________________________
>Springframework-developer mailing list
>Spr...@li...
>https://lists.sourceforge.net/lists/listinfo/springframework-developer
>N�HS^�隊[)�{(��[�ZrAڴ�y���j)�+�)࢚h�ۧ�؞�X���0�ZrHZ��&J���jg���z������x%��R�����ڙ�(�G^��h����l���q���z�m��?�X���(��~��zw��X�����b��?����jg���z��
>
|