What is a Java Bean and what is it for?

Asked

Viewed 25,678 times

31

I’m starting to read about development layered, and read something about the bean, an encapsulated class.

But I couldn’t find any content that would really explain the purpose and an example.

So, what is and what is a bean for?

3 answers

22


Good did not get very specific about what exactly you are talking about, if the answer is not exactly what you need, please specify.

What is and what a Bean is for ?

In short:

  • A class containing all private attributes
  • Have getters and setters for your attributes
  • Used to encapsulate and abstract an entity
  • Implements java.io.Serializable

Javabean is an extremely simple class, the example below reflects a "User" on some system. That is, it is just a class that has only attributes and their respective getters and setters... in general has no additional logic.

Not to be confused with Bean, which is a component of the Java EE (Enterprise Java Bean) EJB specification

Example:

    public class Usuario {

    private String nome;
    private String sobrenome;
    private int idade;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }
}

Note that there is a convention in the naming of getters and setters methods. Here you can find more details on the standard nomenclature.

**Edit*****

Okay, that’s a bean, but what it’s for, what’s its function ?

A Javabean represents an entity (or unit or model) of the system (UML here!). It encapsulates the information needed to be transported along the layers or modules. In other cases it also maps these entities with the database, see JPA, or also integrations between applications (web services JAX-RS for example).

Summarizing all necessary data in one place.

To better understand, it is necessary to think more like modeling systems rather than programming itself:

//Ok, eu sei que na instância de 'user' eu tenho todas as informações
//necessárias e encapsuladas ao meu modelo(entidade) "USUÁRIO".
Usuario user = new Usuario();

If it’s not exactly what you’re looking for, just return.

  • Okay, that’s a bean, but what’s it for?

  • I understand what it is, but I want to understand now what its purpose is, what it’s for?

  • Thank you @Math... João Neto, see if you can answer your questions.

  • That’s right. Thank you very much!

  • I’m a little late, but there’s a difference for Managedbean?

  • @Patrick Managedbean I believe you are referring to the Annotations: javax.faces.bean.ManagedBean, javax.annotation.ManagedBean which are a separate issue here, in this case, the closest to the subject:http://answall.com/questions/20706/defini%C3%A7%C3%A3o-de-ejb/20817#20817 In general Managedbean is a term used more in JSF.

  • @Josue I believe that lacked the explicit builder

Show 2 more comments

2

I also find the term bean (grain, in English) somewhat obscure. In Java the pun is justified, since the Beans represent the elements from which the product (coffee, in this case) is made.

Eric Evans proposes a simpler language for the universe of software development. For him, the language ubiquitous (Omnipresent, the one everyone talks about) is the basis for everyone to understand the system. Thus, in order to facilitate understanding, it calls a group of Domain Domains (or Models) and each bean is an Entity. As already mentioned, the Beans are useful to represent the core of the business, that is, the elements about which it is vital to manage information.

1

Simplifying the form of explanation, the Javabean pattern, is nothing more than the classes that model objects, where, obligatorily, the attributes are declared private, there is a public constructor standard, and have to have the access methods (getters and setters) public for each attribute, and only that. As the example of the User Class.java;

public class Usuario {

    private Long idUsuario;
    private String nome;
    private String sobrenome;
    private Integer idade;

    public Usuario(){
    }

    public Long getIdUsuario(){
        return idUsuario;
    }

    public void setIdUsuario(Long idUsuario){
        this.idUsuario = idUsuario;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public Integer getIdade() {
        return idade;
    }

    public void setIdade(Integer idade) {
        this.idade = idade;
    }
}
  • I did this example using Java, but it can be used in other languages like PHP and C#

Browser other questions tagged

You are not signed in. Login or sign up in order to post.