DeclareParents class
Makes the proxies of matching beans implement an extra interface, with an implementation the aspect supplies (an AspectJ "introduction").
Use it to add a capability to many beans without changing their
classes, for example a usage counter every service can report. Put it on
a static field of an @Aspect class: the field's type is the
interface to add. The proxy of every bean whose class matches the
value type pattern (Orders, *Service, Repository+) then
implements it and forwards its members to a defaultImpl instance,
created once per proxy on first use. Only beans of the aspect's own
module get the interface, and only while the aspect is registered.
Advice can receive the introduced object with this(name).
@Aspect()
@Component()
class UsageTracking {
// Every *Service bean also implements UsageTracked.
@DeclareParents('*Service+', defaultImpl: DefaultUsageTracked)
static UsageTracked? mixin;
// Each service call bumps that service's own counter.
@Before('execution(* *Service.*(..)) && this(usage)')
void count(UsageTracked usage) => usage.increment();
}
// Elsewhere: read the counter through the introduced interface.
final usage = context.getBean<OrderService>() as UsageTracked;
Spring (Java):
@DeclareParents(value = "com.x.service.*+",
defaultImpl = DefaultUsageTracked.class)
public static UsageTracked mixin;
Constructors
- DeclareParents(String value, {required Type defaultImpl})
-
Adds the field's interface to the proxies of beans matching
value, implemented bydefaultImpl.const
Properties
- defaultImpl → Type
-
The class implementing the interface; it needs a constructor without
arguments. Each proxy gets its own instance.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- value → String
-
The type pattern of the bean classes that get the interface: a name
with
*wildcards, and+for the type and its subtypes.final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited