A4 - My Spring Boot Project (name- FirstPro)

  YELLOW IS for Java Project 

  BLUE IS for   Folder

  GREEN IS for Package

  RED IS for Class 



_____________________________________________________________________________


FirstPro

       src/main/java

                    com.example.demo

                                  Alien.java

                                  FirstProApplication.java

                                  Food.java


________________________________________________________________________________


Alien.java

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component  
@Scope(value="prototype")
public class Alien {
    private int id;
    private String name;
    private String tech;
   
    @Autowired
    public Food food;
   
   
    public Alien() {
          System.out.println("Constructor of Alien Created");
       
    }
   
    //Getter and Setter for the parameters
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
   
    public String getTech() {
        return tech;
    }
    public void setTech(String tech) {
        this.tech = tech;
    }
   
    // A method to print something and check IDE is working or not and food autowiring
  public void print() {
      System.out.println("I am a human being");
      food.eat();
  }
   
}




FirstProApplication.java

/*
--->  What is bean?
--->  A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
      Otherwise, a bean is simply one of many objects in your application.
 
 
________________________________@Component______________________________________________
--> We use @Component keyword to tell framework I want object from this class
-->  Spring will: Scan our application for classes annotated with @Component.
       Instantiate them and inject any specified dependencies into them.
_______________________________________________________________________________________


_________________________________@Scope(value="prototype")_____________________________________________________
--> If we do not use @Scope(value="prototype")  then by default spring framework follows Singleton design pattern
--> Singleton design pattern  means object created with or without instantiation
--> This means constructor executed only once with or without instantiation[Alien a = context.getBean(Alien.class);]
--> Scope prototype means that every time you ask spring (getBean or dependency injection)
    for an instance it will create a new instance and give a reference to that.
_______________________________________________________________________________________________




_____________________________________@Autowired________________________________________________________
-->If we use @Autowired means you have connected or imported that class into this particular class
   For example - Food class is imported or connected inside of Alien Class

-->Autowiring feature of spring framework enables you to inject the object dependency implicitly.
   It internally uses setter or constructor injection.
   Autowiring can't be used to inject primitive and string values. It works with reference only.
   __________________________________________________________________________________________________
  */


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class FirstProApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context= SpringApplication.run(FirstProApplication.class, args);
       
       
        Alien a = context.getBean(Alien.class);
        a.print();
       
    }
}



Food.java

package com.example.demo;



import org.springframework.stereotype.Component;



@Component
public class Food {
public void eat() {

    System.out.println("I am eating");
}
}






































Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot