Nearby lessons

2 of 35

Spring - System Architecture

📌 What You Will Learn
  • Understand Spring - System Architecture
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring - System Architecture step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Spring Modules

Spring1.x version:

Spring2.x version:

Spring3.x Version:

Spring4.x Version:

Module5.x Version

Core Module

It is fundamental module in Spring Framework; it has provided basic foundation for all

other modules of spring framework.

This module can be used to prepare Standalone Applications directly.

This module is able to provide the features like IOC Containers, Beans, Dependency

Injection.....

AOP Module[Aspect Oriented Programming]:

IN general, if we prepare enterprise applications by using only Object Orientation then we

have to provide both business logic and Services like Transactions, JMS, JAAS,.... in

combined manner, it will provide tightly coupled design, it will provide less sharability and

less reusability.

In the above context, to improve sharability and Reusability we have to provide loosely

coupled design , to get loosely coupled design we have to apply Aspect Oriented

Programming.

In Aspect Oriented Programming, we will declare each and every service as an aspect and

we will inject these aspects in Business Logic at runtime.

JDBC/DAO Modules

The main intention of this module is to interact with database from Spring

application inorder to perform database operations with JDBC Persistance mechanism.

JDBC/DAO modules are able to abstract common JDBC implementation inorder to simplify

Database interaction from spring applications by providing template classes.

EX:

In JDBC, if we want to interact with database then we have to use the following steps.

  • Load And Register Driver

EX: Class.forName("oracle.jdbc.OracleDriver");

  • Establish Connection between Java appl and DB.

EX:

Connction con=DriverManager.getConnection("jdbc: oracle:thin:@localhost:1521:xe",

"system", "durga");

  • Create Statement/PreparedStatement/ CallableStatement as per the requirement.

EX: Statement st=con.createStatement();

  • Write and Execute SQL Queries

EX: ResultSet rs=st.executeQuery("select * from emp1");

  • Close the Connection

EX: con.close();

In the above JDBC Steps, Load and Register Driver, Establish Connection and Create

Statement and close the Connection are vary common in all the JDBC applications, here

Spring JDBC/DAO modules are able to abstract the commonly used steps and giving option

to the developers to provided variable part , that is, Executing Sql queries .

Spring DAO and JDBC modules are having their own Exception Classes hierarchy to expose

the exception details.

Spring DAO/JDBC modules are converting the JDBC generates checked exceptions to Spring

defined Unchecked Exceptions by using Exceptions Re-Throwing Mechanism.

EX:

try{

---Exception-1 ----JDBC Checked Exception

}

catch(Exception_Name e){

----Exception-2---Spring Unchecked Exception

}

ORM Module[Object-Relational Mapping]:

ORM is the mapping between a table, properties or columns and primary key column

with the respective bean component provided Bean class name, ID property, normal

properties,

ORM has define a set of rules and regulations to provide mapping between Object Oriented

Data Model and Relational Data Model in order to achieve data persistency.

EX: Hibernate, JPA, Toplink,....

To prepare Hibernate Application then we have to use the following instructions.

  • Create Configuration class object.

EX:

Configuration cfg=new Configuration();

cfg.configure();

  • Create SessionFactory class object:

EX:

SessionFactory sf=cfg.buildSessionFactory();

  • Create Session object:

EX:

Session s=sf.openSession

  • Perform Persistance operations:

EX:

Object obj=s.load("com.durgasoft.Employee.class", 111);

  • Close SessionFactory and Configuration:

EX:

sf.close();

cfg.close();

In the above Hibernate Application steps, Create Configuration class object, Creation

SessionFactory object , create Session object and close SessionFactory and Configuration

steps are very common in all hibernate applications, in this case, Spring ORM module is

able to abstract all the common instructions in order to simplify Data persistence in

enterprise applications.

Spring ORM module has provided its own Exceptions hierarchy to expose behalf of the

under lying Persistence mechanism like Hibernate, JPA,.... provided checked exceptions.

JAVA EE/Context/Remoting

The main intetion of this module to integrate Spring applications with the

diustributed tech applications like EJBs, RMI,.... it able to get moddleware services like JNDI,

JTA, JAAS,.... from J2EE .

WEB/WEB-MVC Modules:

WEB Module has provided very good environment to integrate other MVC based

framework applications like Struts, JSF, XWork2,.....

WEB-MVC is an MVC implementation provided by Spring framework directly inorder to

prepare web applications.

Test:

