Advance Java Practical Journal
ISBN 9788119221226

Highlights

Notes

  

Chapter 7: Practical program on Core Java Spring Framework

Practical 7.1

1 Write a program to print “Hello World” using spring HelloWorld.java

package com.hiraymca;

public class HelloWorld {

private String message;

public void getMessage() {System.out.println(“Your message=“+message);

}

public void setMessage(String message) {

this.message = message;

}

}

MainApp.java

package com.hiraymca;

import org.springframework.context.ApplicationContext;

import

org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

// TODO Auto-generated method stub ApplicationContext context=new

ClassPathXmlApplicationContext(“Beans.xml”);

HelloWorld obj=(HelloWorld)context.getBean(“helloworld”); obj.setMessage(“Hello World”);

obj.getMessage();

}

}

Beans.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 xsi:schemaLocation=http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd>

<bean id=“helloworld” class=“com.hiraymca.HelloWorld” />

</beans>

Output

Your message=Hello World

Practical 7.2

Write a program to demonstrate dependency injection via setter method.

Employee.java

package com.hiraymca;

public class Employee {private String ename; private int eage; private int esal;

public String getEname() {

return ename;

}

public void setEname(String ename) {

this.ename = ename;

}

public int getEage() {

return eage;

}

public void setEage(int eage) {

this.eage = eage;

}

public int getEsal() {

return esal;

}

public void setEsal(int esal) {

this.esal = esal;

}

public void display()

{

System.out.println(“Name=“+ename+”\n”+”Age=“+eage+”\n”+”Salary=“+ esal);

}

}

MainApp.java

package com.hiraymca;

import org.springframework.context.ApplicationContext;

import

org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

// TODO Auto-generated method stub ApplicationContext context=new

ClassPathXmlApplicationContext(“Beans.xml”);

Employee e=(Employee)context.getBean(“employee”); e.display();

}

}

Beans.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 xsi:schemaLocation=http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd>

<bean id=“employee” class=“com.hiraymca.Employee”>

<property name=ename” value=“AAA”></property>

<property name=eage” value=“24”> </property>

<property name=esal” value=“75000”></property>

</bean>

</beans>

Output

Name=AAA

Age=24

Salary=75000

Practical 7.2b

Write a program to demonstrate dependency injection via setter method using beanfactory container

Employee.java

package com.hiraymca;

public class Employee {private String ename; private int eage; private int esal;

public Employee(String ename, int eage, int esal) {

super();

this.ename = ename; this.eage = eage;

this.esal = esal;

}

public Employee() {this.ename=“XYZ”; this.eage=22; this.esal=65000;

}

public String getEname() {

return ename;

}

public void setEname(String ename) {

this.ename = ename;

}

public int getEage() {

return eage;

}

public void setEage(int eage) {

this.eage = eage;

}

public int getEsal() {

return esal;

}

public void setEsal(int esal) {

this.esal = esal;

}

public void display()

{

System.out.println(“Name=“+ename);

System.out.println(“Age=“+eage); System.out.println(“Salary=“+esal);

}

}

Beans.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 xsi:schemaLocation=http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd>

<bean id=“emp” class=“com.hiraymca.Employee” > </bean>

</beans>

MainApp.java

package com.hiraymca;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

// TODO Auto-generated method stub

BeanFactory factory=new ClassPathXmlApplicationContext(“Beans.xml”); Employee e=(Employee)factory.getBean(“emp”);

e.display();

}

}

Output

Name=XYZ Age=22 Salary=65000

Practical 7.3

Write a program to demonstrate dependency injection via Constructor.

Employee.java

package com.hiraymca;

public class Employee {private String ename; private int eage; private int esal;

public Employee(String ename, int eage, int esal) {

super();

this.ename = ename; this.eage = eage;

this.esal = esal;

}

public Employee()

{

this.ename=“Dukhiram”; this.eage=75; this.esal=5000;

}

public String getEname() {

return ename;

}

public void setEname(String ename) {

this.ename = ename;

}

public int getEage() {

return eage;

}

public void setEage(int eage) {

this.eage = eage;

}

public int getEsal() {

return esal;

}

public void setEsal(int esal) {

this.esal = esal;

}

public void display()

{

System.out.println(“Name=“+ename); System.out.println(“Age=“+eage);

System.out.println(“Salary=“+esal);

}

}

MainApp.java

package com.hiraymca;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {

public static void main(String[] args) {

// TODO Auto-generated method stub

BeanFactory factory=new ClassPathXmlApplicationContext(“Beans.xml”);

Employee e=(Employee)factory.getBean(“emp”);

e.display();

}

Beans.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 xsi:schemaLocation=http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd>

<bean id=“emp” class=“com.hiraymca.Employee” >

<constructor-arg index=“0” value=“sukhiram”/>

<constructor-arg index=“1” value=“65”/>

<constructor-arg index=“2” value=“15000”/>

</bean>

Output

Name=sukhiram

Age=65

Salary=15000