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

  • 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
    Use the forum search and read the reference guide, especially chapter 3 and 6.

    Short answer, use an ApplicationContext not a BeanFactory.

    Comment


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

      Comment


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

        Comment


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

          Comment

          Working...
          X