DEV Community

Neelakandan R
Neelakandan R

Posted on

5 3 3 3 3

Spring Boot,Annotation

What is Spring Boot?

Spring boot is a framework for building enterprise java application.

Spring boot is a open source java based framework for creating spring based application.

What is Dependency Injection?

Dependency Injection (DI) is a design pattern in Java (and other object-oriented programming languages) used to achieve Inversion of Control (IoC) by injecting dependencies into a class rather than allowing the class to create them itself.

Instead of a class creating its own dependencies (objects), they are passed (injected) from the outside. This makes the code more loosely coupled, more testable, and easier to maintain.

Benefits of DI:

Loose Coupling: The Car class does not depend on a specific Engine type.

Easier Testing: We can pass a mock Engine in unit tests.

Better Maintainability: We can easily replace Engine with ElectricEngine.

What is IOC?

IoC = Giving control to a framework or container instead of manually managing dependencies.

What is an Annotation in Java?

Annotations in Java are special markers (metadata) used to provide additional information about the code without affecting its logic. They are used by the compiler, runtime, and frameworks (like Spring, Hibernate, etc.) for different purposes.

What is Metadata?

Metadata is "data about data." It provides additional information about a file, program, or data structure, making it easier to understand, manage, or process.

What is an Annotation in Spring Boot?

In Spring Boot, annotations are special markers (metadata) that help configure and manage Spring components automatically. They remove the need for XML configurations and make the code cleaner, readable, and easier to maintain.

Why Use Annotations in Spring Boot?

✔ Simplifies Dependency Injection (DI) – Injects objects automatically.
✔ Easier Bean Management – Spring auto-detects components.
✔ Improves Readability & Maintainability – Code is self-explanatory.

What is XML?

XML (Extensible Markup Language) is a structured format used to store and transfer data in a human-readable and machine-readable way.

@Component in Spring Boot

@Component is an annotation in Spring Boot that tells Spring to create and manage an object (bean) automatically. This means you don’t need to manually create objects using new.

@Autowired in Spring Boot

@Autowired is an annotation in Spring Boot that automatically injects dependencies (objects) without needing new. Spring will create and provide the required object automatically.
**
**Why Use @Autowired?

✔ No need for new keyword – Spring creates objects for you.
✔ Automatic Dependency Injection (DI) – Reduces manual wiring.
✔ Loosely Coupled Code – Makes it easier to test and modify.

Example Program:

package com.example.demo;

import org.springframework.stereotype.Component;

@Component
public class Engine {
    String fuel1, fuel2;

//  Engine(String fuel1,String fuel2)
//  {
//      this.fuel1=fuel1;
//      this.fuel2=fuel2;
//  }
    public void start() {
        System.out.println("Engine Started");
    }
}
Enter fullscreen mode Exit fullscreen mode
package com.example.demo;

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

@Component
public class car {
    @Autowired
    Engine engine;

    public static void main(String[] args) {
        // car cr=new car();
        // cr.maruti();
    }

    public void maruti() {
        // Engine engine = new Engine("Petrol");
        engine.start();

    }

}
Enter fullscreen mode Exit fullscreen mode
package com.example.demo;

import org.springframework.boot.SpringApplication;//spring package
import org.springframework.boot.autoconfigure.SpringBootApplication;//spring package
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext cac = SpringApplication.run(DemoApplication.class, args);
        car cr = cac.getBean(car.class);
        cr.maruti();
    }

}

Enter fullscreen mode Exit fullscreen mode

. ____ _ __ _ _
/\ / ' __ _ ()_ __ __ _ \ \ \ \
( ( )_
| '_ | '| | ' \/ ` | \ \ \ \
\/ _
)| |)| | | | | || (| | ) ) ) )
' |
__| .|| ||| |_, | / / / /
=========||==============|_/=////

:: Spring Boot :: (v3.4.4)

2025-04-03T18:18:10.927+05:30 INFO 10662 --- [demo] [ restartedMain] com.example.demo.DemoApplication : Starting DemoApplication using Java 21.0.6 with PID 10662 (/home/neelakandan/Documents/workspace-spring-tool-suite-4-4.29.1.RELEASE/demo/target/classes started by neelakandan in /home/neelakandan/Documents/workspace-spring-tool-suite-4-4.29.1.RELEASE/demo)
2025-04-03T18:18:10.930+05:30 INFO 10662 --- [demo] [ restartedMain] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2025-04-03T18:18:10.973+05:30 INFO 10662 --- [demo] [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2025-04-03T18:18:10.973+05:30 INFO 10662 --- [demo] [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2025-04-03T18:18:11.812+05:30 INFO 10662 --- [demo] [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-04-03T18:18:11.824+05:30 INFO 10662 --- [demo] [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-04-03T18:18:11.824+05:30 INFO 10662 --- [demo] [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.39]
2025-04-03T18:18:11.850+05:30 INFO 10662 --- [demo] [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-04-03T18:18:11.851+05:30 INFO 10662 --- [demo] [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 876 ms
2025-04-03T18:18:12.163+05:30 INFO 10662 --- [demo] [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2025-04-03T18:18:12.183+05:30 INFO 10662 --- [demo] [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2025-04-03T18:18:12.192+05:30 INFO 10662 --- [demo] [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 1.585 seconds (process running for 1.88)
Engine Started

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay