通过注解拦截对类属性的访问?
是否有任何Java注释能够拦截对public类属性的访问(读或写)?
这将能够像使用 getter 和 setter 那样注入逻辑,而无需在不需要时创建它们。
Is there any Java annotation enabling to intercept the access (read or write) to a public class attribute ?
This would enable to inject logic as you would with getters and setters, without the need of making them when it's not necessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有内置注释可以执行此操作,但您可以添加自己的注释。但是,您可能想要使用面向方面的编程。例如 AspectJ
There is no built in annotations to do this, but you could add your own. However, Using aspect orientated programming may be what you have in mind. e.g. AspectJ
不。Java 不提供任何方法来拦截属性的访问。通过面向方面的编程,您可以使用方法实现类似的目标,但对于属性来说这是不可能的。
No. Java does not provide any means to intercept the access of an attribute. With aspect oriented programming you can achieve something like this with methods but for attributes this is impossible.