java用beaninfo,如何在Java 9中使用新的BeanInfo批注

JEP 256: BeanInfo Annotations provides for JavaBean and BeanProperty annotations. While there is not much documentation, I have been hoping this would allow us to use annotations to designate fields on a class as being JavaBean-style properties without having to create boilerplate getter/setter accessor/mutator methods.

So this:

public class Person {

private String name ;

public String getName( ) {

return this.name ;

}

public void setName( String nameArg ) {

this.name = nameArg ;

}

}

…would become this:

import java.beans.BeanProperty;

public class Person {

@BeanProperty

public String name ;

}

Yet when I try this in a Java 9 project in IntelliJ 2017.2.2, I get error in the IDE on the "@" annotation saying:

'@BeanProperty' not applicable to field

Compiler reports error:

Error:(8, 5) java: annotation type not applicable to this kind of declaration

➠ Have I misunderstood the purpose of these new annotations? Or do I have some syntax problem?

I have not found any documentation other than the JEP and JavaDoc linked above.

I am experimenting with the recent release candidates for Java 9, currently Java 9+181 on macOS Sierra 10.12.6.

解决方案

The javadoc says BeanProperty is @Target(METHOD). Looks like it’s a way to customize PropertyDescriptors without having to create a BeanInfo implementation. I don’t think it was intended to work like Lombok. (And thank goodness—see Why use getters and setters? for all the reasons explicit methods are a good idea.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值