Module 3 Assignments Java (MCA)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Assignment No 7 : based on Spring Framework

1. Create a simple spring application to print Hello World


2. Write a program to demonstrate dependency injection via setter method. (Bank
Account Application).
3. Write a program to demonstrate dependency injection via Constructor (Employee
Application).
Spring Application Setup
1. Select Java project.
2. Add the required spring jar files explicitly
3. Create POJO class in src/package
4. Create config.xml file in eclipse under src folder
a. Create beans for POJO class.
5. In app.java file
a. Create a spring IOC Container to load the beans.

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

POJO Class : HelloWorld.java


package com.springpkg;
public class HelloWorld {
public void display() {
System.out.println("Hello World!");
}
}
config.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans
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="helloworld" class="com.springpkg.HelloWorld"></bean>
</beans>
App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String args[]) {
ApplicationContext context =new
ClassPathXmlApplicationContext("config.xml");
HelloWorld hw=(HelloWorld)context.getBean("helloworld");
hw.display();
}
}
2. Write a program to demonstrate dependency injection via setter method.
(Bank Account Application).

Pojo class of Account


config.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans
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="acc" class="com.setterinject.Account">
<property name="acc" value="05">
</property>
<property name="accName" value="HDFC">
</property>
</bean>
</beans>
Main File
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");

Account acc = (Account) context.getBean("acc");

System.out.println("Account No = " + acc.getAcc());

System.out.println("Account Name = " + acc.getAccName());


For More :
https://www.benchresources.net/how-to-generate-co
nstructor-using-fields-in-eclipse-ide/
3.Write a program to demonstrate dependency injection via Constructor (Employee
Application).

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();

}
}

You might also like