@@ RequestScope具有Java继承的注释行为

问题描述 投票:0回答:1

让我们说一堂课

@RequestScope 
public abstract class A {
    int a;
}

和扩展上述类的另一个类

@Service 
public class B extends A {
    public int getA () { return a; }    
}

此类B的变量(它是从A扩展的)是请求范围变量吗?

spring spring-boot inheritance spring-annotations requestscope
1个回答
0
投票

要成为可继承的注释,必须使用@Inherited注释进行标记。看一下@RequestScope的源代码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope(WebApplicationContext.SCOPE_REQUEST)
public @interface RequestScope {

    /**
     * Alias for {@link Scope#proxyMode}.
     * <p>Defaults to {@link ScopedProxyMode#TARGET_CLASS}.
     */
    @AliasFor(annotation = Scope.class)
    ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;

}

没有标记为@Inherited。因此,它不会影响子类。这意味着您示例中的类B的变量不是请求范围的而是单例的,因为默认情况下它应该是单例的。您可以找到有关预定义批注here的更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.