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

  • simple working example of AOP Introduction

    Hello, world!

    I'm trying to understand how to use AOP Introductions. A simple yet complete and working example would help me tremendously.

    Here is my toy application (getter & setters not shown for brevity):

    Car.java
    Code:
    public Class Car {
    
        private boolean fourDoor;
        //getter & setter
    
    }
    I would like to introduce an attribute to hold a string for the paint color in my Car objects. My understanding so far is that I need to create an interface for the attribute I want to add:

    PaintColor.java
    Code:
    public interface PaintColor {
    
        public String getColor();
        public void setColor(String color);
    
    }
    Then I need to implement the interface by extending the DelegatingIntroductionInterceptor:

    PaintColorMixin.java
    Code:
    import org.springframework.aop.support.DelegatingIntroductionInterceptor;
    
    public class PaintColorMixin extends DelegatingIntroductionInterceptor {
    
        private String color;
        //getter & setter
    
    }
    Here is my spring config file:

    CarApp.xml
    Code:
    ...snip xml stuff...
    <beans>
        <bean id="carTarget"  class"Car" singleton="false"></bean>
    
        <bean id="paintColorMixin" class="PaintColorMixin" singleton="false"></bean>
    
        <bean id="paintColorAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor" singleton="false">
            <constructor-arg>
                <ref bean="paintColorMixin">
            </constructor-arg>
        </bean>
    
        <bean id="car"  class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyTargetClass"> <value>true</value> </property>
            <property name="singleton"> <value>false</value> </property>
            <property name="proxyInterfaces"> <value>PaintColor</value> </property>
            <property name="paintColorAdvisor"> <value>paintColorAdvisor</value> </property>
            <property name="target"> <value ref="carTarget" </property>
        </bean>
    <beans>
    The paintColorAdvisor property of the car bean seems suspicious. The value suggested in Spring In Action pp. 119 recommends "<list><value>servletAction</value></list>".

    I'm not confident in my configuration, and I'm at a loss for how I'm supposed to use the enhanced car object in client code.

    Any nudge (or mule kick) in the right direction is greatly appreciated.

    Best regards,
    Tony

  • #2
    At a glance I can see...
    Code:
    public class PaintColorMixin extends DelegatingIntroductionInterceptor &#123;
    should implement PainColor
    Code:
    public class PaintColorMixin extends DelegatingIntroductionInterceptor implements PaintColor  &#123;

    Comment


    • #3
      A simple yet complete and working example would help me tremendously
      Try this:
      Code:
      package introductions;
      
      public class Car &#123;&#125;
      Code:
      package introductions;
      
      public interface PaintColor &#123;
          public String getColor&#40;&#41;;
          public void setColor&#40;String color&#41;;
      &#125;
      Code:
      package introductions;
      
      import org.springframework.aop.support.DelegatingIntroductionInterceptor;
      
      public class PaintColorMixin extends DelegatingIntroductionInterceptor
          implements PaintColor &#123;
          private String color = "Not Set";
          public String getColor&#40;&#41; &#123;return color;&#125;
          public void setColor&#40;String color&#41; &#123;this.color = color;&#125;
      &#125;
      Code:
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
      <beans>
      
          <bean id="carTarget" class="introductions.Car" singleton="false"></bean>
      
          <bean id="paintColorMixin" class="introductions.PaintColorMixin" singleton="false"></bean>
      
          <bean id="paintColorAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor" singleton="false">
              <constructor-arg>
                  <ref bean="paintColorMixin"/>
              </constructor-arg>
          </bean>
      
          <bean id="car" class="org.springframework.aop.framework.ProxyFactoryBean">
              <property name="proxyTargetClass"> <value>true</value> </property>
              <property name="interceptorNames">
                  <list>
                      <value>paintColorAdvisor</value>
                  </list>
              </property>
              <property name="target"> <ref bean="carTarget"/> </property>
          </bean>
      </beans>
      Code:
      package introductions;
      
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class IntroductionDriver &#123;
      
          public static void main&#40;String&#91;&#93; args&#41; &#123;
              ApplicationContext ctx = new ClassPathXmlApplicationContext&#40;
                      "/introductions/introductions.xml"&#41;;
      
              Car car = &#40;Car&#41; ctx.getBean&#40;"car"&#41;;
              PaintColor carColor = &#40;PaintColor&#41; car;
      
              // test interfaces
              System.out.println&#40;"Is Car?&#58; " + &#40;car instanceof Car&#41;&#41;;
              System.out.println&#40;"Is PaintColor?&#58; " + &#40;car instanceof PaintColor&#41;&#41;;
      
              // test is modified implementation
              System.out.println&#40;"Get color&#58; " + carColor.getColor&#40;&#41;&#41;;
              carColor.setColor&#40;"Blue"&#41;;
              System.out.println&#40;"Get color&#58; " + carColor.getColor&#40;&#41;&#41;;
              carColor.setColor&#40;"Red"&#41;;
              System.out.println&#40;"Get color&#58; " + carColor.getColor&#40;&#41;&#41;;
          &#125;
      &#125;

      Comment


      • #4
        Thank you, katentim. That's just what I needed. It's working great

        Comment

        Working...
        X