AOP advice applied at the class level of a bean will apply the advice to the factory itself and not to any beans defined with the @Bean annotation.
AOP advice applied on a method annotated with a bean scope will apply the AOP advice to the bean that the factory produces.
Consider the following two examples:
AOP Advice at the type level of a
@Timed
@Factory
class MyFactory {
@Prototype
MyBean myBean() {
return new MyBean()
}
AOP Advice at the type level of a
The above example will log the time it takes to create the MyBean
bean.
Now consider this example:
AOP Advice at the method level of a @Factory
@Factory
public class MyFactory {
@Prototype
@Timed
public MyBean myBean() {
return new MyBean();
}
AOP Advice at the method level of a @Factory
@Factory
open class MyFactory {
@Prototype
@Timed
open fun myBean(): MyBean {
return MyBean()
}
The above example will log the time it takes to execute all of the public methods of the MyBean
bean, but not the bean creation.
The rationale for this behaviour is that you may at times wish to apply advice to a factory and at other times apply advice to the bean produced by the factory.
Note that there is currently no way to apply advice at the method level to a bean and all advice for factories must be applied at the type level. You can control which methods have advice applied by defining methods as non-public (non-public methods do not have advice applied).