0% found this document useful (0 votes)
35 views12 pages

Spring Introduction-1

This document provides an overview of the Spring framework and discusses some key concepts like Inversion of Control (IOC). It describes common Spring modules like Spring MVC, Spring Security, and Spring Transaction. It also includes examples of Java classes configured as Spring beans and an XML configuration file defining bean dependencies.

Uploaded by

Ramesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
35 views12 pages

Spring Introduction-1

This document provides an overview of the Spring framework and discusses some key concepts like Inversion of Control (IOC). It describes common Spring modules like Spring MVC, Spring Security, and Spring Transaction. It also includes examples of Java classes configured as Spring beans and an XML configuration file defining bean dependencies.

Uploaded by

Ramesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 12

Spring Introduction

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

* Spring is a powerful lightweight application development framework used for Java


Enterprise Edition (JEE)

* You can say that the Spring Framework is a comprehensive tool for supporting
applications using Java programming language.

* Spring Provides various Modules to support the Application Development on Java


Plateform.

* Spring Provides Bello Modules:-

1. Spring IOC (Inversion Of Control).

2. Spring AOP (Aspect Oriented Progrmaing).

3. Spring MVC (Model View Controller).

4. Spring Security.

5. Spring Transaction.

6. Spring Batch and other several modules.

* Spring is a framework and using Java Servlet with core Technology concept it is
designed.

-----------------------------------------------------------------------------------
----------------
Q. What is difference between web application and System application/Stand alone
application?

Ans:-
System Application:-

The application which is designed for System Level as well to access these
application we not need for Network as well as
any web browser.

Example:- Notepad, VLC Player ,Microdoft Office.

* The application which is accessed via web browser then it is called Web
application.

* Almost all the apps are comes under web applications.

* Now, What is difference between Framework and Technology?

Ans:-

Example:-
* Technology:-
* Technology is a specification given by an organization and it will provide
a functionality.

* Java is a Computer languase and it is well known Technology for the


application Development in the Software Industries.

* Java is written using various Computer/Programing languase such as


C,C++ and other languases also.

* Frameworks are a subset of technologies. All computer languages are


technologies. All frameworks are written in computer languages to solve specific
problems.

* Framework:-

* Frameworks provides more convinced way to use technologies and provides


new API on top of technology API.

* Framework is a set of Modules using Technology/Computer Languages to


solve the specific scenarios or problem in application Development.

* Spring is a framework which uses java Technology to solve the various


problem such as Bean Injection,Aspects of Business Logics,Uses of Model
view Controller Transaction as well as Securities etc.

* Jdbc is a Technology which has JDBC API.


MySql.jar,ojbc14.jar,ojdbc6.jar

* Where as Hibernate is a framework , which is designed on the top of jdbc API.

* Spring is a Framework which is designed on the top of Java Technology API.

Spring IOC (Inversion Of Control)


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

* Inversion Of Control is a core concept of Spring Framework.

* IOC is well known as Inversion of Control.

* Control of Injection/Initialization for an object is Transfered from


Developer/user to the Spring Container is known as Inversion of Control.

Example :- Before Spring Bellow control was there in Developers/Users hand and
they are fully responsible to create,process and close the object after use.

//Creating the object and setting the properties values.

Student st=new Student();


st.setSid(101);
st.setSname("Ajay");
st.setCourse("B.Tech");

// Process the object.


if(st.getSid()<101){

System.out.println("This is old Student");

}else{
System.out.println("This is new Student");
}

//Close the object after use.

st.close();

* Using Spring , Spring Containers are responsible to Inject/Initialize the object


and close it after use.

* So Now the control is transfered from Developers to Spring Container to create


the object,Process the object and close the object.

* Since Control is transfered Hence This Process/Mechanism is known as Inversion of


Control.

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

* What is a Java Bean?

Ans:- To Know the exact answer of this question let's Know the Java class.

