Module 3 Assignments Java (MCA)
Module 3 Assignments Java (MCA)
Module 3 Assignments Java (MCA)
Jar file: For every spring Application we require Spring Jar File
https://drive.google.com/drive/folders/1VFtflyViwGabDTiDNjkPy3F06ixO
GREw?usp=sharing
Create a simple spring application to print Hello World
package com.constructorinject;
POJO Class : Address
public class Address {
private String city;
private String state;
private String country;
public Address(String city, String state, String country) {
super();
this.city = city;
this.state = state;
this.country = country;
}
public String toString() {
return city + " " + state + " " + country;
}
}
package com.constructorinject;
public class Employee {
private int id;
private String name; POJO Class : Employee
private Address address;//Aggregation
public Employee() {System.out.println("def cons");}
public Employee(int id, String name, Address address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
void show(){
System.out.println(id+" "+name);
System.out.println(address.toString());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
config.xml
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="a1" class="com.constructorinject.Address">
<constructor-arg value="Mumbai" index="0"></constructor-arg>
<constructor-arg value="Maharashtra" index="2"></constructor-arg>
<constructor-arg value="India" index="1"></constructor-arg>
</bean>
<bean id="e" class="com.constructorinject.Employee">
<constructor-arg value="102" type="int"></constructor-arg>
<constructor-arg value="Shrikant"></constructor-arg>
<constructor-arg>
<ref bean="a1"/>
</constructor-arg>
</bean>
</beans>
package com.constructorinject; Main method class
import org.apache.log4j.lf5.util.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
// ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
ClassPathResource r=new ClassPathResource("config.xml");
BeanFactory factory=new XmlBeanFactory(r);
Employee s=(Employee)factory.getBean("e");
s.show();
}
}