Spring AOP从接口继承了注释

时间:2013-09-03 06:17:05

标签: java spring annotations aop

我有一个注释:

@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的方法上时它正在工作......

谢谢!

2 个答案:

答案 0 :(得分:2)

在Java中,注释不是从接口继承的。你应该创建一个抽象的超类来做到这一点。有关详细信息,请查看Why java classes do not inherit annotations from implemented interfaces?

答案 1 :(得分:0)

@Inhereted注释只能通过分类和接口本身继承,而不能通过其方法继承。也就是说,如果您使用UserService注释对@Privilege进行注释,则UserServiceImpl会继承它。