Skip to content

Commit 29139df

Browse files
committed
Fixed SPR-5920 by implementing GenericInterfaceDrivenDependencyInjectionAspect
1 parent 7199fcc commit 29139df

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2008 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.beans.factory.aspectj;
17+
18+
/**
19+
* Generic-based dependency injection aspect.
20+
* <p>
21+
* This aspect allows users to implement efficient, type-safe dependency injection without
22+
* the use of the &#64;Configurable annotation.
23+
*
24+
* The subaspect of this aspect doesn't need to include any AOP constructs.
25+
* For example, here is a subaspect that configures the <code>PricingStrategyClient</code> objects.
26+
* <pre>
27+
* aspect PricingStrategyDependencyInjectionAspect
28+
* extends GenericInterfaceDrivenDependencyInjectionAspect<PricingStrategyClient> {
29+
* private PricingStrategy pricingStrategy;
30+
*
31+
* public void configure(PricingStrategyClient bean) {
32+
* bean.setPricingStrategy(pricingStrategy);
33+
* }
34+
*
35+
* public void setPricingStrategy(PricingStrategy pricingStrategy) {
36+
* this.pricingStrategy = pricingStrategy;
37+
* }
38+
* }
39+
* </pre>
40+
* @author Ramnivas Laddad
41+
* @since 3.0.0
42+
*/
43+
public abstract aspect GenericInterfaceDrivenDependencyInjectionAspect<I> extends AbstractInterfaceDrivenDependencyInjectionAspect {
44+
declare parents: I implements ConfigurableObject;
45+
46+
public pointcut inConfigurableBean() : within(I+);
47+
48+
public final void configureBean(Object bean) {
49+
configure((I)bean);
50+
}
51+
52+
// Unfortunately, erasure used with generics won't allow to use the same named method
53+
protected abstract void configure(I bean);
54+
}

‎org.springframework.aspects/src/test/java/org/springframework/beans/factory/aspectj/BeanConfigurerTests.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ public void testInterfaceDrivenDependencyInjection() {
194194
assertNotNull("Interface driven injection didn't occur for direct construction", testOrder.mailSender);
195195
}
196196

197+
public void testGenericInterfaceDrivenDependencyInjection() {
198+
PricingStrategy injectedPricingStrategy = new PricingStrategy();
199+
PricingStrategyDependencyInjectionAspect.aspectOf().setPricingStrategy(injectedPricingStrategy);
200+
LineItem testLineItem = new LineItem();
201+
assertSame("Generic interface driven injection didn't occur for direct construction", injectedPricingStrategy, testLineItem.pricingStrategy);
202+
}
203+
197204
public void testInterfaceDrivenDependencyInjectionMultipleInterfaces() {
198205
MailClientDependencyInjectionAspect.aspectOf().setMailSender(new JavaMailSenderImpl());
199206
PaymentProcessorDependencyInjectionAspect.aspectOf().setPaymentProcessor(new PaymentProcessor());
@@ -509,6 +516,18 @@ public void setPaymentProcessor(PaymentProcessor paymentProcessor) {
509516
}
510517
}
511518

519+
private static aspect PricingStrategyDependencyInjectionAspect extends GenericInterfaceDrivenDependencyInjectionAspect<PricingStrategyClient> {
520+
private PricingStrategy pricingStrategy;
521+
522+
public void configure(PricingStrategyClient bean) {
523+
bean.setPricingStrategy(pricingStrategy);
524+
}
525+
526+
public void setPricingStrategy(PricingStrategy pricingStrategy) {
527+
this.pricingStrategy = pricingStrategy;
528+
}
529+
}
530+
512531
public static interface MailSenderClient {
513532
public void setMailSender(MailSender mailSender);
514533
}
@@ -521,6 +540,22 @@ public static class PaymentProcessor {
521540

522541
}
523542

543+
public static interface PricingStrategyClient {
544+
public void setPricingStrategy(PricingStrategy pricingStrategy);
545+
}
546+
547+
public static class PricingStrategy {
548+
549+
}
550+
551+
public static class LineItem implements PricingStrategyClient {
552+
private PricingStrategy pricingStrategy;
553+
554+
public void setPricingStrategy(PricingStrategy pricingStrategy) {
555+
this.pricingStrategy = pricingStrategy;
556+
}
557+
}
558+
524559
public static class Order implements MailSenderClient, Serializable {
525560
private transient MailSender mailSender;
526561

0 commit comments

Comments
 (0)