• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can AOP work with BeanFactory Resource Implementation ?

 
Sumeet H Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all !

I have made a simple AOP example.
It consists of a function insertData for which I have configured Before & After advice.
I am calling 2 seperate methods on before & after advice(which only have an SOP statement).

If I run it using ClassPathXmlApplicationContext to instantiate my bean, it works perfectly(all 3 methods execute in the proper order)
But while using BeanFactory to instantiate the bean, it executes only code in insertData method.


Is it necessary to use ApplicationContext(or any of its implementations) only for AOP, or can it work with BeanFactory also?


Calling Code:

//AOP Works
BeanFactory beanFactory = new ClassPathXmlApplicationContext("springAOPExample/beansAOP.xml");
DBInsert dBInsert = (DBInsert) beanFactory.getBean("dBInsertBean");
dBInsert.insertData();

//AOP doesn't work
BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("springAOPExample/beansAOP.xml"));
DBInsert dBInsert = (DBInsert) beanFactory.getBean("dBInsertBean");
dBInsert.insertData();

The XML configuration is shown below:

<bean id="dBInsertBean" class="springAOPExample.DBInsert" />
<bean id="dBConnectionBean" class="springAOPExample.DBConnection" />

<aop:config>
<aop:aspect ref="dBConnectionBean">
<aop:pointcut id="dbPointcut" expression="execution(* *.insertData(..))" />

<aop:before method="openConnection" pointcut-ref="dbPointcut"/>
<aop:after-returning method="commitTransaciton" pointcut-ref="dbPointcut" />

</aop:aspect>
</aop:config>
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
377
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumeet,
As you observed, you need an application context for AOP. A Bean Factory provides less features than an Application Context.
 
Sumeet H Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Thanks Jeanne !
 
nikhilg gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i was facing the same issue.
Thanks it is resolved now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic