<property>
or <constructor-arg>
tags.
<bean id="engine" class="com.example.Engine" />
<bean id="car" class="com.example.Car">
<property name="engine" ref="engine" />
</bean>
<property>
tag. Spring will not attempt to inject dependencies automatically.
<property>
or <constructor-arg>
tags in the XML configuration. Spring does not attempt to inject anything by itself.
<constructor-arg>
tag. <property>
tag. no
using <constructor-arg>
tag.
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;
// Constructor for dependency injection
public Car(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">
<!-- Define Engine bean -->
<bean id="engine" class="in.sp.beans.Engine" />
<!-- Define Car bean with constructor-based dependency injection -->
<bean id="car" class="in.sp.beans.Car">
<constructor-arg ref="engine" />
</bean>
</beans>
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)
{
// Load Spring context from XML configuration
ApplicationContext context = new ClassPathXmlApplicationContext("in/sp/resources/applicationContext.xml");
// Retrieve the Car bean
Car car = context.getBean(Car.class);
// Call the drive method
car.drive();
}
}
Engine started... Car is running...
no
using <property>
tag.
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;
// Setter method for dependency injection
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">
<!-- Define Engine bean -->
<bean id="engine" class="in.sp.beans.Engine" />
<!-- Define Car bean with explicit dependency injection -->
<bean id="car" class="in.sp.beans.Car">
<property name="engine" ref="engine" />
</bean>
</beans>
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)
{
// Load Spring context from XML configuration
ApplicationContext context = new ClassPathXmlApplicationContext("in/sp/resources/applicationContext.xml");
// Retrieve the Car bean
Car car = context.getBean(Car.class);
// Call the drive method
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.