Results 1 to 5 of 5

Thread: Simple AOP example is not working

  1. #1
    Join Date
    Jul 2010
    Posts
    3

    Default Simple AOP example is not working

    Hi everyone,

    I'm very new to Spring and its DI and AOP stuff.

    I'm trying to learn AOP as of now and it seems I can't get my project working. Can someone help me?

    greeting interface:
    Code:
    public interface GreetingService {
        void sayGreeting();
    }
    greeting implementation:
    Code:
    public class GreetingServiceImpl implements GreetingService{
        private String greeting;
    
        public GreetingServiceImpl() {
        }
    
        public GreetingServiceImpl(String greeting) {
            this.greeting = greeting;
        }
        
        public void sayGreeting() {
            System.out.println(this.greeting);
        }
    
        public void setGreeting(String greeting){
            this.greeting = greeting;
        }
    
    }
    main class:
    Code:
    public class HellopApp {
        public static void main(String[] args){
    
            try{            
                BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src/com/springinaction/chapter01/hello/hello.xml"));
    
                GreetingService greetingService = (GreetingService) factory.getBean("greetingService");
                greetingService.sayGreeting();
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
    }
    I'm trying to weave this class as the aspect object but it seems that its methods are not called before and after the "sayGreeting()" method. I expect an output like :
    "Hola que tal?"
    "Muy bien, y tu?"
    "Muy bien, gracias!"

    But what i get is:
    "Muy bien, y tu?"

    Code:
    public class GreetingMan {
        public void pre(){
            System.out.println("Hola que tal?");
            
        }
    
        public void post(){
            System.out.println("Muy bien, gracias!");
            
        }
    }
    What seems to be the problem with my XML configuration file?
    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.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
            
        <bean id="greetingService" class="com.springinaction.chapter01.hello.GreetingServiceImpl">
            <property name="greeting" value="Muy bien, y tu?" />
        </bean>
     
        <bean id="greetingMan" class="com.springinaction.chapter01.hello.GreetingMan"/>
        
        <aop:config>
            <aop:aspect ref="greetingMan">
                <aop:pointcut id="greetPointCut" expression="execution(* *.sayGreeting(..)) and target(bean)"/>
                <aop:before method="pre" pointcut-ref="greetPointCut" arg-names="bean"/>
                <aop:after-returning method="post" pointcut-ref="greetPointCut" arg-names="bean"/>
            </aop:aspect>
        </aop:config>    
    </beans>
    Thanks

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Use the forum search and read the reference guide, especially chapter 3 and 6.

    Short answer, use an ApplicationContext not a BeanFactory.
    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

  3. #3
    Join Date
    May 2010
    Posts
    6

    Default

    Here is the answer. I've attached project for your example. Don't use <aop:config> as spring recommend on the early stages -when you are a new one.
    Attached Files Attached Files

  4. #4
    Join Date
    Jul 2010
    Posts
    3

    Default

    Quote Originally Posted by Marten Deinum View Post
    Use the forum search and read the reference guide, especially chapter 3 and 6.

    Short answer, use an ApplicationContext not a BeanFactory.
    Well I did my own research and study before posting here.
    By the way, I am using Spring In Action ebook to learn Spring framework and the example uses BeanFactory. Thanks for the heads up about ApplicationContext.

  5. #5
    Join Date
    Jul 2010
    Posts
    3

    Default

    Quote Originally Posted by sirtoxy View Post
    Here is the answer. I've attached project for your example. Don't use <aop:config> as spring recommend on the early stages -when you are a new one.
    Well thanks man! This really helps although it has different approach.

Posting Permissions

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