✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page
  • Using XML as configuration
  • Using Class based configuration
  • Creating a protoptye bean inside singleton class

Was this helpful?

  1. Spring Boot
  2. Spring Basics

Spring Beans

Using XML as configuration

  1. Add the dependency to pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.16</version>
</dependency>

2. Create a spring.xml in resources folder

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="doctor" class="com.backend.springtok.entity.Doctor">
        <property name="qualification" value="MBBS"/>
        <property name="nurse" ref="nurse"/>
        <constructor-arg value="MBBS"/>
    </bean>

</beans>

3.1 Doctor as example

package com.backend.springtok.entity;

public class Doctor implements Staff {

    private String qualification;
    private Nurse nurse;

    public Doctor(String qualification) {
        this.qualification = qualification;
    }

    public void assist() {
        System.out.println("doctor is assisting");
    }

    public String getQualification() {
        return qualification;
    }

    public void setQualification(String qualification) {
        this.qualification = qualification;
    }

    public Nurse getNurse() {
        return nurse;
    }

    public void setNurse(Nurse nurse) {
        this.nurse = nurse;
    }

    @Override
    public String toString() {
        return "Doctor{" +
                "qualification='" + qualification + '\'' +
                ", nurse=" + nurse +
                '}';
    }
}

3. Create a application context

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

// to create a bean
Doctor doctor = context.getBean(Doctor.class);

Using Class based configuration

  1. create a config bean class

@Configuration
@ComponentScan( basePackages = "com.backend.springtok.entity")
public class BeanConfig {

    @Bean
    public NewDoctor getNewDoctor() {
        return new NewDoctor();
    }

}

2. Create class to construct bean of

@Scope(scopeName = "singleton")
// you can define the any other scope like prototype as well
public class NewDoctor implements Staff, BeanNameAware {

    private String qualification;

    public void assist() {
        System.out.println("new doctor is assisting");
    }

    public String getQualification() {
        return qualification;
    }

    public void setQualification(String qualification) {
        this.qualification = qualification;
    }

    @Override
    public String toString() {
        return "NewDoctor{" +
                "qualification='" + qualification + '\'' +
                '}';
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("call for setBeanName");
    }

    @PostConstruct
    public void postConstruct(){
        System.out.println("post construct called");
    }
}

3. Add this to your spring.xml under the beans tag

<context:component-scan base-package="com.backend.springtok"/>

4. Create application context to initialise and manage beans

ApplicationContext anotherContext = 
    new AnnotationConfigApplicationContext(BeanConfig.class);

var newDoctor = anotherContext.getBean(NewDoctor.class);
newDoctor.assist();
newDoctor.setQualification("MBBS++");
System.out.println(newDoctor);

Using the javax.annotation.api , we can annotate different methods at different bean life-cycle to execute some code. Example, in NewDoctor class in above example, we have @PostContruct method which is called after the bean is created by spring.

    @PostConstruct
    public void postConstruct(){
        System.out.println("post construct called");
    }

Creating a protoptye bean inside singleton class

If you declare a prototype bean inside a singleton class, and create 2 different instance of singleton, there prototype bean will also be the same

// example : prototype

@Service
@Scope("prototype")
public class WeatherService {

    String time = LocalDateTime.now().toString();
    int temp = new Random().nextInt(60);

    public String getTodayTemp() {
        return time + " -> " + temp;
    }
}

// service : singleton

@Service
public class UserService {

    @Autowired
    WeatherService weatherService;

    @Autowired
    ApplicationContext context;

    @Autowired
    private ObjectFactory<WeatherService> wFactory;
    // use this to create seperate beans for each singleton instance

    public String getCurrentTemp() {
        return weatherService.getTodayTemp();
    }

    public String getCurrentTempDiff() {
        // not recommended
        // return context.getBean(WeatherService.class).getTodayTemp();
        return wFactory.getObject().getTodayTemp();
    }

}
PreviousSpring BasicsNextImportant Annotations

Last updated 2 years ago

Was this helpful?