I have a simplePOJO class which has been decorated using an introduction advisor. Each time I need a reference on my decorated simplePOJO, I want a different instance. Is it possible???????
thanks
I have a simplePOJO class which has been decorated using an introduction advisor. Each time I need a reference on my decorated simplePOJO, I want a different instance. Is it possible???????
thanks
The bean has to be declared as prototyope (i.e. scope="prototype" or singleton="false" in the bean definition).
If you use ProxyFactoryBean for proxying, ensure to set the boolean "singleton" property of the factory to "false".
Regards,
Andreas
The ProxyFactoryBean has a "singleton" property. When set to "false" it indicates that the proxy it returns should be a new prototype instance each time.
Can you provide a little bit more information and/or an excerpt from your config with the relevant bean definitions?
Mark Fisher
Spring Integration Lead
SpringSource, a division of VMware
http://www.springsource.com
http://www.springsource.org/spring-integration
http://blog.springsource.com/main/author/markf
Thanks for the quick response. This is my application context
<bean id="simplePOJO"
class="com.cae.launchpad.core.aop.SimplePOJO">
</bean>
<bean id="advisor"
class="com.cae.launchpad.core.aop.LpadQueryIntrodu ctionAdvisorSample" >
</bean>
<bean id="proxy"
class="org.springframework.aop.framework.ProxyFact oryBean" singleton="false">
<property name="target">
<bean class="com.cae.launchpad.core.aop.SimplePOJO" />
</property>
<property name="interceptorNames">
<list>
<value>advisor</value>
</list>
</property>
<property name="proxyTargetClass" value="true"/>
</bean>
And I get :
org.springframework.beans.factory.BeanDefinitionSt oreException: Error registering bean with name 'proxy' defined in class path resource [com/cae/launchpad/core/aop/aopTest-beans.xml]: Validation of bean definition failed; nested exception is org.springframework.beans.factory.support.BeanDefi nitionValidationException: FactoryBean must be defined as singleton - FactoryBeans themselves are not allowed to be prototypes
org.springframework.beans.factory.support.BeanDefi nitionValidationException: FactoryBean must be defined as singleton - FactoryBeans themselves are not allowed to be prototypes
at org.springframework.beans.factory.support.RootBean Definition.validate(RootBeanDefinition.java:166)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.registerBeanDefinition(DefaultL istableBeanFactory.java:282)
at org.springframework.beans.factory.support.BeanDefi nitionReaderUtils.registerBeanDefinition(BeanDefin itionReaderUtils.java:145)
at org.springframework.beans.factory.xml.DefaultXmlBe anDefinitionParser.decorateAndRegisterBeanDefiniti on(DefaultXmlBeanDefinitionParser.java:430)
at org.springframework.beans.factory.xml.DefaultXmlBe anDefinitionParser.parseDefaultElement(DefaultXmlB eanDefinitionParser.java:382)
at org.springframework.beans.factory.xml.DefaultXmlBe anDefinitionParser.parseBeanDefinitions(DefaultXml BeanDefinitionParser.java:355)
at org.springframework.beans.factory.xml.DefaultXmlBe anDefinitionParser.registerBeanDefinitions(Default XmlBeanDefinitionParser.java:201)
at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.registerBeanDefinitions(XmlBeanDefinit ionReader.java:384)
at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio nReader.java:255)
at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:205)
at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:180)
at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:126)
at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:142)
at org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:113)
at org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:81)
at org.springframework.context.support.AbstractRefres hableApplicationContext.refreshBeanFactory(Abstrac tRefreshableApplicationContext.java:89)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:279)
at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:87)
at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:72)
at org.springframework.test.AbstractSpringContextTest s.loadContextLocations(AbstractSpringContextTests. java:121)
at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.loadContextLocations(Abstract DependencyInjectionSpringContextTests.java:210)
at org.springframework.test.AbstractSpringContextTest s.getContext(AbstractSpringContextTests.java:101)
at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.setUp(AbstractDependencyInjec tionSpringContextTests.java:178)
at junit.framework.TestCase.runBare(TestCase.java:125 )
at junit.framework.TestResult$1.protect(TestResult.ja va:106)
at junit.framework.TestResult.runProtected(TestResult .java:124)
at junit.framework.TestResult.run(TestResult.java:109 )
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)
The _bean definition_ of the factory bean must not be declared to be a singleton.
Rather the class ProxyFactoryBean has a property "singleton" which has to be configured to be "false"
Besides that your simplePOJO bean has to be defined as prototype.Code:<property name="singleton" value="false"/>
Regards,
Andreas
appreciate the support
Thanks, it works now!!!
I have noticed that whether or not the simplePOJO is prototype, it is still seen as a singleton. This is my application context.
<beans>
<bean id="simplePOJO"
class="com.cae.launchpad.core.aop.SimplePOJO" singleton="false" >
</bean>
<bean id="advisor"
class="com.cae.launchpad.core.aop.LpadQueryIntrodu ctionAdvisorSample" singleton="false">
</bean>
<bean id="proxy"
class="org.springframework.aop.framework.ProxyFact oryBean" >
<property name="singleton" value="false"/>
<property name="target">
<bean class="com.cae.launchpad.core.aop.SimplePOJO" />
</property>
<property name="interceptorNames">
<list>
<value>advisor</value>
</list>
</property>
<property name="proxyTargetClass" value="true"/>
</bean>
Is this a bug or desired??
Actually you do not refer to the prototype bean, instead creating an inner (singleton) bean.
Change
toCode:<property name="target"> <bean class="com.cae.launchpad.core.aop.SimplePOJO" /> </property>
Regards,Code:<property name="target"> <bean ref="simplePOJO" /> </property>
Andreas