• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Spring 2.5 and AOP - BeanPostProcessor before instantiation of bean failed

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unable to locate method [takeSeats] on bean [audience]

you only have takeSeat (without the 'S')



got it?

Herman
 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The only cure for that is hours of television radiation. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic