【问题标题】:Spring AOP inherited annotation from an interfaceSpring AOP 从接口继承注解
【发布时间】:2014-02-09 07:53:29
【问题描述】:

我有一个注释:

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

    String[] value();   
}

还有一个界面:

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 spring annotations aop


    【解决方案1】:

    在 Java 中,注解不是从接口继承而来的。您应该创建一个抽象超类来执行此操作。欲了解更多信息,请查看Why java classes do not inherit annotations from implemented interfaces?

    【讨论】:

      【解决方案2】:

      @Inhereted 注解只能被类和接口本身继承,不能被它们的方法继承。也就是说,如果您使用 @Privilege 注释来注释 UserService,那么 UserServiceImpl 将继承它。

      【讨论】:

        猜你喜欢
        • 2020-08-23
        • 1970-01-01
        • 2012-09-07
        • 2011-06-15
        • 2010-10-21
        • 1970-01-01
        • 1970-01-01
        • 2011-04-15
        • 2013-08-31
        相关资源
        最近更新 更多