Advance Java Practical Journal
ISBN 9788119221226

Highlights

Notes

  

Annexure – V: Spring Important

What is Spring Framework and its core module? Or

Explain IoC container of Spring Farmework.

Ans.

The Spring Framework is in built package and methods or class using java API to perform Enterprise Projects.

The Spring Framework having following two IoC Spring container where are codes are stored and executed.

    a. A light weight Spring Container called as BeanFactory

    b. ApplicationContext (Annotation based container)

Using above core module, we can develop only standalone application

Using this core module we can manage Spring lifecycle methods and dependency injection.

The Spring container is a Software application or Java classes that can take care of the whole life cycle of Spring program till execution completion

    A. BeanFactory Container

To activate BeanFactory container in our application create object for java class that implements org.springframework.beans.factory.Beanfactory interface

Syntax is

XmlBeanFactory factory = new XmlBeanFactory(“<object poiting to Spring ”);

Where XmlBeanFactory -> class implementing BeanFactory

Factory - > represents BeanFactory container

    B. ApplicationContext

To activate ApplicationContext container in our Application create Object for Java class that implements

org.springframeowrk.context.ApplicationContext (interface)

Ex.

SystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(“<spring config file>”);

Where SystemXmlApplicationContext - > class that implementing ApplicationContext interface

Spring Container is a lightweight container because Spring container activate execution within Spring jar files and it does not require any other Software tools.

For assumption, we suppose that we have to execute Servlet and JSP program, then we have to create object in any other software or framework Bundle for execution. For ex. Tomcat Server

The Spring Core Module Application contains the following resources

    1. Spring interface (Java Interface)

    2. Spring Bean Class(Java Class with Setter and Getter Methods)

    3. Spring configuration file (XML file)

    4. Client Application (java class with main method)

    A. Spring Interface

    It is always POJI.

    Sometimes creating of Spring interface is optional but if we use then it will be better for future reference.

    This interface only contains declaration of business method only

    B. Spring Bean class

    This class implements Spring interface abstract methods and provides body or logic into it.

    In this class, we have to provide setter and getter methods to the properties of class.

    This class may be define with POJO or non-POJO concept.\

    This class can contains injection methods.

    C. Spring Configuration File (XML files)

    Any <filename>.xml can be taken as Spring Config. File

    In one application, we can define one or more spring configuration file

    This xml file contains Dependency Injection and IoC methods instructions.

    It can be develop using DTD or Schema rules

    D. Client Application

    This is used to activate Spring container by specifying Spring Configuration file

    Get Spring Bean class object from bean container. And using this object , call the Business methods of Bean class to provide input.

Java Spring supports 3 types of Dependency Injection(DI)

    1. Setter injection by using setXXX(-) setter method of Spring Bean class.

    2. Constructor Injection by using parameterized constructor Spring Bean class)

    3. Interface Injection by implementing special interface on SpringBean class)

Dependency Injection (DI) can be defines as under the any server or container execution environment we have to assign dependant value to our resources file(ex. Class / Object).

    A. Setter Injection

    B. In Spring container calls setXXX(-) setter method automatically and synamically assign value to the properties (member function of Bean class) of class.

Spring POJO Bean class code

package com.setter.injection;

import java.util.Date;

public class WishGenrator {

String name;

Date d;

public WishGenrator()

{

}

public WishGenrator(String name, Date d) {

super();

this.name = name;

this.d = d;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Date getD() {

return d;

}

public void setD(Date d) {

this.d = d;

}

}

2. Configuration File XML file

Go to the following website and copy and paste

https://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html

<?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=“dt” class=“java.util.Date”>

</bean>

<bean id=“msg” class=“com.setter.injection.WishGenrator”>

<property name=“name” value=“Hiray College”/>

</bean>

</beans>

3. Client Application

package com.setter.injection;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import org.springframework.core.io.FileSystemResource;

public class ClientApp {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(“This Setter Injection Demo”);

ApplicationContext context;

context = new FileSystemXmlApplicationContext(“src/applicationcontext.xml”);

WishGenrator bean = context.getBean(“msg”,WishGenrator.class);

String message = bean.toString();

System.out.println(message);

}

}

Annotation Based Configuration File Demo

Prog.1

package com.annotation.base;

public class SIDepartment {

String deptName;

int deptId;

public String getDeptName() {

return deptName;

}

public int getDeptId() {

return deptId;

}

public void setDeptName(String deptName) {

this.deptName = deptName;

}

public void setDeptId(int deptId) {

this.deptId = deptId;

}

}

Configuration file code

package com.annotation.base;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Configuration

@ComponentScan(basePackages = {“com.annotation.base”})