Spring Framework has provided its own testing environment to test the enterprise

applications by the developers[Unit Testing] in their own way in the form of Test module.

Note: Spring framework has provided Test module right from its Spring3.x version.

Instrumentation:

Spring Framework has provided Instrumentation API to perform manipulations over

the actions which are generated from the .class files with out having modifications over the

source code.

Messaging:

Spring Framework has provided Messaging module along with its Spring4.x version to

provide Messaging Services what JMS is providing previously as part of J2EE.

Note: When we perform any transactions in bank application , we are able to recieve

mobile updations, in this context, if our mobile device is not ready to recieve messages

WEB Module has provided very good environment to integrate other MVC based framework applications

like Struts, JSF, XWork2,.....

then we have to keep the respective message in Messaging Containier or JMS Server to

send the respective message when the target machine / device is ready.

Steps to prepare First Spring Application

  • Download Spring Framework from Internet.
  • Provide Spring Setup in Eclipse IDE
  • Prepare Bean Class
  • Prepare Bean Configuration File
  • Prepare Test / Client Appl.
  • Download Spring Framework from Internet.

a) Download spring jar files in the form of

"spring-framework-4.1.6.RELEASE-dist.zip" from the URL

"https://repo.spring.io/release/org/springframework/spring/4.1.6.RELEASE/" and

"commons-logging-1.2.jar" from apache website.

b) Unzip "spring-framework-4.1.6.RELEASE-dist.zip" at a particular location in our

machine[E:\softwares\spring]

  • Provide Spring Setup in Eclipse IDE

a) Open Eclipse IDE.

  • Download eclipse-jee-neon-2-win32.zip file from www.eclipse.org website.
  • Extract ZIP file at "C" drive and get "eclipse" folder.
  • Click on "eclipse" icon at "C:\Eclipse" location
  • Provide Workspace "D:\spring3\core\eclipse".
  • Click on "OK" button.

b) Create Java Project

  • Right Click on "Project Explorer" Browser.
  • Select "New"
  • Select "Project".
  • Select "Java Project".
  • Click on "Next" button.
  • Provide project name "app1".
  • Click on "Next" button.
  • Click on "Next" button.
  • Click on "Yes" button.

c) Create User defined Library with all Spring jar files and add that User defined Library

to "Java project".

  • Right Click on Project [app1].
  • Select "Properties".
  • Select "Java Build path".
  • Select "Libraries".
  • Click on "Add Library" button.
  • Select "User library".
  • Click on "Next" button.
  • Click on "User Libraries" button.
  • Click on "New" button.
  • Provide Library Name "Spring4.1.6_Lib_New".
  • Click on "OK" button.
  • Select "Spring4.1.6_Lib_New" and Click on

"Add External Jars" button.

  • Select the required jar files

commons-logging-1.2.jar

spring-beans-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-context-support-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

  • Click on "Open" Button.
  • Click on "OK" button.
  • Click on "Finish" button.
  • Click on "Apply" button and "OK" button.

Note:

Prepare User Defined Library[Spring4.1.6_Lib_New] only one time and use the same

for all Spring applications, not required to prepare for each and every application.

  • Prepare Bean Class

The main intention of Bean classes in Spring applications is to manage properties and

their setXXX() and getXXX() methods and some other Business Methods.

To prepare Bean classes in Spring applications we have to use the following Guidlines.

a) Bean classes must be POJO classes, they must not extend or implement any

predefined Library excepr java.io.Serializable marker interface.

b) Bean must be declared as "public" , "Non-abstract" and "non-final".

---->The main intention of declaring bean class as "public" is to make available bean

class scope to IOC Container inorder to create objects.

---->The main intention to declare bean class as "Non-abstract" is to allow to create

object .

---->The main intention to declare bean classes as "Non-final" is to extend one bean

class to another bean class inorder to improve reusability.

c) In Bean classes, we have to declare all properties as "private" and all behaviours as

"public", it will improve "Encapsulation".

d) If we want to provide any constructor in bean class then provide a constructor , it

must be 0-arg constructor and "public" constructor, because, IOC Container will search

and execute public and 0-arg constructor while instantiating bean.

To create bean class in Eclipse IDE, we have to use the following steps.

  • Right Click on "src" flder.
  • Select "New".
  • Select "Class".
  • Provide package name and class name.

package name: com.durgasoft.beans

class name: HelloBean

  • Click on "Finish" button.
  • As per the requirement provide properties and setXXX() and getXXX() with the

