Results 1 to 4 of 4

Thread: simple working example of AOP Introduction

  1. #1
    Join Date
    May 2005
    Posts
    2

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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;

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    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;

  4. #4
    Join Date
    May 2005
    Posts
    2

    Default

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

Similar Threads

  1. Replies: 1
    Last Post: Oct 6th, 2005, 10:32 AM
  2. simple question
    By yukster in forum Security
    Replies: 6
    Last Post: Sep 19th, 2005, 07:58 AM
  3. Replies: 2
    Last Post: Jul 1st, 2005, 12:11 PM
  4. Simple cacheing idea
    By jonmor in forum AOP
    Replies: 3
    Last Post: Feb 5th, 2005, 02:57 AM
  5. Replies: 3
    Last Post: Aug 30th, 2004, 09:56 AM

Posting Permissions

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