public class SIEmployeeConfiguration {

@Bean(name = “siemployee”)

public SIEmployee siemployee()

{

SIEmployee emp = new SIEmployee();

SIDepartment dept = new SIDepartment();

dept.setDeptId(1000);

dept.setDeptName(“MCA”);

emp.setDept(dept);

emp.setEmpId(14990);

emp.setEmpName(“Amey”);

emp.setEmpSal(100000.00d);

return emp;

}

}

    4. Client Application

    5. package com.hiray.di.setter;

    6. public class SIDepartment {

    7. String deptName;

    8. int deptID;

    9. public SIDepartment()

    10. {

    11. }

    12. public String getDeptName() {

    13. return deptName;

    14. }

    15. public int getDeptID() {

    16. return deptID;

    17. }

    18. public void setDeptName(String deptName) {

    19. this.deptName = deptName;

    20. }

    21. public void setDeptID(int deptID) {

    22. this.deptID = deptID;

    23. }

    24. }

----]

package com.hiray.di.setter;

public class SIEmployee {

String empName;

int empId;

double empSal;

SIDepartment dept;

public SIEmployee()

{

}

public String getEmpName() {

return empName;

}

public int getEmpId() {

return empId;

}

public double getEmpSal() {

return empSal;

}

public void setEmpName(String empName) {

this.empName = empName;

}

public void setEmpId(int empId) {

this.empId = empId;

}

public void setEmpSal(double empSal) {

this.empSal = empSal;

}

public SIDepartment getDept() {

return dept;

}

public void setDept(SIDepartment dept) {

this.dept = dept;

}

}

<?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=“department” class=“com.hiray.di.setter.SIDepartment”>

<property name=“deptName” value=“MCA”/>

<property name=“deptID” value=“123”/>

</bean>

<bean id=“employee” class=“com.hiray.di.setter.SIEmployee”>

<property name=“empId” value=“1234”></property>

<property name=“empName” value=“Amey”/>

<property name=“empSal” value=“10000”/>

<property name=“dept” ref=“department”/>

</bean>

</beans>

package com.hiray.di.setter;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SetterInjectionTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext context;

context = new ClassPathXmlApplicationContext(“beansetter.xml”);

SIEmployee emp = (SIEmployee)context.getBean(“employee”,”SIEmployee.class”);

System.out.println(“Emp Name”+emp.getEmpName());

System.out.println(“ID”+emp.getEmpId());

System.out.println(“Salaray”+emp.getEmpSal());

System.out.println(“Department name”+emp.getDept());

System.out.println(“Department ID”+emp.getEmpId());

}

}

    C. Constructor Based Injection

It is one of the type of Dependency Injection where objects constructor is used to inject dependencies.

This type of injection is safe as the object will not get created if the dependencies are not available. Or dependencies can not be resolve.

Prog1.

package com.org.constructor;

public class CIDepartment {

String deptName;

int deptId;

public CIDepartment(String deptName, int deptId) {

super();

this.deptName = deptName;

this.deptId = deptId;

}

public String getDeptName() {

return deptName;

}

public int getDeptId() {

return deptId;

}

public void setDeptName(String deptName) {

this.deptName = deptName;

}

public void setDeptId(int deptId) {

this.deptId = deptId;

}

}

=======================================

Prg2.

package com.org.constructor;

public class CIEmployee {

String empName;

int empId;

double empSal;

CIDepartment dept;

public CIEmployee(String empName, int empId, double empSal, CIDepartment dept) {

super();

this.empName = empName;

this.empId = empId;

this.empSal = empSal;

this.dept = dept;

}

public String getEmpName() {

return empName;

}

public int getEmpId() {

return empId;

}

public double getEmpSal() {

return empSal;

}

public CIDepartment getDept() {

return dept;

}

}

-

Prog3. 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=“cidept” class=“com.org.constructor.CIDepartment”>

<constructor-arg value=“technology”/>

<constructor-arg value=“123”/>

</bean>

<bean id=“ciemp” class=“com.org.constructor.CIEmployee”>

<constructor-arg value=“Krutan”/>

<constructor-arg value=“12”/>

<constructor-arg value=“12345.78”/>

<constructor-arg ref=“cidept”/>

</bean>

</beans>

==================================

Prog.4

package com.org.constructor;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClientApp {

@SuppressWarnings(“resource”)

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext context;

context = new ClassPathXmlApplicationContext(“beans.xml”);

//BeanFactory factory;

//factory = new XmlBeanFactory(new ClassPathResource(“beans.xml”));

CIEmployee emp = (CIEmployee)context.getBean(“ciemp”);

System.out.println(“Employee Name”+emp.getEmpName());

System.out.println(“Employee ID”+emp.getEmpId());

System.out.println(“Emp Salaray”+emp.getEmpSal());

System.out.println(“Department name”+emp.getDept().getDeptId());

System.out.println(“Department ID”+emp.getDept().getDeptName());

}

}