following steps.

a) Declare variables manually.

b) Right Click and Select "Source".

c) Select "Generate Getters and Setters".

d) select "Select All" button.

e) Click on "OK" button.

  • Provide Business Methods as per the requirement.

EX: HelloBean.java

--------------------

package com.durgasoft.beans;

public class HelloBean {

public String sayHello(){

return "Hello User!";

}

}

4 Prepare Bean Configuration File

The main intention of Bean COnfiguration File is to provide all the bean

components configuration details like logical name, fully qualified names of the bean

classes, bean classes properties and bean classes dependencies,...... to the IOCcontainer

inorder to create bean objects and tjheir dependent objects.

Bean Configuration File is an XML file, it will use the following XML tags to configure bean

classes.

<beans ----XSD----- >

-----

<bean id="--" class="---">

-----

</bean>

-----

</beans>

Where "<beans>" tag is a root tag, it will include no of beans configurations.

Where "<bean>" tag can be used to configure single bean class.

Where "id" attribute will take identity name or logical name to the Bean component.

Where "name" attribute will take fully qualified name of the bean class.

To prepare bean confguration file in Eclipse IDE we have to use the following steps.

JAVA EE/Context/Remoting

a) Right Click on "src".

b) Select "New".

c) select "package".

d) Provide package name "com.durgasoft.cfgs".

e) Click on "Finish" button.

Example05
JCode Cell
1 
2Create a package under "src" folder.
3

JAVA EE/Context/Remoting

a) Right Click on "com.durgasoft.cfgs" package.

b) Select "New".

c) Select "Others".

d) Select XML and "XML File".

e) Click on "Next" button.

f) Provide file name "spring_beans_config.xml".

g) Click on "Next" button.

h) Click on "Next" button.

i) Click on "Finish" button.

Example06
JCode Cell
1 
2Create XML file under package.
3

JAVA EE/Context/Remoting

Open bean.html file available at the location

"E:\softwares\spring\spring-framework-4.1.6.RELEASE\docs\spring-framework-

reference\html" copy XSD from any example and past into XML file.

Provide beans configurations as per the requirement.

EX:spring_beans_config.xml

-----------------------------

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

<beans --- XSD ---->

<bean id="hello" class="com.durgasoft.beans.HelloBean"/>

</beans>

  • Prepare Test / Client Appl:

The main intention of Test / Client application is to activate IOC Container , to

create Bean Components and to access business methods.

To prepare Test application in Eclipse IDE we have to use the following steps.

a) Right Click on "SRC".

b) Select "New".

c) Select "Class".

d) Provide the following details.

package name: com.durgasoft.test

Class Name : Test

Select "public static void main(String[] args)

e) Click on "Finish" button.

Provide Application logic in main() method with the following steps.

Example07
JCode Cell
1 
2Provide spring XSD in configuration File.
3

JAVA EE/Context/Remoting

EX: Test.java

package com.durgasoft.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.durgasoft.beans.HelloBean;

public class Test {

public static void main(String[] args)throws Exception {

ApplicationContext context=new ClassPathXmlApplicationContext

("/com/durgasoft/cfgs/spring_beans_config.xml");

HelloBean bean=(HelloBean) context.getBean("hello");

System.out.println(bean.sayHello());

}

}

If we provide properties and their respective

setXXX() and getXXX() methods in bean classes then we have to send values to bean

properties in the following two ways.

Example08
JCode Cell
1 
2Activate ApplicationContext IOC Container.
3Get Bean Object from ApplicationContext.
4Access Business Method.
5

JAVA EE/Context/Remoting

In this approach, we have to set value to bean properties programatically from Test

application.

Example:

HelloBean.java

---------------

package com.durgasoft.beans;

public class HelloBean {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String sayHello() {

return "Hello "+name;

}

}

applicationContext.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 name="helloBean" class="com.durgasoft.beans.HelloBean">

</bean>

</beans>

Test.java

package com.durgasoft.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.durgasoft.beans.HelloBean;

public class Test {

public static void main(String[] args) throws Exception{

ApplicationContext context=new

ClassPathXmlApplicationContext("applicationContext.xml");

HelloBean bean=(HelloBean)context.getBean("helloBean");

bean.setName("Durga");

System.out.println(bean.sayHello());

}

}

OP: Hello Durga!

In the above approach, we have to recompile the Test application when we change

messages, it is not suggestible in application development. To overcome this problem we

