Announcement Announcement Module
Collapse
No announcement yet.
Simple AOP Not Working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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();
        }        
      }
    }

    Comment


    • #3
      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.

      Comment


      • #4
        Thanks

        Thanks for helping a noob out. I appreciate it.

        C

        Comment

        Working...
        X