Best Spring Training Center In Chennai Adyar With Placement

What Is Spring ?

Spring is the most popular application development framework for enterprise Java. Millions of developers around the world use Spring Framework to create high performing, easily testable, reusable code.

Spring framework is an open source Java platform and it was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.

The core features of the Spring Framework can be used in developing any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring framework targets to make J2EE development easier to use and promote good programming practice by enabling a POJO-based programming model.

Benefits of Using Spring Framework:

Following is the list of few of the great benefits of using Spring Framework:

  • Spring Training In Chennai it can be enable developers to develop enterprise-class applications using POJOs. The benefit of using only POJOs is that you do not need an EJB container product such as an application server but you have the option of using only a robust servlet container such as Tomcat or some commercial product.
  • Spring is organized in a modular fashion. Even though the number of packages and classes are substantial, you have to worry only about ones you need and ignore the rest.
  • Spring does not reinvent the wheel instead, it truly makes use of some of the existing technologies like several ORM frameworks, logging frameworks, JEE, Quartz and JDK timers, other view technologies.
  • Testing an application written with Spring is simple because environment-dependent code is moved into this framework. Furthermore, by using JavaBean-style POJOs, it becomes easier to use dependency injection for injecting test data.
  • Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over engineered or less popular web frameworks.
  • Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO, for example) into consistent, unchecked exceptions.
  • Lightweight IoC containers tend to be lightweight, especially when compared to EJB containers, for example. This is beneficial for developing and deploying applications on computers with limited memory and CPU resources.
  • Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for example) and scale up to global transactions (using JTA, for example).

Spring Modules

The Spring framework comprises of many modules such as core, beans, context, expression language, AOP, Aspects, Instrumentation, JDBC, ORM, OXM, JMS, Transaction, Web, Servlet, Struts etc. These modules are grouped into Test, Core Container, AOP, Aspects, Instrumentation, Data Access / Integration, Web (MVC / Remoting) as displayed in the following diagram.

Test

This layer provides support of testing with JUnit and TestNG.

Spring Core Container

The Spring Core container contains core, beans, context and expression language (EL) modules.

Core and Beans

These modules provide IOC and Dependency Injection features.

Context

This module supports internationalization (I18N), EJB, JMS, Basic Remoting.

Expression Language

It is an extension to the EL defined in JSP. It provides support to setting and getting property values, method invocation, accessing collections and indexers, named variables, logical and arithmetic operators, retrieval of objects by name etc.

AOP, Aspects and Instrumentation

These modules support aspect oriented programming implementation where you can use Advices, Pointcuts etc. to decouple the code.

The aspects module provides support to integration with AspectJ.

The instrumentation module provides support to class instrumentation and classloader implementations.

Data Access / Integration

This group comprises of JDBC, ORM, OXM, JMS and Transaction modules. These modules basically provide support to interact with the database.

Web

This group comprises of Web, Web-Servlet, Web-Struts and Web-Portlet. These modules provide support to create web application.

Example of spring application in Myeclipse

Creating spring application in myeclipse IDE is simple. You don’t need to be worried about the jar files required for spring application because myeclipse IDE takes care of it. Let’s see the simple steps to create the spring application in myeclipse IDE.

  • create the java project
  • add spring capabilities
  • create the class
  • create the xml file to provide the values
  • create the test class

Steps to create spring application in Myeclipse IDE

Let’s see the 5 steps to create the first spring application using myeclipse IDE.

1) Create the Java Project

Go to File menu — New — project — Java Project. Write the project name e.g. firstspring — Finish. Now the java project is created.

2) Add spring capabilities

Go to Myeclipse menu — Project Capabilities — Add spring capabilities — Finish. Now the spring jar files will be added. For the simple application we need only core library i.e. selected by default.

3) Create Java class

In such case, we are simply creating the Student class have name property. The name of the student will be provided by the xml file. It is just a simple example not the actual use of spring. We will see the actual use in Dependency Injection chapter. To create the java class, Right click on src — New — class — Write the class name e.g. Student — finish. Write the following code:

  1. package com.javatpoint;
  2. public class Student {
  3. private String name;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public void displayInfo(){
  11. System.out.println(“Hello: “+name);
  12. }
  13. }

This is simple bean class, containing only one property name with its getters and setters method. This class contains one extra method named displayInfo() that prints the student name by the hello message.

4) Create the xml file

In case of myeclipse IDE, you don’t need to create the xml file as myeclipse does this for yourselves. Open the applicationContext.xml file, and write the following code:

  1. <?xml version=”1.0" encoding=”UTF-8"?>
  2. <beans
  3. xmlns=”http://www.springframework.org/schema/beans"
  4. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p=”http://www.springframework.org/schema/p"
  6. xsi:schemaLocation=”http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  8. <bean id=”studentbean” class=”com.javatpoint.Student”>
  9. <property name=”name” value=”Vimal Jaiswal”></property>
  10. </bean>
  11. </beans>

The bean element is used to define the bean for the given class. The property subelement of bean specifies the property of the Student class named name. The value specified in the property element will be set in the Student class object by the IOC container.

5) Create the test class

Create the java class e.g. Test. Here we are getting the object of Student class from the IOC container using the getBean() method of BeanFactory. Let’s see the code of test class.

  1. package com.javatpoint;
  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.core.io.ClassPathResource;
  5. import org.springframework.core.io.Resource;
  6. public class Test {
  7. public static void main(String[] args) {
  8. Resource resource=new ClassPathResource(“applicationContext.xml”);
  9. BeanFactory factory=new XmlBeanFactory(resource);
  10. Student student=(Student)factory.getBean(“studentbean”);
  11. student.displayInfo();
  12. }
  13. }

The Resource object represents the information of applicationContext.xml file. The Resource is the interface and theClassPathResource is the implementation class of the Reource interface. The BeanFactory is responsible to return the bean. The XmlBeanFactory is the implementation class of the BeanFactory. There are many methods in the BeanFactory interface. One method is getBean(), which returns the object of the associated class.