Spring bean, life cycle, and creation from a java developer point of view.

This story will tell about the basic thing that will come into the mind of the developer when using spring bean.

What is actually spring bean? Is there any other naming convention used?

Spring bean is an object that forms your application's backbone and is managed by the Spring Ioc container.

How you use spring bean in your project?Most used annotation?

  • @Component: class-level annotation which by default denotes a bean with the same name as the class name with a lowercase first letter. It can be specified a different name using the value argument of the annotation
import org.springframework.stereotype.Component;

* @Repository: class-level annotation representing the DAO layer of an application; it enables an automatic persistence exception translation.

import org.springframework.stereotype.Repository;
@Repository
public interface Repository extends CassandraRepository<tLog, UUID> {

}

* @Service: class-level annotation representing where the business logic usually resides and can flavor the reuse of the code.
* @Controller: class-level annotation representing the controllers in Spring MVC (Model-View-Controller). See also @RestController for the REST “mode”.
* @Configuration: class-level annotation to say that it can contain bean definition methods annotated with @Bean

Spring bean life cycle you should know?

The bean life cycle like all other life cycles mainly depends on below.

  • When the object is created and how it is created
  • Purpose of the object and where it is used
  • How and when it s destroyed

Spring bean life cycle over ride method

PostConstruct, init-method are BeanPostProcessors.

  1. @PostConstruct is a JSR-250 annotation while init-method is Spring's way of having an initializing method.
  2. If you have a @PostConstruct method, this will be called first before the initializing methods are called.
  3. If your bean implements InitializingBean and overrides afterPropertiesSet , first @PostConstruct is called, then the afterPropertiesSet and then init-method.

Scope of beans and their used cases?

The scope of bean means the life cycle of how the bean should be created and processed for each request

  1. Singleton: it means the container creates a single instance of that bean and when requested (by name or type) the same object will be returned. This is by default and most of the application is using this scope.

Use case: default normally all the beans scope

2. Prototype: a different instance is created every time it is requested from the container.

NOTE: As a rule of thumb, you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans.

3. Request: creates a bean instance for a single HTTP request

4. Session: creates a bean instance for each HTTP session

5. Application: creates a bean instance for the lifecycle of a ServletContext

6. WebSocket: creates a bean instance for a particular WebSocket session.

Choosing the type of proxy created

By default, when the Spring container is creating a proxy for a bean that is marked up with the <aop:scoped-proxy/> element, a CGLIB-based class proxy will be created. This means that you need to have the CGLIB library on the classpath of your application.

Note: CGLIB proxies will only intercept public method calls! Do not call non-public methods on such a proxy; they will not be delegated to the scoped target object.

You can choose to have the Spring container create ‘standard’ JDK interface-based proxies for such scoped beans by specifying ‘false' for the value of the 'proxy-target-class' attribute of the <aop:scoped-proxy/> element. Using JDK interface-based proxies does mean that you don't need any additional libraries on your application's classpath to effect such proxying, but it does mean that the class of the scoped bean must implement at least one interface, and all of the collaborators into which the scoped bean is injected must be referencing the bean via one of its interfaces.

Display all spring bean?

Implement the main class with CommandLineRunner/ApplicationRunner interface and override its run method.

package com.java4s.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringBootApp implements CommandLineRunner {

@Autowired
private ApplicationContext context;

public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
}

@Override
public void run(String...args) throws Exception {
String[] beans = context.getBeanDefinitionNames();

for (String bean: beans) {
System.out.println(bean);
}
}
}

Java 8 | Spring | React | Angular | CI/CD | Microservices | Monitoring |Apache Kafka. https://www.linkedin.com/in/shuaib-kunhabdulla-83780188