gpt4 book ai didi

java - Spring AOP 从接口(interface)继承注解

转载 作者:行者123 更新时间:2023-11-30 09:18:13 24 4
gpt4 key购买 nike

我有一个注释:

@Target( { ElementType.METHOD } )
@Retention( RetentionPolicy.RUNTIME )
@Inherited
public @interface Privilege {

String[] value();
}

还有一个接口(interface):

public interface UserService {
@Privilege("USER_READ")
UserDTO getUserProperties(long userId);
}

及其实现:

public class UserServiceImpl implements UserService {
public UserDTO getUserProperties(long userId) { ... }
}

和 Spring AOP 设置:

    <aop:aspectj-autoproxy />
<aop:config>
<aop:aspect id="securityAspect" ref="hlSecurityCheck">
<aop:pointcut id="securityPointcut"
expression="@annotation(services.annotation.Privilege)" />
<aop:around pointcut-ref="securityPointcut" method="checkService" />
...

为什么这不起作用? (当我将注解直接放在 UserServiceImpl 的方法上时它起作用了...

谢谢!

最佳答案

在 Java 中,注释不是从接口(interface)继承的。您应该创建一个抽象父类(super class)才能执行此操作。有关更多信息,请查看 Why java classes do not inherit annotations from implemented interfaces?

关于java - Spring AOP 从接口(interface)继承注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18585374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com