Fixed SPR-5920 by implementing GenericInterfaceDrivenDependencyInjectionAspect

This commit is contained in:
Ramnivas Laddad
2009-07-12 05:58:55 +00:00
parent 7199fcc233
commit 29139dfd1a
2 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.aspectj;
/**
* Generic-based dependency injection aspect.
* <p>
* This aspect allows users to implement efficient, type-safe dependency injection without
* the use of the &#64;Configurable annotation.
*
* The subaspect of this aspect doesn't need to include any AOP constructs.
* For example, here is a subaspect that configures the <code>PricingStrategyClient</code> objects.
* <pre>
* aspect PricingStrategyDependencyInjectionAspect
* extends GenericInterfaceDrivenDependencyInjectionAspect<PricingStrategyClient> {
* private PricingStrategy pricingStrategy;
*
* public void configure(PricingStrategyClient bean) {
* bean.setPricingStrategy(pricingStrategy);
* }
*
* public void setPricingStrategy(PricingStrategy pricingStrategy) {
* this.pricingStrategy = pricingStrategy;
* }
* }
* </pre>
* @author Ramnivas Laddad
* @since 3.0.0
*/
public abstract aspect GenericInterfaceDrivenDependencyInjectionAspect<I> extends AbstractInterfaceDrivenDependencyInjectionAspect {
declare parents: I implements ConfigurableObject;
public pointcut inConfigurableBean() : within(I+);
public final void configureBean(Object bean) {
configure((I)bean);
}
// Unfortunately, erasure used with generics won't allow to use the same named method
protected abstract void configure(I bean);
}

View File

@@ -194,6 +194,13 @@ public class BeanConfigurerTests extends TestCase {
assertNotNull("Interface driven injection didn't occur for direct construction", testOrder.mailSender);
}
public void testGenericInterfaceDrivenDependencyInjection() {
PricingStrategy injectedPricingStrategy = new PricingStrategy();
PricingStrategyDependencyInjectionAspect.aspectOf().setPricingStrategy(injectedPricingStrategy);
LineItem testLineItem = new LineItem();
assertSame("Generic interface driven injection didn't occur for direct construction", injectedPricingStrategy, testLineItem.pricingStrategy);
}
public void testInterfaceDrivenDependencyInjectionMultipleInterfaces() {
MailClientDependencyInjectionAspect.aspectOf().setMailSender(new JavaMailSenderImpl());
PaymentProcessorDependencyInjectionAspect.aspectOf().setPaymentProcessor(new PaymentProcessor());
@@ -509,6 +516,18 @@ public class BeanConfigurerTests extends TestCase {
}
}
private static aspect PricingStrategyDependencyInjectionAspect extends GenericInterfaceDrivenDependencyInjectionAspect<PricingStrategyClient> {
private PricingStrategy pricingStrategy;
public void configure(PricingStrategyClient bean) {
bean.setPricingStrategy(pricingStrategy);
}
public void setPricingStrategy(PricingStrategy pricingStrategy) {
this.pricingStrategy = pricingStrategy;
}
}
public static interface MailSenderClient {
public void setMailSender(MailSender mailSender);
}
@@ -521,6 +540,22 @@ public class BeanConfigurerTests extends TestCase {
}
public static interface PricingStrategyClient {
public void setPricingStrategy(PricingStrategy pricingStrategy);
}
public static class PricingStrategy {
}
public static class LineItem implements PricingStrategyClient {
private PricingStrategy pricingStrategy;
public void setPricingStrategy(PricingStrategy pricingStrategy) {
this.pricingStrategy = pricingStrategy;
}
}
public static class Order implements MailSenderClient, Serializable {
private transient MailSender mailSender;