tags:

views:

340

answers:

6

What is a bean in spring framework? What is the use of the bean?

A: 

a bean is a POJO with setters and getters that follow the bean convention.

beans are used to pass around data, also jsps has special support for beans

mkoryak
POJO = plain old java object
knittl
You're confusing Java Beans with Spring Beans.
Thomas Lötzer
is spring bean not a java bean?
mkoryak
A spring bean may be a java bean, but not necessarily
Don
A: 

a bean is a class with a public standard constructor, getters/setters (properties) and it must be serializable. wikipedia has an explenation on this.

this way the spring framework knows how to create an instance of the bean (public standard constructor) and which properties exist (with reflection)

knittl
There is no requirement that a Spring bean class must be a JavaBean
Don
The Spring framework can instantiate a bean with a factory-method. So it's not a requirement that your class has a public constructor. And actually, Spring also handles constructors with arguments.
Espen
+3  A: 

The Java Bean spec does spell out no-arg constructor, getters/setters, and serializable, but Spring does not require that your beans follow the spec. Spring deals with Plain Old Java Objects, whether they conform to the Java Bean spec or not.

What's the use of beans? They express your wishes in code. All Spring is doing is managing their lifecycle and wiring them together to accomplish your goals.

duffymo
+14  A: 

In the context of Spring, a bean is a managed object. What is a managed object? It's an object that Spring is aware of and that Spring knows how to manipulate, e.g. inject properties, invoke callback methods, etc.

There is then a difference between a regular java class (which Spring doesn't know about) and beans (which Spring knows about).

Generally Spring beans follow the Java bean convention, so that Spring can manipulate them easily. For instance if the bean is declared to have a property xxx, then Spring will expect getXxx and setXxx to be present. However, since Spring 2.X it is possible to dependency-inject private variables (using annotations), and therefore it is is no longer necessary to define a public setter in order to enable dependency injection for a property.

( The term bean is confusing in the sense that it is frequently uses to denote either (1) something that is managed by a container, like an enterprise java bean (EJB) or (2) something that adheres to the Java bean conventions. )

ewernli
A: 

Spring finds some of the Java Bean conventions useful for injecting dependencies into objects using setter injection. As long as the Spring-managed object has setters Spring can inject into it. (Of course Spring can use constructor-based injection too.) Also Spring uses the zero-argument constructor when creating managed objects.

Most of the beans in an application using the Spring framework are either resources like data sources or transaction managers, or process objects (like services, data access objects, or controllers) that make use of resources or other process objects.

Nathan Hughes
A: 

A bean in Spring world is any object that is defined using Spring conventions using bean id or name. Its life time is managed by Spring container. The bean can be a POJO or a factory instance with static methods. It can even be a JNDI resource and not necessary a Java Bean. Bean managed by the Spring container can even be a proxy bean which may hide the bean (esp with TransactionProxyFactory).

In short, any bean whose lifetime is managed by Spring container is a Spring bean.

Kartik