package in.sp.beans;
import org.springframework.stereotype.Component;
@Component
public class Engine
{
public void start()
{
System.out.println("Engine started...");
}
}
package in.sp.beans;
import org.springframework.stereotype.Component;
@Component
public class Car
{
private Engine engine; // Engine dependency
// Setter method for dependency injection
public void setEngine(Engine engine)
{
this.engine = engine;
}
public void drive()
{
engine.start();
System.out.println("Car is running...");
}
}
NOTE : Do not use final keyword for fields (i.e. private final Engine engine;
) like we used for constructor injection because the nature of final conflicts with the behavior of setter injection.
package in.sp.config;
import in.sp.beans.Car;
import in.sp.beans.Engine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "in.sp.beans")
public class AppConfig
{
@Bean
public Engine engine()
{
return new Engine(); // Manually creating the Engine bean
}
@Bean
public Car car()
{
Car car = new Car(); // Creating Car bean manually
car.setEngine(engine()); // Manually setting the Engine dependency through setter
return car;
}
}
package in.sp.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import in.sp.beans.Car;
import in.sp.config.AppConfig;
public class MainApp
{
public static void main(String[] args)
{
// Loading Spring context from annotations
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Car car = context.getBean(Car.class);
car.drive();
}
}
Engine started... Car is running...
package in.sp.beans;
public class Engine
{
public void start()
{
System.out.println("Engine started...");
}
}
package in.sp.beans;
public class Car
{
private Engine engine;
// Dependency Injection via Setter Method
public void setEngine(Engine engine)
{
this.engine = engine;
}
public void drive()
{
engine.start();
System.out.println("Car is running...");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Engine bean -->
<bean id="engine" class="in.sp.beans.Engine" />
<!-- Car bean with setter injection for Engine -->
<bean id="car" class="in.sp.beans.Car">
<property name="engine" ref="engine" />
</bean>
</beans>
NOTE : Here we have used <property name="----" ref="----" />
tag for Setter Method Injection.
package in.sp.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import in.sp.beans.Car;
public class MainApp
{
public static void main(String[] args)
{
// Loading Spring context from XML configuration
ApplicationContext context = new ClassPathXmlApplicationContext("in/sp/resources/applicationContext.xml");
Car car = context.getBean(Car.class);
car.drive();
}
}
Engine started... Car is running...
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.