Announcement Announcement Module
Collapse
No announcement yet.
Problem with annotation config of bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with annotation config of bean

    I'm new with AOP. Reading the book "Spring in Action". There is the chapter with AOP and the example. I have made it with XML and all works good. There is the example of annotated configuration of Aspects. I have made one in one like in the book, but it doesn't work. Please, help.

    **Error:**

    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption:
    Error creating bean with name 'guitar' defined in class path resource [springIdolBeansAnnotation.xml]:
    Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 c
    an't find referenced pointcut performance

    File with beans named springIdolBeansAnnotation.xml:

    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/schem...-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        
        	<bean id="guitar" class="com.springinaction.springidol.Guitar"/>
        	<aop:aspectj-autoproxy/>
        	<bean id="kenny2" class="com.springinaction.springidol.Instrumentalist">
        		<property name="song" value="Jingle B"/>
        		<property name="instrument" ref="guitar"/>
        	</bean>
        	<bean id="audience" class="com.springinaction.springidol.Audience"/>
        </beans>
    **File Guitar:**

    Code:
      package com.springinaction.springidol;
        
        public class Guitar implements Instrument {
        
        	@Override
        	public void play() {
        		System.out.println("Guitar playing");
        	}
        	
        }
    **file Instrumentalist:**

    Code:
    package com.springinaction.springidol;
        
        public class Instrumentalist implements Performer {
        
        	private String song;
        	
        	private Instrument instrument;
        	
        	public Instrumentalist(){}
        	
        	@Override
        	public void perform() throws Exception {
        		System.out.println("Playing "+song + " : ");
        		instrument.play();
        	}
        
        	public String getSong() {
        		return song;
        	}
        
        	public void setSong(String song) {
        		this.song = song;
        	}
        
        	public String screamSong(){
        		return song;
        	}
        
        	public void setInstrument(Instrument instrument) {
        		this.instrument = instrument;
        	}
        	
        }
    file Audience(Aspect):

    Code:
    package com.springinaction.springidol;
        
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.AfterReturning;
        import org.aspectj.lang.annotation.AfterThrowing;
        import org.aspectj.lang.annotation.Around;
        import org.aspectj.lang.annotation.Aspect;
        import org.aspectj.lang.annotation.Before;
        import org.aspectj.lang.annotation.Pointcut;
        
        @Aspect
        public class Audience {
        	@Pointcut("execution(* com.springinaction.springidol.Performer.perform(..))")
        	public void performance(){}
        	
        	@Before("performance()")
        	public void takeSeats(){
        		System.out.println("The audience is takig their seats");
        	}
        	
        	@Before("performance()")
        	public void turnOffCellPhones(){
        		System.out.println("The audience is turning off their cellphones");
        	}
        	
        	@AfterReturning("performance()")
        	public void applaud(){
        		System.out.println("CLAP CLAP CLAP CLAP CLAP");
        	}
        	
        	@AfterThrowing("performance()")
        	public void demandRefund(){
        		System.out.println("Boo! We want our money back");
        	}
        	
        	@Around("performance()")
        	public void watchPerformance(ProceedingJoinPoint joinpoint){
        		try {
        			System.out.println("Theaudienceistakingtheirseats.");
        			System.out.println("Theaudienceisturningofftheircellphones");
        			long start=System.currentTimeMillis();
        			joinpoint.proceed();
        			long end=System.currentTimeMillis();
        			System.out.println("CLAP CLAP CLAP CLAP CLAP");
        			System.out.println("Theperformancetook"+(end-start)
        			+ "milliseconds.");
        		} catch(Throwable t){
        			System.out.println("Boo!Wewantourmoneyback!");
        		}
        	}
        }
    **Main file:**

    Code:
     package com.springinaction.springidol;
        
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        public class SpringIdolMain {
        
        	public static void main(String[] args) {
        		ApplicationContext ctx = new ClassPathXmlApplicationContext("springIdolBeansAnnotation.xml");
        		Performer performer = (Performer) ctx.getBean("kenny2");
        		try {
        			performer.perform();
        		} catch (Exception e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		}
        	}
        }

  • #2
    For starters your aspect is broken... The around advice must return object and you must return the result of the proceed method it should also throw Throwable and you shouldn't catch the exception (this basically breaks your AfterThrowing advice).

    Comment


    • #3
      I have tried it your advice. But there's no effect

      Originally posted by Marten Deinum View Post
      For starters your aspect is broken... The around advice must return object and you must return the result of the proceed method it should also throw Throwable and you shouldn't catch the exception (this basically breaks your AfterThrowing advice).
      Thanks for your advice. I have changed the method's code. But it doesn't have an effect. Exception with errorMessage appears "Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'duke' defined in class path resource [springIdolBeans.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut performance"

      My new code is:
      Code:
      @Around("performance()")
      	public Object watchPerformance(ProceedingJoinPoint joinpoint) throws Throwable{
      			System.out.println("Theaudienceistakingtheirseats.");
      			System.out.println("Theaudienceisturningofftheircellphones");
      			long start=System.currentTimeMillis();
      			Object returnObject = joinpoint.proceed();
      			long end=System.currentTimeMillis();
      			System.out.println("CLAP CLAP CLAP CLAP CLAP");
      			System.out.println("Theperformancetook"+(end-start)+ "milliseconds.");
      			return returnObject;
      	}

      Such example I have found in the book "Spring in Action" Craig Walls. 3-rd edition. page N105.

      Comment


      • #4
        Please use [ code][/code ] tags for the stacktraces as well that way it remains readable.

        Make sure that there are no typos in your pointcut and that the correct pointcut (methodname) is referenced, it is quite easy to make a typo.

        I also suggest the search as there are several answers regarding the sample code for spring in action on the forums.

        Comment


        • #5
          I've replaced the @Pointcut annotation

          Originally posted by Marten Deinum View Post
          Please use [ code][/code ] tags for the stacktraces as well that way it remains readable.

          Make sure that there are no typos in your pointcut and that the correct pointcut (methodname) is referenced, it is quite easy to make a typo.

          I also suggest the search as there are several answers regarding the sample code for spring in action on the forums.
          I've replaced the @Pointcut annotation with
          Code:
          @Around("execution (* *.perform(..))")
          All methods works fine. But with @Pointcut it's still a problem

          Comment

          Working...
          X