@Getter @Setter 注解无法解析为类型
我正在尝试自动化 JavaBean 中的 getter 和 setter 方法。
因为,方法和字段命名约定是针对 Bean @ 命名约定,必须有一种方法来使用此约定,以避免样板代码,例如为每个 bean 编写 getter 和 setter 方法。我在这里找到了一种这样的方法。
我在 Windows 计算机中使用 Eclipse Helios IDE 和 Java SE 6。尽管 Eclipse 确实提供了一种生成 getter 和 setter 方法而无需显式编写它们的方法,但使用注释将使代码更加清晰和可读。
但是,当我在 Eclipse 中编写以下代码时,我在编译时收到以下错误:
private @Getter @Setter int price;
错误:
Getter 无法解析为类型。
Setter 无法解析为类型。
我该如何解决?
I am trying to automatize the getter and setter methods in my JavaBean.
Since, method and field naming conventions are described for Beans @ Naming Convention, there has to be a way to use this conventions in-order to avoid the boiler plate code such as writing getter and setter methods for every bean. I found one such way here.
I am using Eclipse Helios IDE with Java SE 6 in my Windows machine. Though Eclipse do provides a way to generate getter and setter methods without explicitly writing them, using annotations would make the code much more cleaner and readable.
However, when I write the following code in my Eclipse I get the following error @ Compile-time:
private @Getter @Setter int price;
Error :
Getter cannot be resolved to a type.
Setter cannot be resolved to a type.
How do I resolve it??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@Getter
和@Setter
不是 Java 的一部分。 Project Lombok 具有带有这些名称的注释,因此您应该了解如何设置它。@Getter
and@Setter
are not part of Java. Project Lombok has annotations with these names, so you should look into how to set it up.尽管它有效,但您不应该将其作为常规练习。多个人处理相同的代码可能没有与您相同的设置。
我们花了几乎一天的时间试图找出为什么某些东西无法在我们的构建服务器上正确编译,结果发现这是由 eclipes 自动生成的一些注释引起的。
Even though it works, you shouldn't make it a regular practice. Multiple people working on the same code may not have the same setup that you have.
We spent almost a day trying to figure out why something wouldn't compile correctly on our build server and turned out it was caused by some annotations that were auto-generated by eclipes.
如果 Eclipse 仍然无法识别 @Getter 和 @Setter,您必须遵循 lombok.jar 的安装说明。对于日食,你必须
在日食上
执行lombok.jar(双击它,或运行java -jar lombok.jar)。
然后重新启动 Eclipse,Eclipse 才会识别@Getter 和@Setter。仅在构建路径中包含 lombok.jar 可能无法解决 Eclipse 上的问题。
People who may still be not getting @Getter and @Setter recognized by eclipse you have to follow the installation instruction for lombok.jar. For eclipse you will have to
On eclipse
Execute lombok.jar (doubleclick it, or run java -jar lombok.jar).
then restart eclipse only then eclipse will recognize the @Getter and @Setter. Simply having lombok.jar in build path may not solve this on eclipse.
我通过在 pom.xml 中使用此依赖项来解决。
I resolved by using this dependency in pom.xml.