的Java bean,注释:他们做什么工作?他们如何帮助我? [英] Java beans, annotations: What do they do? How do they help me?

查看:227
本文介绍了的Java bean,注释:他们做什么工作?他们如何帮助我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我到目前为止的理解:

Here is what what I understand so far:

Java bean的只是帮助其他东西(视觉的东西?)你的code相互作用。我认为,这主要是为UI的东西,因为它更容易直观地设计这一点。它是不好的做法使用Java豆非UI的东西呢?

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 bean中有getter和setter方法​​(OOP不好的做法)和序列化。

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

在注释方面,我认为,用户定义的注释不提供任何功能。一些注释像@德pretiated提高编译器警告。用户定义的注释可以做到这一点不知何故?是用户定义的注释很好地用于文档的任何其他?我该如何使用它们?不偏食或的IntelliJ有一些功能,包括注释吗?

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?

有一个美好的周末。

杰克

更新:是开始更有意义。可能有人指我到时候会适当使用java bean的格式为例,当它不会?

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?

此外,我读的地方,几类可以是一个bean,它是包装类的方法。

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

只是为了澄清一件事。我95%肯定是一个java bean是有点象是一个单身(或其他方式)。它不影响编译器做什么。

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.

推荐答案

注解 declaritive编程的形式。首先,你必须神交declaritive编程的好处之前,注释的效用变得清晰。从本质上讲,你可以简单地宣布,它具有一定的特性code块添加功能或行为。这是相对于实际编写了一系列语句来申请或设置相同的行为。

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.

的<一个href=\"http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html\"相对=nofollow> JPA注解与注解增加功能的一个例子。我不知道比如把我的头顶部的创建用户,但JPA注释正是实现你或我会做同样的方式。

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.

据的Java bean,原来使用他们是GUI编程。使用JavaBeans的易的方式是使用命名约定来定义bean的属性 - 因此getter和setter。据我所知,JavaBeans最初是为形式和用户界面的基于GUI的编辑的实现。所以getter和setter方法​​使它容易对用户界面软件发现用户可查看或编辑属性。一个bean描述符可以改变描述符的工作一点的方式...

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...

他们坚持到今天的原因是他们提供了一个事实上的方式来检查是否有公开暴露的属性的对象。这是不坏的形式使用GUI之外的JavaBeans。 Java中的preference似乎是使用无参数的构造函数,然后注入你的依赖,而不是使用的编程(不是它的严格可用)的RAII风格...

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)...

这其实相当普遍,particuarly如果对象将以code,不知道先验它将被操纵的对象(看看的休眠一个很好的例子)。

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).

这篇关于的Java bean,注释:他们做什么工作?他们如何帮助我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