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;
// Dependency Injection via Constructor
public Car(Engine engine)
{
this.engine = engine;
}
public void drive()
{
engine.start();
System.out.println("Car is running...");
}
}
NOTE : Its good practice to use final
keyword for fields (private final Engine engine;
) that should not change after initialization, especially for dependencies injected via constructor 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()
{
return new Car(engine()); // Manually injecting the Engine bean into 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 final Engine engine;
// Dependency Injection via Constructor
public Car(Engine engine)
{
this.engine = engine;
}
public void drive()
{
engine.start();
System.out.println("Car is running...");
}
}
NOTE : Its good practice to use final
keyword for fields (private final Engine engine;
) that should not change after initialization, especially for dependencies injected via constructor injection.
<?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 constructor injection for Engine -->
<bean id="car" class="in.sp.beans.Car">
<constructor-arg ref="engine" />
</bean>
</beans>
NOTE : Here we have used <constructor-arg ref="----" />
tag for Constructor 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 annotations
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.