我有一个注释:
@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的方法上时它正在工作......
谢谢!
答案 0 :(得分:2)
在Java中,注释不是从接口继承的。你应该创建一个抽象的超类来做到这一点。有关详细信息,请查看Why java classes do not inherit annotations from implemented interfaces?
答案 1 :(得分:0)
@Inhereted
注释只能通过分类和接口本身继承,而不能通过其方法继承。也就是说,如果您使用UserService
注释对@Privilege
进行注释,则UserServiceImpl
会继承它。