Announcement Announcement Module
Collapse
No announcement yet.
AOP schema config not working. help plz Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP schema config not working. help plz

    Hi, I am newbie to Spring. I am trying to run the following example but nothing happens.

    Code:
    package test;
    
    public class HelloService {
    
    	public void main() {
    		System.out.println( "Hello World!" );
    	}
    }
    Code:
    package test;
    
    public class HelloAspect {
    
    	  public void sayHello() {
    	    System.out.println("Hello from AspectJ!");
    	  }
    
    }
    Code:
    import example.*;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		ClassPathResource cpr = new ClassPathResource( "test/applicationContext.xml" );
            XmlBeanFactory xbf = new XmlBeanFactory( cpr );
    		
            HelloService test = (HelloService)xbf.getBean( "helloService" );
    
            test.main();
    	}
    
    }
    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"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    	<bean id="helloService" class="test.HelloService"/>
    
       	<aop:config>
       		<aop:aspect ref="helloFromSpringAOP">
       			<aop:pointcut id="mainMethod" expression="execution(* main(..))"/>
       			<aop:after pointcut-ref="mainMethod" method="sayHello"/>
    		</aop:aspect>
    	</aop:config>
    
            <bean id="helloFromSpringAOP" class="test.HelloAspect"/>
    	
    </beans>
    When I run the example, I only see the "Hello World!" message. Why can't I see "Hello from AspectJ!" message?

    Any help appreciated. I am using Spring2.0-RC4. I included all JARs to be sure to not miss something.

    Thanks

  • #2
    Hi

    if you switch to an ApplicationContext instead of the BeanFactory, it will work ;-)

    Code:
    public static void main(String[] args) {
    	ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    		
    	HelloService test = (HelloService) ctx.getBean("helloService");
    
    	test.main();
    }
    Can't tell you why it doesn't work with only a BeanFactory. There are some differences between a BeanFactory and ApplicationContext, most importantly that BeanFactory is less powerfull than ApplicationContext. I tend to always use an ApplicationContext, if not in a restricted environment (memory size), it makes life easier ;-)

    There is something else, because you programm directly against a class without interface (HelloService) you need cglib-nodep-2.1_3.jar in your classpath. Apart from that the following jars are sufficient in your case:
    spring.jar, aspectjweaver.jar, commons-logging.jar

    Comment


    • #3
      Thanks you very much

      Comment

      Working...
      X