Java Spring Autowiring

In Spring, autowiring is a feature that allows the Spring container to automatically inject dependencies into your beans. Let's go through a step-by-step tutorial on how to use autowiring in the Spring Core module.


Step 1: Create a Maven Project

You can use a tool like Maven to manage your project dependencies. Create a new Maven project and add the necessary dependencies for Spring. Include the following dependencies in your pom.xml file:

<!-- Maven Dependency -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.12.RELEASE</version> <!-- Use the latest version available -->
</dependency>

Step 2: Create Beans to Autowire

Let's create a few classes that represent beans to be autowired. For example, consider a Car class and an Engine class.

// Engine.java
public class Engine {
    private String type;

    public Engine(String type) {
        this.type = type;
    }
    public String getType() {
        return type;
    }
}

// Car.java
public class Car {
    private Engine engine;

    public Car(Engine engine) {
        this.engine = engine;
    }

    public void start() {
        System.out.println("Car is starting with " + engine.getType() + " engine.");
    }
}

Step 3: Create a Spring Configuration Class

Create a Spring configuration class where you define your beans and enable autowiring. You can use XML or Java-based configuration. Here, we'll use Java-based configuration.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.yourpackage") // Specify the package
to scan for components
public class AppConfig {

    @Bean
    public Engine engine() {
        return new Engine("V8");
    }

    // Car bean will be autowired with the Engine bean
    @Bean
    public Car car() {
        return new Car(engine());
    }
}

Replace "com.yourpackage" with the actual package where your classes are located.


Step 4: Create a Main Application

Create a main class to bootstrap the Spring application and retrieve the beans.

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new
        AnnotationConfigApplicationContext(AppConfig.class);

        Car car = context.getBean(Car.class);
        car.start();

        context.close();
    }
}

Step 5: Run the Application

Run the Application class, and you should see output indicating that the car is starting with a V8 engine.

This is a simple example of how autowiring works in the Spring Core module. The Car bean gets automatically wired with the Engine bean without explicitly specifying dependencies in the configuration.


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext