Results 1 to 4 of 4

Thread: Simple AOP Not Working

  1. #1
    Join Date
    Aug 2007
    Posts
    16

    Default Simple AOP Not Working

    Good Morning Everyone,

    I am trying to get a simple aop app to work but it seems as if spring is ignoring the aop part of my spring-config file. Not sure what I'm doing wrong.

    Here is what I have:
    1) A simple interface and implementing class
    2) A pojo aspect class
    3) A class to test it
    4) My spring config.xml


    Code:
     
    //==========================================
     
    package com.macdon.aop.logging.test;
    public interface IService {
    	Object doSomething(String obj,int i);	
    	void doException() throws Throwable;
    }
     
    //==========================================
     
    package com.macdon.aop.logging.test;
    public class ServiceImpl implements IService{
    	public Object doSomething(String obj, int i) {
    		System.err.println(obj + i);
    		return obj;
    	}
    	public void doException() throws Throwable {
    		throw new Exception("Thrown exception");
    	}
    }
     
    //==========================================
     
    package com.macdon.aop.logging;
    public class TestAspect {
      public TestAspect() {}  
      public void foo() {
        System.out.println("===== This got injected =====");
      }
    }
     
    //==========================================
     
    <?xml version='1.0' encoding='windows-1252'?>
     
    <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="fooService" class="com.macdon.aop.logging.test.ServiceImpl" />  
      <bean id="testerid" class="com.macdon.aop.logging.TestAspect" />
      
      <aop:config>      
        <aop:aspect ref="testerid">        
        <aop  ointcut id="all" expression="execution(* *.*(..))" />
          <aop:before method="foo" pointcut="execution(* *.*(..))" />
          <aop:before           method="foo" pointcut-ref="all"/>
          <aop:after-returning  method="foo" pointcut-ref="all" />
          <aop:after-throwing   method="foo" pointcut-ref="all" />      
        </aop:aspect>
      </aop:config>
    </beans>
    I get output:

    Testing 123
    java.lang.Exception: Thrown exception at com.macdon.aop.logging.test.ServiceImpl.doExceptio n(ServiceImpl.java:17)
    at com.macdon.aop.logging.test.TestLogger.main(TestLo gger.java:19)

    But no aop behavior.

    Can someone please help?

    Thanks so much.

    Yeuker

  2. #2
    Join Date
    Aug 2007
    Posts
    16

    Default Forgot to post my test class

    Code:
    import com.macdon.aop.logging.test.IService;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    
    public class TestLogger  {
      public TestLogger() {  }  
      public static void main(String[] args) {    
        Resource resource = new ClassPathResource("spring-config.xml");
        BeanFactory  beanFactory = new XmlBeanFactory(resource);    
        IService foo = (IService) beanFactory.getBean("fooService");    
        foo.doSomething("Testing ", 123);
        try {
          foo.doException();  
        } catch (Throwable t) {
          t.printStackTrace();
        }        
      }
    }

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    As answered numerous times use an ApplicationContext implementation. In your case ClassPathXmlApplicationContext. Chapter 3.8 of the reference guide explains the differences between a BeanFactory and an ApplicationContext.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Aug 2007
    Posts
    16

    Default Thanks

    Thanks for helping a noob out. I appreciate it.

    C

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •