my dog learned polymorphism*
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Can AOP work with BeanFactory Resource Implementation ?" Watch "Can AOP work with BeanFactory Resource Implementation ?" New topic

Can AOP work with BeanFactory Resource Implementation ?

Sumeet H Singh

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");

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

The XML configuration is shown below:

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

<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" />

Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30050

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

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

Joined: Feb 22, 2011
Posts: 4
Hi i was facing the same issue.
Thanks it is resolved now.
jQuery in Action, 2nd edition
subject: Can AOP work with BeanFactory Resource Implementation ?
Similar Threads
Hello world spring aop
Transaction demarcation works with TransactionProxy but not AOP
Spring AOP issue
Spring Aop
Spring AOP program not running as expected