File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes Spring 2.5 and AOP - BeanPostProcessor before instantiation of bean failed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring 2.5 and AOP - BeanPostProcessor before instantiation of bean failed" Watch "Spring 2.5 and AOP - BeanPostProcessor before instantiation of bean failed" New topic
Author

Spring 2.5 and AOP - BeanPostProcessor before instantiation of bean failed

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
I am trying to use Spring 2.5 for AOP. I get exception as follows, when I run the AudienceTest.java class as a Java program.

Error creating bean with name 'audience' defined in class path resource [aop_audience.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.config.MethodLocatingFactoryBean] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Unable to locate method [takeSeats] on bean [audience]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:405)
at java.security.AccessController.doPrivileged(AccessController.java:219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at aop.AudienceTest.main(AudienceTest.java:8)


My AudienceTest.java looks like:

package aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AudienceTest {
public static void main(String args[]){
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop_audience.xml");
Performing performance = new Performing();
}
}


My AudienceTest.java looks like:

package aop;

public class Audience {

public void takeSeat(){
System.out.println("take seat");
}

public void applaud(){
System.out.println("applaud");
}

public void demandRefund(){
System.out.println("demanding refund");
}
}

My Performing.java class looks like:


package aop;

public class Performing {
public void perform(){
System.out.println("Inside performing");
}
}

My aop_audience.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="audience" class="aop.Audience" />
<aop:config>
<aop:aspect ref="audience">
<aop ointcut
id="performance"
expression="execution(* aop.Performing.perform())" />
<aop:before
method="takeSeats"
pointcut-ref="performance"/>
<aop:after-returning
method="applaud"
pointcut-ref="performance"/>
<aop:after-throwing
method="demandRefund"
pointcut-ref="performance" />
</aop:aspect>
</aop:config>
</beans>
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
Unable to locate method [takeSeats] on bean [audience]

you only have takeSeat (without the 'S')



got it?

Herman
Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
Herman, I fixed the typo to takeSeat for the method name.

Now, the program executes successfully. But I am not getting aop:before and aop:after-returning executed. I do NOt get an exception and the driver class (AudienceTest.java) executes without throwing any exception. I get printlines from aop:before and aop:after-returning.

Following is AudienceTest.java:

package org.poc.aop;
/**
* From Spring in Action book.
*/

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AudienceTest {
public static void main(String args[]){
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop_audience.xml");
Performing performance = new Performing();
performance.perform();
}
}

package org.poc.aop;

public class Performing {
public void perform(){
System.out.println("Inside performing");
}
}

I get following output in console as: Inside performing

package org.poc.aop;

public class Audience {

public void takeSeat(){
System.out.println("take seat");
}

public void applaud(){
System.out.println("applaud");
}

public void demandRefund(){
System.out.println("demanding refund");
}
}

I do not get printline for "take seat" and "applaud" for aop:before and aop:after-returning.

Following is aop_audience.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="audience" class="org.poc.aop.Audience" />
<aop:config>
<aop:aspect ref="audience">
<aop ointcut
id="performance"
expression="execution(* org.poc.aop.Performing.perform())" />
<aop:before
method="takeSeat"
pointcut-ref="performance"/>
<aop:after-returning
method="applaud"
pointcut-ref="performance"/>
<aop:after-throwing
method="demandRefund"
pointcut-ref="performance" />
</aop:aspect>
</aop:config>
</beans>

The takeSeat() method of Audience.java should get executed before perform() method of Performing.java is executed and should print "Take seat". I do not see that.

In the same way, applaud() method of Audience.java should get executed after perform() method of Performing.java is executed and should print "applaud". I do not see that.

I only see "Inside performing" getting printed out in console as a result of executing perform() method of Performing.java.
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Sam,

I see you make your own code, based on Spring In Action code?

Did you get that code working?
I have not used AspectJ with Spring.

Herman
Suchita Jain
Greenhorn

Joined: Oct 16, 2008
Posts: 1
You should change the aop_audience.xml file with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="audience" class="aop.Audience" />
<bean id="per" class="aop.Performing" />
<aop:config>
<aop:aspect ref="audience">
<aop ointcut id="performance" expression="execution(* aop.Performing.perform())" />
<aop:before method="takeSeat" pointcut-ref="performance"/>
<aop:after-returning method="applaud" pointcut-ref="performance"/>
<aop:after-throwing method="demandRefund" pointcut-ref="performance" />
</aop:aspect>
</aop:config>
</beans>


You will see the following output
take seat
Inside performing
applaud
 
 
subject: Spring 2.5 and AOP - BeanPostProcessor before instantiation of bean failed
 
Similar Threads
AOP not getting invoked on my Target Bean Method --> Singer.perform() method
Spring AOP HELP needed
unable to call AOP method
Autoproxying @AspectJ aspects
Spring AOP issue