wood burning stoves 2.0*
The moose likes Spring and the fly likes Can AOP work with BeanFactory Resource Implementation ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Can AOP work with BeanFactory Resource Implementation ?" Watch "Can AOP work with BeanFactory Resource Implementation ?" New topic
Author

Can AOP work with BeanFactory Resource Implementation ?

Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 29
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Sumeet,
As you observed, you need an application context for AOP. A Bean Factory provides less features than an Application Context.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 29
Ok, Thanks Jeanne !
nikhilg gupta
Greenhorn

Joined: Feb 22, 2011
Posts: 4
Hi i was facing the same issue.
Thanks it is resolved now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can AOP work with BeanFactory Resource Implementation ?
 
Similar Threads
Spring AOP program not running as expected
Spring AOP issue
Hello world spring aop
Transaction demarcation works with TransactionProxy but not AOP
Spring Aop