0% found this document useful (0 votes)
11 views2 pages

Lab Java 9

The document contains a Java application that demonstrates the use of Spring Framework for dependency injection. It defines an interface 'Machine' with two implementations: 'LatheMachine' and 'MillingMachine'. The 'Workshop' class uses a 'Machine' instance to operate, and the configuration is provided in an XML file, specifying the beans and their dependencies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Lab Java 9

The document contains a Java application that demonstrates the use of Spring Framework for dependency injection. It defines an interface 'Machine' with two implementations: 'LatheMachine' and 'MillingMachine'. The 'Workshop' class uses a 'Machine' instance to operate, and the configuration is provided in an XML file, specifying the beans and their dependencies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.industry.

app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

interface Machine {
void operate();
}

class LatheMachine implements Machine {


public void operate() {
System.out.println("Operating Lathe Machine...");
}
}

class MillingMachine implements Machine {


public void operate() {
System.out.println("Operating Milling Machine...");
}
}

class Workshop {
private Machine machine;

public void setMachine(Machine machine) {


this.machine = machine;
}

public void startWork() {


machine.operate();
}
}

public class IndustryApp {


public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Workshop workshop = (Workshop) context.getBean("workshop");
workshop.startWork();
}
}
<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">

<bean id="latheMachine" class="com.industry.app.LatheMachine"/>


<bean id="millingMachine" class="com.industry.app.MillingMachine"/>

<bean id="workshop" class="com.industry.app.Workshop">


<property name="machine" ref="latheMachine"/>
</bean>

</beans>

You might also like