AspectAutoProxyCreator class

The bean post-processor that puts advice into effect: it replaces each bean that some advice matches with the bean's generated proxy.

Aspects and advisor beans only describe advice; something has to wrap the advised beans as they are created. This post-processor collects the advice of every @Aspect and every Advisor bean, orders it, and after each bean is initialized checks it against the pointcuts. A matched bean is replaced by its proxy, which is what getBean and other beans then receive; an unmatched bean is left as it is and costs nothing.

Applications never create it: the application context registers it as soon as an @Aspect or an Advisor bean exists, here or in an ancestor context (no @EnableAspectJAutoProxy needed). Aspects and advisors of ancestor contexts apply too. Aspects, advisors and post-processors are never proxied themselves, nor is a bean the generator wrote no proxy class for.

The proxy implements the bean's class when that class can be implemented, else its interfaces; a class that can only be extended is created as a generated subclass instead. A bean proxied through its interfaces only must be injected by one of them.

// Defining the aspect is enough: the context wraps the Calculator.
@Aspect()
@Component()
@Log()
class AuditAspect {
  @Before('execution(* Calculator.*(..))')
  void before(JoinPoint joinPoint) => log.info(joinPoint.toShortString());
}

final context = ApplicationContext.run([appBeans]);
final calculator = context.getBean<Calculator>(); // a proxy
calculator.add(1, 2); // prints execution(Calculator.add(..))

Spring (Java):

@Configuration
@EnableAspectJAutoProxy // registers AnnotationAwareAspectJAutoProxyCreator
@ComponentScan
class AppConfig {}

var context = new AnnotationConfigApplicationContext(AppConfig.class);
Calculator calculator = context.getBean(Calculator.class);
calculator.add(1, 2);
Inheritance
Implemented types

Constructors

AspectAutoProxyCreator(ConfigurableListableBeanFactory beanFactory)
A post-processor reading aspects and advisors from beanFactory and its ancestors.

Properties

advisors → List<PointcutAdvisor>
Every advisor that may apply, outermost first: one per aspect advice method, plus the advisor beans.
no setter
beanFactory → ConfigurableListableBeanFactory
The factory whose bean definitions provide the aspects and advisors, and whose beans are proxied.
final
hashCode → int
The hash code for this object.
no setterinherited
order → int
Ordered.highestPrecedence: it runs first among the ordered post-processors, like Spring's auto proxy creator, so later post-processors see the proxy.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
postProcessAfterInitialization(Object bean, String beanName) → Object
Returns the proxy of bean when an advisor matches one of its members, or a @DeclareParents introduction of a registered aspect applies to it; else bean itself.
override
postProcessBeforeInitialization(Object bean, String beanName) → Object
Called for each new bean after injection and the aware callbacks, before the init callbacks (@PostConstruct, afterPropertiesSet, init method). Returns the object to go on with, usually bean itself.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

isRequiredFor(ConfigurableListableBeanFactory factory) → bool
Whether factory or one of its ancestors declares an @Aspect or an Advisor bean (also one registered as a plain singleton), so the context must register an auto proxy creator.