java bean 注释,Java bean,注释:他们做什么?他们如何帮助我?

Here is what what I understand so far:

Java beans are just to help other things (visual things?) interact with your code. I think that this is mostly for UI stuff because it is easier to design this visually. Is it bad practice to use Java beans for non-UI things?

Java beans have getter and setter methods (bad OOP practice) and are serializable.

In terms of annotations, I think that user defined annotations do not provide any functionality. Some annotations like @depretiated raise compiler warnings. Can user defined annotations do this somehow? Are user defined annotations good for anything other than documentation? How can I use them? Does eclipse or intellij have some feature that involves annotations?

Have a good weekend.

Jake

Update: that is starting to make more sense. Could someone refer me to an example of when it would be appropriate to use the java bean format and when it would not?

Also I read somewhere that several classes can be a bean and it is a way of packaging classes.

Just to clarify one more thing. I am 95% sure that being a java bean is somewhat like being a singleton (or other pattern). It does not affect what the compiler does.

解决方案

Annotations are a form of declaritive programming. First, you have to grok the benefits of declaritive programming before the utility of annotations becomes clear. In essence, you can add functionality or behavior to a block of code simply by "declaring" that it has a certain characteristic. This is in contrast to actually writing out a series of statements to apply or setup the same behavior.

The JPA annotations are an example of adding functionality with annotations. I don't know of "user created" example off the top of my head, but the JPA annotations are implemented exactly the same way that you or I would do it.

As far as Java Beans, the original use of them was for GUI-programming. The "easy" way of using JavaBeans was to use naming conventions to define the "attributes" of a bean--hence getters and setters. As far as I know, JavaBeans were originally an implementation for GUI-based editing of forms and UI's. So the getters and setters made it easy for the UI software to discover user-viewable or editable attributes. With a Bean Descriptor you can change the way descriptors work a bit...

The reason they persist to this day is they provide a de facto way to inspect objects for publicly exposed properties. It is not bad form to use JavaBeans outside of a GUI. The preference in Java seems to be to use the no arg constructor and then inject your dependencies rather than using RAII style of programming (not that it's strictly available)...

It's actually quite common, particuarly if the object will be manipulated by code that does not know a priori the object it will be manipulating (take a look at Hibernate for a good example).

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值