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 amp; setters not shown for brevity):
Car.java
Code:
public Class Car #123;
private boolean fourDoor; //getter amp; setter
#125;
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 #123;
public String getColor#40;#41;; public void setColor#40;String color#41;;
#125;
Then I need to implement the interface by extending the DelegatingIntroductionInterceptor:
PaintColorMixin.java
Code:
import org..aop.support.DelegatingIntroductionInterceptor;
public class PaintColorMixin extends DelegatingIntroductionInterceptor #123;
private String color; //getter amp; setter
#125;
Here is my spring config file:
CarApp.xml
Code:
...snip xml stuff...
lt;beansgt; lt;bean id=quot;carTargetquot; classquot;Carquot; singleton=quot;falsequot;gt;lt;/beangt;
lt;bean id=quot;paintColorMixinquot; class=quot aintColorMixinquot; singleton=quot;falsequot;gt;lt;/beangt;
lt;bean id=quot;paintColorAdvisorquot; class=quot;org..aop.support.DefaultIntroductionAdvisorquot; singleton=quot;falsequot;gt; lt;constructor-arggt;lt;ref bean=quot;paintColorMixinquot;gt; lt;/constructor-arggt; lt;/beangt;
lt;bean id=quot;carquot; class=quot;org..aop.framework.ProxyFactoryBeanquot;gt; lt;property name=quot;proxyTargetClassquot;gt; lt;valuegt;truelt;/valuegt; lt;/propertygt; lt;property name=quot;singletonquot;gt; lt;valuegt;falselt;/valuegt; lt;/propertygt; lt;property name=quot;proxyInterfacesquot;gt; lt;valuegt aintColorlt;/valuegt; lt;/propertygt; lt;property name=quot;paintColorAdvisorquot;gt; lt;valuegt;paintColorAdvisorlt;/valuegt; lt;/propertygt; lt;property name=quot;targetquot;gt; lt;value ref=quot;carTargetquot; lt;/propertygt; lt;/beangt;
lt;beansgt;
The paintColorAdvisor property of the car bean seems suspicious. The value suggested in Spring In Action pp. 119 recommends quot;lt;listgt;lt;valuegt;servletActionlt;/valuegt;lt;/listgt;quot;.
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
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;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..aop.support.DelegatingIntroductionInterceptor;
public class PaintColorMixin extends DelegatingIntroductionInterceptor implements PaintColor #123; private String color = quot;Not Setquot;; public String getColor#40;#41; #123;return color;#125; public void setColor#40;String color#41; #123;this.color = color;#125;
#125;
Code:
lt;!DOCTYPE beans PUBLIC quot;-//SPRING//DTD BEAN//ENquot; quot;from#58;// lt;bean id=quot;carTargetquot; class=quot;introductions.Carquot; singleton=quot;falsequot;gt;lt;/beangt;
lt;bean id=quot;paintColorMixinquot; class=quot;introductions.PaintColorMixinquot; singleton=quot;falsequot;gt;lt;/beangt;
lt;bean id=quot;paintColorAdvisorquot; class=quot;org..aop.support.DefaultIntroductionAdvisorquot; singleton=quot;falsequot;gt; lt;constructor-arggt;lt;ref bean=quot;paintColorMixinquot;/gt; lt;/constructor-arggt; lt;/beangt;
lt;bean id=quot;carquot; class=quot;org..aop.framework.ProxyFactoryBeanquot;gt; lt;property name=quot;proxyTargetClassquot;gt; lt;valuegt;truelt;/valuegt; lt;/propertygt; lt;property name=quot;interceptorNamesquot;gt;lt;listgt; lt;valuegt;paintColorAdvisorlt;/valuegt;lt;/listgt; lt;/propertygt; lt;property name=quot;targetquot;gt; lt;ref bean=quot;carTargetquot;/gt; lt;/propertygt; lt;/beangt;
lt;/beansgt;
Code:
package introductions;
import org..context.ApplicationContext;
import org..context.support.ClassPathXmlApplicationContext;
public class IntroductionDriver #123;
public static void main#40;String#91;#93; args#41; #123; ApplicationContext ctx = new ClassPathXmlApplicationContext#40; quot;/introductions/introductions.xmlquot;#41;;
Car car = #40;Car#41; ctx.getBean#40;quot;carquot;#41;; PaintColor carColor = #40 aintColor#41; car;
// test interfaces System.out.println#40;quot;Is Car?#58; quot; + #40;car instanceof Car#41;#41;; System.out.println#40;quot;Is PaintColor?#58; quot; + #40;car instanceof PaintColor#41;#41;;
// test is modified implementation System.out.println#40;quot;Get color#58; quot; + carColor.getColor#40;#41;#41;; carColor.setColor#40;quot;Bluequot;#41;; System.out.println#40;quot;Get color#58; quot; + carColor.getColor#40;#41;#41;; carColor.setColor#40;quot;Redquot;#41;; System.out.println#40;quot;Get color#58; quot; + carColor.getColor#40;#41;#41;; #125;
#125;Thank you, katentim. That's just what I needed. It's working great |