โŒ

Normal view

There are new articles available, click to refresh the page.
Yesterday โ€” 30 June 2025Main stream

spring Boot Introduction -II

By: Prasanth
30 June 2025 at 13:36

Step:1 Open Eclipse
If you Don not install ,install
https://www.eclipse.org/downloads/

Step:2 Install Spring Tools (Spring Tool Suite 4) Plugin
1.Go to menu:
Help-> Eclipse marketplace
2.Type Spring tool In search bar
Spring Tools 4 (aka Spring Tool Suite 4)
then install. Restart Eclipse.

Step:3 create spring boot project
File->New-> Spring starter Project.

Demo1 obj1 = new Demo(); // tightly couple
Demo1 obj1; // loosely couple

Now not need any dependencies , now I skip dependencies.

Image description

Jar,War,Dependencies,Pom.xml,Maven,Gradel,Spring,Spring Boot,yml,Application properties,Tomcat..etc to read this Blog:
https://dev.to/prasanth362k/spring-boot-1-2-class-14h0

Group:Package name
Artifact: Project Name

Image description

Project Structure:-

Image description

Demo1 is project name

src/main/java:

This folder contains your java source code:

  • @SpringBootApplication main class
  • Controllers (@RestController)
  • Services (@Service)
  • Repositories (@Repository)
  • Model classes (@Entity)

src/main/resources

This folder stores all configuration and static files like:

  • application.properties or application.yml
  • static/ (HTML, CSS, JS)
  • templates/ (Thymeleaf or JSP)
  • .sql or .xml files (if needed)

this location configure Database url,Db username/password,server port number.

src/test/java

This used for writing Junit test cases to check if controller/Api is working?,Service logic is correct?, function returns should give correct output.it helps tester and developer find bugs early.

In real time first we done testing the project then deployment.

JRE System Library [JavaSE-17]

This Library provide all the core java classes (like String,List,System.out.println) you project needs.without library won't understand basic java code

For Example-

System.out.println("Hello World");  // Needs java.lang classes from JRE

why it's need?
java compiler version ( project built in using java 17),Runtime version ( you app will run run on java 17), this both need java 17 or higher version for spring boot .that need to your project.

Image description

Image description

Maven Dependencies

This file contain .jar file(libraries) downloaded form maven(internet) based on you what you add dependency from pom.xml .

why and what Maven dependencies and JRE System Library [JavaSE-17] both contain jar file inside package and inside .class file.

Maven Dependencies:

Image description

JRE System Library [JavaSE-17]:

Image description

.jar -> is a compressed file format or Zip folder for .class file,.jar contain .class, .properties,xml related. it user for java application not for web application(war) , can be executable to run with java -var app.jar(main class) , inside .jar you see .compile files.

.class , created after compiling .java,platform indepenedent(can run any osusing jvm),it is not human readable(byte code(plain text)),it is single file not a zip folder

target/

maven stores all compiled code and build files here.After mvn install, .class and .jar files go here.his is auto-generated โ€“ don't modify manually.

pom.xml

The main configuration file for Maven:
Add dependencies (Spring Web, JPA, Lombok)

In this blog i demonstrate how to call method without object new keyword only class reference not create object like.Before 2 thing definitely to know one for what is tightly coupling and loose coupling why to use then what is spring container please read my previews blog

By Default Main program start this file

package com.example.demo;

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

import com.example.demo.service.SMSNotificationService;



@SpringBootApplication
public class DemoApplication {

    private NotificationService notificationService;

    public static void main(String[] args) {
        // before SpringApplication.run(DemoApplication.class, args);
        ConfigurableApplicationContext context  = SpringApplication.run(DemoApplication.class, args);// after 
        //it is tigtely couple relation to avoid like without new key word
        SMSNotificationService sms =context.getBean(SMSNotificationService.class);
        //SMSNotificationService sms ; // = new  SMSNotificationService();
        //SMSNotificationService sms;
        sms.send_message("abcd ","1234");
    }

}

Step:2
I created SMSNotificationService class

package com.example.demo.service;


import org.springframework.stereotype.Component;

import com.example.demo.NotificationService;

@Component
public class SMSNotificationService implements NotificationService{

    @Override
    public void send_message(String to ,String message)
    {
        System.out.println("Sending SMS "+to +" : "+message);

    }
}


Step:3 created Interface

package com.example.demo;

public interface NotificationService {

    //abstract method 
    void send_message(String to ,String message);

}

Program Explanation:

SpringApplication is a Built in spring boot class ,it is helps to create environment for your project .
it does 3 thing: start your spring boot application , create the spring container when run this method SpringApplication.run(DemoApplication.class, args), DemoApplication.class, args why to use you are tell to the spring boot where to begin otherwise spring boot do not know where to start you said start from main.then why args , to pass to arguments in command line like: java -jar app.jar --server.port=9090
,args will contain --server.port=9090, so spring boot can read setting from command line. that all , then i already told SpringApplication.run(DemoApplication.class, args); it give Spring container just like environment(เฎšเฏ‚เฎดเฎฒเฏ)(to create object inside the environment), we create environment like spring container where will be stored that place is ConfigurableApplicationContext (in Built interface)context (reference variable)

SMSNotificationService sms = context.getBean(SMSNotificationService.class);

we Already marked SMSNotificationService @Component annotation ,which tell spring please create object of this class and manages,SpringApplication.run(....) when run this method , start this app and return ConfigurableApplicationContext, this is the spring Container(environment) that manage the all object(beans) ,we already know that ,already created object , now just get the bean(object) of class SMSNotificationService.class from spring container ,then store the variable sms , then call method sms.send_message("abcd ","1234");

context is a reference to the Spring container,getBean(inbuild function)

**
Inversion of control(IoC) and Spring Container and Dependency Injection(DI) lets connect with together?**

  • IoC is a design principle/idea that says:
  • you should not create objects manually using new,instead spring will manage them ,Spring fallow the principle using technique is called DI.Spring container is the environment the create ,store, and manages all thes beans(objects),these object created and stored heap memory,DI achived using Constructor Injection ,Field Injection,Setter Injection.To inject dependencies spring use the @Autowired annotation
    .

  • If you Do not mention @componet on top of class , spring can not create and store object in the spring container.

  • @Component creates one object inside Spring container (by default singleton). by default singleton mean, by default, Spring creates only one object per class โ†’ this is called singleton scope.

  • spring by default create only one object per class , can we create multiple? yes

We learned 2 annotation in this blog:-

@springBootApplicaion
point your mouse arrow int this annotation the press f3 you will some info.
Inside you see lot of annotations combines , in this annotation 3 is important:

1.@Configuration: spring tells this class provide object(bean) , spring should register them spring container.@bean tells spring please call method,take return value and store it spring container.(Do not need to call method manually , automatically call)

Example:-

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}

2.@EnableAutoConfiguration

Tells Spring boot will automatically configure beans based on what is in the classpath For example , you add spring web in pom.xml configure Tomcat server , web settings(DispatcherServlet ), set port automatically you do not do manually ,this automatically configure.
"DispatcherServlet is the front controller in Spring MVC.
It handles all incoming web requests and routes them to the correct @Controller or @RestController methods."

  1. @ComponentScan

Spring scan the current package and sub package @Component,@Service,@Controller,
@Repository automatically create objects(beans) and store them container.Before i said if you mention top of the class @Component object created and store in spring container , @Service,@Controller,
@Repository internally @Component you can point you mouse cursor in annotation and press f3 .
it will create objects only for classes :

  • @Component
  • @Service
  • @Controller
  • @Repository

mvt vs mvc

โŒ
โŒ