micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6k stars 1.04k forks source link

AOP on beans in custom scope broken #9210

Open mancze opened 1 year ago

mancze commented 1 year ago

Expected Behavior

AOP annotations (e.g. caching annotations) on beans in custom scope should work normally.

Possibly related: #9208

Actual Behaviour

Steps To Reproduce

Environment Information

No response

Example Application

https://github.com/mancze/micronaut-sandbox/tree/aop-in-custom-scope

Version

3.9.1

graemerocher commented 1 year ago

Probably this needs extra documentation, but this is not currently a bug. You need to add @ScopedProxy to @MyScope, making this change passes the tests:

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.PARAMETER, ElementType.TYPE, ElementType.METHOD})
    @ScopedProxy
    @interface MyScope {
    }
mancze commented 1 year ago

Does that mean that custom scopes have to be annotated with @ScopedProxy for AOP to work on them? That surely would use some docs :).

mancze commented 1 year ago

I have a follow up question to this. We were able to overcome the need of applying @ScopedProxy by using eagerInitAnnotated(MyScope.class) instead.

But after debugging the code, I'm not sure if we aren't taking advantage of possibly another issue in Micronaut. It appears, that usage of eagerInitAnnotated(<your annotation>) automatically puts those beans always into Singleton scope. It disregards the fact that they might be declared in some other scope. Is that something to be reported in a separate issue, or is that another case of insufficient documentation?