File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Can AOP work with BeanFactory Resource Implementation ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Spring
Reply 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: 18
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: 23635

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]
Certs: SCEA Part 1, Part 2 & 3 & Core Spring 3, OCAJP
Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 18
Ok, Thanks Jeanne !
 
 
subject: Can AOP work with BeanFactory Resource Implementation ?
 
Threads others viewed
Transaction demarcation works with TransactionProxy but not AOP
Spring AOP issue
Spring AOP program not running as expected
Hello world spring aop
Spring Aop
developer file tools