Posts

Singleton and Prototype Bean Scopes in Java Spring

  Bean Scopes   refers to the lifecycle of Bean that means when the object of Bean will be instantiated, how long does that object live, and how many objects will be created for that bean throughout. Basically, it controls the instance creation of the bean and it is managed by the spring container. Bean Scopes in Spring   The spring framework  provides five scopes for a bean. We can use three of them only in the context of web-aware  Spring ApplicationContext  and the rest of the two is available for both  IoC container and Spring-MVC container . The following are the different scopes provided for a bean:    Singleton:  Only one instance will be created for a single bean definition per Spring IoC container and the same object will be shared for each request made for that bean. Prototype:  A new instance will be created for a single bean definition every time a request is made for that bean. Request:  A new instance wi...

Difference between Singleton and Prototype in Java Spring

  Singleton Prototype Only one instance is created for a single bean definition per Spring IoC container A new instance is created for a single bean definition every time a request is made for that bean. Same object is shared for each request made for that bean. i.e. The same object is returned each time it is injected. For each new request a new instance is created. i.e. A new object is created each time it is injected. By default scope of a bean is singleton. So we don’t need to declare a been as singleton explicitly. By default scope is not prototype so you have to declare the scope of a been as prototype explicitly. Singleton scope should be used for stateless beans. While prototype scope is used for all beans that are stateful

Spring Framework Stereotype Annotations

  @Component:  It is a class-level annotation. It is used to mark a Java class as a bean. A Java class annotated with  @Component  is found during the classpath. The Spring Framework pick it up and configure it in the application context as a  Spring Bean . A component is responsible for some operations. Spring framework provides three other specific annotations to be used when marking a class as a Component. @Service @Repository @Controller 1: @Service:  We specify a class with @Service to indicate that they’re holding the business logic. Besides being used in the service layer, there isn’t any other special use for this annotation. The utility classes can be marked as Service classes. 2: @Repository:  We specify a class with @Repository to indicate that they’re dealing with  CRUD operations , usually, it’s used with DAO (Data Access Object) or Repository implementations that deal with database tables. 3: @Controller:  We specify a class wit...

Spring Framework Standalone Collections

Image
Spring Framework allows to inject of collection objects into a bean through constructor dependency injection or setter dependency injection using  <list>,<map>,<set> , etc. Given below is an example of the same. Example Project pom.xml:  XML < project xmlns = " http://maven.apache.org/POM/4.0.0 "             xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "             xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0                                http://maven.apache.org/xsd/maven-4.0.0.xsd ">    < modelVersion >4.0.0</ modelVersion >        < groupId >org.example</ groupId >    < artifactId >GFGArticles</ artifactId...