class Employee{

OR

class Employee implements Serializable{

private int id;

private String name;

private double salary;

default Constructors

setter/getter methods

Now A JavaBean is a Java object that satisfies certain programming conventions:


* The JavaBean class must implement either Serializable or Externalizable.

* The JavaBean class must have a no-arg constructor.

* All JavaBean properties must have public setter and getter methods.

* All JavaBean instance variables should be private.

class Hello{

class Stdent{

Example:-

@Entity
public class Employee implements Serializable{

@Id
private int id;
private String name;
private int salary;

public Employee() {}

public Employee(String name, int salary) {


this.name = name;
this.salary = salary;
}
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;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}

* What is a Spring Bean?

Ans:-
* if you written any class in spring franework that is called Spring Bean.

Spring Lab1:-
===========================================

* Prepare the Work space for Spring IOC program.

* go to the file->new->Project->Maven->Maven Project->click on Next button.

* Now check the options for below two:-


A.

A.java
==========================================
public class A {

private int a;

private String msg;

public A() {

public void setA(int a) {

System.out.println("A--setA()");
this.a=a;
}

public void setMsg(String msg) {

System.out.println("A---setMsg()");
this.msg=msg;
}

public void show() {


System.out.println("A---show()");
System.out.println(a);
System.out.println(msg);
}

}
=================================================

B.java
=================================================
public class B {

private int b;

private String str;

public B(int b,String str) {


System.out.println("A---B()--cons");
this.b=b;
this.str=str;
}

public void show() {


System.out.println(b);
System.out.println(str);
}

=============================================
Hello.java
==============================================
package com.example.springiocexamples;

public class Hello {

private A aobj; //Dependency of Hello bean on A.

private B bobj; //Dependency of Hello bean on B.

//if Initialization is take place by setter method then it is called setter


Injection.
public void setAobj(A aobj) {
System.out.println("Hello---setAobj()");
this.aobj=aobj;
}

//if Initialization takes place from Constructor ,then it is called


Constructor Injection.
public Hello(B bobj) {
System.out.println("Hello()---cons");
this.bobj=bobj;
}

public void show() {


aobj.showA();
bobj.showB();
}

================================
applicationContext.xml
========================================

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<bean id="aobj" class="com.example.springiocexamples.A">


<property name="a" value="99"/>
<property name="msg" value="This is msg"/>
</bean>

<bean id="bobj" class="com.example.springiocexamples.B">


<constructor-arg name="b" value="100"/>
<constructor-arg name="str" value="This is str"/>
</bean>

<bean id="hello" class="com.example.springiocexamples.Hello">


<property name="aobj" ref="aobj"/>
<constructor-arg ref="bobj"/>
</bean>
</beans>

Lab1.java
========================================

package com.example.springiocexamples;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Lab1 {


public static void main(String[] args) {
//Without IOC

A aobj=new A(); aobj.setA(99); aobj.setMsg("This is Msg");

B bobj=new B(88,"This is Str");

Hello hello=new Hello(bobj); hello.setAobj(aobj);


hello.show();
System.out.println("==========================");

//Using IOC

ApplicationContext context=new
ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Container is Ready-------------------"); Hello
h=(Hello)context.getBean("hello"); h.show();
System.out.println("Container is shutdown");

}
}

pom.xml
=================================================

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.springIocExample</groupId>
<artifactId>MySpringExamples</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>4.3.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>

* Right Click on project.


* run as
* maven build...
* goal----> clean install
* skip test
* click on run.

======================================================Date:- 26th May


2022===========================================================

Q.) What is Spring IOC?

Ans:-
* once Developer was responsible to create the object of any class

Student st=new Student();

* Stuff the properties values using constructor or setter methods.

st.setSid(101);
st.setSname("Ajay");
st.setCity("Bangalore");

* Process or use the object.

if(st.getSname().equals("Ajay")){
st.setFee(25000);
totalAmount-25000;
System.out.println("Dues Balance"+st.getDuesBalance());

* Destroy/close the object after use.

st.close();
st.destroy();

Now in Spring the object Creation, stuffing the properties values,process the
object life cycle and finally close/destroy the objecrt

these all responsibility transfered to Spring Container.Hence due to control


transfer from developer to container this mechanism is called Spring IOC i.e
Inversion of Control.

Q.) What is Dependency Injection in Spring?

Ans:-

Dependency Injection is nothing but the Initialization of Spring Bean/object


inside the other class object/bean.

class A{

private int a;

private String msg;

A(){

public void setA(int a){

this.a=a;
}

public void setMsg(String msg){

this.msg=msg;

public void showA(){


SOP(a);
SOP(b);
}

class B{
private int b;

private String str;

B(int b,String str){


this.b=b;
this.str=str;

public void showB(){


SOP(b);
SOP(str);

in bellow class there are two dependencies which are aobj and bobj respectively.

* aobj is a dependency of A type in TestDependencyInjection.

* bobj is a dependency of B type in TestDependencyInjection.

* Dependency Injection are two types.

1. Setter Injection.

Ans:- once the Dependencies are Initialized/Inject by setter method then this is
called setter Injection.

public void setAobj(A aobj){

this.aobj=aobj;

2. Constructor Injection.
//Constructor Injection
TestDependencyInjection(B bobj,int ab){
this.bobj=bobj;
this.ab=ab;
}

Ans:-
once Dependencies are Initialize/Inject By Constructor then this is called
Constructor Injection.

class TestDependencyInjection{

private A aobj; //Dependency

private B bobj; // Dependency

private int ab; // Dependency

//setter Injection
public void setAobj(A aobj){

this.aobj=aobj;

//Constructor Injection
TestDependencyInjection(B bobj,int ab){
this.bobj=bobj;
this.ab=ab;
}

public void display(){

aobj.showA();
bobj.showB();

====================Steps of Injection for above


class=================================
Step 1:-
A aobj=new A();

aobj.setA(10);
aobj.setMsg("This is Dependency Injection");

Step 2:-

B bobj=new B(30,This is B dependency");


// bobj.setB(30);
//bobj.setStr("This is B dependency");

Step 3:-

TestDependencyInjection injection=new TestDependencyInjection(bobj,45);


injection.setAobj(aobj);
//injection.setBobj(bobj); //compilation problem due to setter is not
available for bobj.

* Now Fully Injected bean of TestDependencyInjection i.e injection is available for


use.

applicationContext.xml:-

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


<property name="a" value="10"/>
<property name="msg" value="This is Dependency Injection"/>
</bean>

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


<Constructor-arg name="str" value="This is B dependency">
<constructor-arg name="ab" value="80"/>
</bean>

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


<property name="aobj" ref="aobj"/>
<constructor-arg name="bobj" ref="bobj"/>
<constructor-arg name="ab" value="30"/>
</bean>

ApplicationContext context=new
ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Container is Ready-------------------");

TestDependencyInjection
ij=(TestDependencyInjection)context.getBean(injection);
ij.display();
System.out.println("Container shutdown");

You might also like