have to use beans configuration file to prepare messages inorder to send to Bean object.

  • Beans Configuration File:

To provide messages to the bean properties through their setXXX() methods from bean

configuration file we have to use the following tag in beans configuration file.

<beans ---XSD----- >

----

<bean id="--" class="--">

<property name="--" value="--"/>

----

</bean>

----

</beans>

Where <property> tag is able to represent single bean property.

Where "name" attribute in <property> tag is able to take property name to which we

want to send data.

Where "value" attribute in <property> tag is able to take the value which we want to

send to the bean property.

app2:

-----

HelloBean.java

----------------

package com.durgasoft.beans;

public class HelloBean {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String sayHello() {

return "Hello "+name;

}

}

applicationContext.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 name="helloBean" class="com.durgasoft.beans.HelloBean">

<property name="name" value="Durga"/>

</bean>

</beans>

Test.java

----------

package com.durgasoft.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.durgasoft.beans.HelloBean;

public class Test {

public static void main(String[] args) throws Exception{

ApplicationContext context=new

ClassPathXmlApplicationContext("applicationContext.xml");

HelloBean bean=(HelloBean)context.getBean("helloBean");

System.out.println(bean.sayHello());

}

}

OP: Hello Durga

Steps to prepare Spring Application[Core Module] in Netbeans IDE:

  • Install Netbeans IDE
  • Create Java Project:
  • Add Spring library to Project Library
  • Prepare Packages under "Source Packages"
  • Prepare Bean classes as per the requirement
  • Prepare COnfiguration File
  • Create Test class.
  • Run Test class

1.Installation Process:

  • Download netbeans-8.2-windows.exe file from internet.
  • Double click on netbeans-8.2-windows.exe
  • Click on "Yes" button.
  • Click on "Next" button.
  • Select "Checkbox" like "I Accept the terms in licence aggrement".
  • Click on "Next" button.
  • Change Netbeans installation location from C:\Program Files\NetBeans 8.2

to C:\NetBeans 8.2

  • Click on "Next" button.
  • Change Glassfish server installation location from "C:\Program Files \glassfish-4.1.1"

to "C:\glassfish-4.1.1" .

  • Click on "Next" button.
  • Click on "Install" button.
  • Click on "Finish".
  • Double Click on "Netbeans" IDE icon on our desktop.

2.Create Java Project:

a) Right Click on "Projects" Browser.

b) Select "New Project".

c) Select "Java" under "Catagories".

d) Select "Java Application" under projects.

e) Click on "Next" button.

f) Change application[app21]

g) Change Project Location[D:\spring3\core\netbeans].

h) Provide Main class Name[com.durgasoft.test.Test].

i) Click on "Finish" button.

  • Add Spring library to Project Library:

a) Right Click on "Libraries" folder in Project.

b) Select "Add Library".

c) Select "Spring Framewwork 4.0.1" Library.

d) Click on "Add Library" button.

4.Prepare Packages under "Source Packages":

a) Right Click on "Source Package".

b) Select "New".

c) Select "Java Package".

d) Provide package Name[com.durgasoft.beans].

e) Click on "Finish" button.

Note: Similarily prepare packagfe com.durgasoft.cfgs and com.durgasoft.test.

  • Prepare Bean classes as per the requirement.

a) Right Click on "com.durgasoft.beans" package.

b) Select "New"

c) Select "Java Class".

d) Provide class Name[User].

e) Click on "Fininsh".

f) In the generated class declare variables.

g) Generate setXXX() and getXXX() methods .

Example09
JCode Cell
1 
2From Test Program
3From Beans configuration File.
4From Test Program
5

JAVA EE/Context/Remoting

h) Provide other methods also as per the requirement.

User.java

package com.durgasoft.beans;

public class User {

private String uname;

private String uqual;

private String uage;

private String uaddr;

private String uemail;

private String umobile;

setXXX()

getXXX()

public void display_User_Details(){

System.out.println("User Details");

System.out.println("---------------------");

System.out.println("User Name :"+uname);

System.out.println("User Qualification :"+uqual);

System.out.println("User Age:"+uage);

System.out.println("User Address :"+uaddr);

System.out.println("User Email :"+uemail);

System.out.println("User Mobile :"+umobile);

}

}

Example10
JCode Cell
1 
2Right Click .
3Select "INsert Code".
4Select "Getters and Setters".
5Select "User" check box.
6Click on "Generate".
7
📝 Key Takeaways
  • Key ideas of Spring - System Architecture explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end