Advance Java Practical Journal
ISBN 9788119221226

Highlights

Notes

  

Annexure – I: How to run Spring Program using Netbeans

First We have to Create Bean Class using Java

To create Spring Bean Configuration file

Create the main class to run our Spring Bean.

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package com.org.hiray;

/**

*

* @author Vikram

*/

public class Employee {

int id;

String name;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package com.org.hiray;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

*

* @author Vikram

*/

@Configuration

public class EmployeeConfig {

@Bean(name=“employeeBeanObject”)

public Employee newEmployee()

{

Employee emp = new Employee();

emp.setId(1234);

emp.setName(“Virkam”);

return emp;

}

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package com.org.hiray;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**

*

* @author Vikram

*/

public class EmployeeMainTest {

public static void main(String args[])

{

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EmployeeConfig.class);

Employee employee = (Employee)context.getBean(“employeeBeanObject”,Employee.class);

System.out.println(“Employee Id: “+employee.getId());

System.out.println(“Employee Name”+employee.getName());

}

}

Multiple Configuration File imported into one configuration file

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.com;

/**

*

* @author Vikram

*/

public class MyJdbcService {

public void createJdbcConnection()

{

System.out.println(“Creating JDBC connection for demo.............................................”);

}

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.com;

/**

*

* @author Vikram

*/

public class MyBusinessService {

public void runMyBusiness()

{

System.out.println(“Running our Business method for demo *******************”);

}

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.com;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

*

* @author Vikram

*/

@Configuration

public class BusinessConfig {

@Bean(name=“businessservice”)

public MyBusinessService getBusinessService()

{

return new MyBusinessService();

}

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.com;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Import;

/**

*

* @author Vikram

*/

@Configuration

@Import({JdbcConfig.class, BusinessConfig.class})

public class MyAppConfig {

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.com;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**

*

* @author Vikram

*/

public class MainAppTest {

public static void main(String args[])

{

ApplicationContext context = new AnnotationConfigApplicationContext(MyAppConfig.class);

MyJdbcService jdbc = (MyJdbcService)context.getBean(“jdbcservice”);

jdbc.createJdbcConnection();

MyBusinessService busServ = (MyBusinessService)context.getBean(“businessservice”);

busServ.runMyBusiness();

}

}

POJO and POJI program

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.hiray;

/**

*

* @author Vikram

*/

// Demo.java is a (Plain Old Java Interface (POJI Spring Interface)

public interface Demo {

public String generateMsg(String name);

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.hiray;

import java.util.*;

/**

*

* @author Vikram

*/

//DemoBean.java is a Plain Old Java Object class (POJO class for SpringBean)

public class DemoBean implements Demo

{

String msg;

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

@Override

public String generateMsg(String name) {

java.util.Date d = new java.util.Date();

return “Date “+d+” Name”+name;

}

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.hiray;

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

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.core.io.FileSystemResource;

/**

*

* @author Vikram

*/

public class DemoClient {

public static void main(String args[])

{

//FileSystemResource res = new FileSystemResource(“DemoCfg.xml”);

// above statement locate the configuration file

//ApplicationContext context = new ClassPathXmlApplicationContext(“DemoCfg.xml”);

// above statement activate bean container in Spring Framwwork

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoCfg.class);

DemoBean dObj = (DemoBean)context.getBean(“ddObj”,DemoBean.class);

String message = dObj.generateMsg(“Belesh ! Welcome”);

System.out.println(“Our Message is “+message+” and”+dObj.getMsg());

}

}

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package org.hiray;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

*

* @author Vikram

*/

@Configuration

public class DemoCfg {

@Bean(name=“ddObj”)

public DemoBean generateMessage()

{

DemoBean db = new DemoBean();

db.setMsg(“Lalitkumar”);

return db;

}

}

Spring IoC Container

Spring IoC stands for Inversion of Control.

It is heart of Spring Framework.

The important task performed by IoC Container are as follows.

1. Instantiating the Bean.(Object creation of bean class)

2. Wiring the Bean together

3. Configuring the Bean

4. Managing the beans entire life cycle.

The IoC container receives the metadata from either XML files or Java Annotation in the form of Java code and working according to configuration.

In above diagram Spring creates POJO class using Java code and MetaData is in the form of Configuration files (XML or Annotation) the Spring container executes the application

There are two types of IoC container

1. BeanFactory

2. ApplicationContext

1. The BeanFactory is simple container to provides Dependency Injection and defines in org.sparingframework.bean.factory.BeanFactory interface

2. ApplicationContext : it is defines in org.springframework.context.ApplicationContext interface.

The Application context read the Meta Config files as a textual information using annotation convention.