Spring Framework Series — Bean

devcat
3 min readOct 16, 2023

--

Welcome to the spring framework series, In this article we will understand what is Bean, Scope of Bean and It’s Lifecycle. In the previous part of this blog post series, We understood what is IOC container and Dependency injection. How spring IOC container uses dependency injection mechanism to autowire all the dependencies of object.

In this post, we will learn about what is bean, scope of bean and how we can implement lifecycle callbacks of bean. Let’s first understand what is bean?. Bean is nothing but class with name, scope and it contains state which is loaded from configuration such as xml, annotation or java configuration. Bean should have the following properties in order to qualify as bean, It should have name and scope, It may have constructor arguments, properties, autowiring mode, lazy initialization, init and destruct callback methods.

How beans are named?. When component scanning is in the classpath, Spring generates bean names for unnamed beans by automatically converting upper case of the first character classname into lower case.

We have already understood how spring container is created and beans are loaded and registered into the spring container. In the following examples, we will see how bean is defined through annotation based configuration.

// Simple Bean
@Component
class UserService {

}

// simple bean with scope, constructor, properties
@Scope("singleton")
@Component
class UserService {

UserDao userDao;
@Autowired
public UserService(UserDao userDao) {
this.userDao = userDao;
}
}

// simple bean with scope, constructor, properties, lifecycle callback and lazy initialization

@Lazy
@Scope("singleton")
@Component
class UserService {

UserDao userDao;
@Autowired
public UserService(UserDao userDao) {
this.userDao = userDao;
}

@PostConstruct
public void init() {
System.out.println("invoked after userService instance is created")
}

@PreDestroy
public void destroy() {
System.out.println("invoked before userService instance is destroyed")
}
}

Bean Scope

Bean scope refers how long the bean will live. Let’s consider the same above example, We have defined UserService bean which has singleton scope. Singletone scope means only one instance of the bean will be created and registered per container. Whenever we create a new bean which depends on any singleton scoped bean, the same singleton instance is reused across the ApplicationContext. In the above example, container will load the UserService bean and register in the name of userService so that other beans will find this by name. There are multiple bean scopes are available such as request, session, application, websocket ..etc.

In the following section, we will see and understand how beans are scoped. Some commonly used bean scope examples. In the following diagrams, we can clearly see how singleton and prototype bean are scoped and accessed by other beans.

The following are the scope mechanism which are used by spring to specify how the objects or beans should be created or destroyed based on the scope.

singleton — Only one instance of the bean is created with id. All the requests calling that bean will access the same bean object. Singleton is scoped per container.

prototype — New instance of the bean is created, every time the object is referenced from other collaborating beans.

request — New instance of the bean is created for every http request.

session — Bean creation is scoped at session level. Bean is created and same bean is reused for the same http session.

application — Bean is created once for the ServletContext. It is scoped at ServletContext.

websocket — New bean is created for the lifecycle of WebSocket.

@scope("singleton")
@Component
class SingletonBean {
@Autowired
DaoService daoService;
}

// Request scope
@RequestScope
@Component
public class RequestBean {
@Autowired
DaoService daoService;
}

// session scoped bean
@SessionScope
@Component
public class SessionBean {
@Autowired
DaoService daoService;
}

// application scoped bean, scoped per servletContext
@ApplicationScope
@Component
public class ApplicationBean {
@Autowired
DaoService daoService;
}

Conclusion

In this blog post, so far we have understood the definition of Bean. How the beans are scoped at different level. How scope plays a role in the lifecycle of bean. We have also seen an example of how bean is defined.

--

--

No responses yet