0% found this document useful (0 votes)
8 views

Spring Module (1)

Uploaded by

Pawan Chavan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Spring Module (1)

Uploaded by

Pawan Chavan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 25

Spring Module

___________________________________

Q. What is meaning of spring?

Spring is a framework of framework which is used for develop the any kind of
application like using spring we can develop console base application , database
application as well as web application as well as enterprise level application.

Note: before learn spring framework we need to know dependency injection


concept.

Q. What is dependency injection?

If particular method or constructor is dependent on specified type of parameter


called as dependency injection

Types of dependency Injection

A. setter injection: setter injection means if setter method is dependent on


parameter called as setter injection.
B. constructor injection: constructor injection means if we parameterized
constructor and if constructor calling is dependent on some specified parameter
called as constructor injection.

C. object dependency

___________________________________

if particular class method or constructor or object is dependent on another class


object called as dependency

D. collection dependency: if particular method or constructor is dependent on


collection called as collection dependency
E. map dependency

___________________________________________

Map dependency means if particular method or constructor or class is


dependent on map as parameter called as Map dependency

F. property dependency : if we pass Properties class as parameter in method or


in constructor called as Property dependency

Q. Why learn spring or why use spring?

__________________________________________

1. Able develop any kind of application like as console or web or enterprise etc
2. Provide dependency injection and inversion of control.

3. Provide loose coupling

4. provide multiple sub modules which help to work with different kind of
application and provide security framework known as spring security

5. easy to test

etc

Spring provide multiple module to us

1. Spring core: spring core is root module spring means this module contain all
spring core concept like as manage dependency injection ,inversion of control etc
and using this module we can design console base application.

2. Spring DAO or JDBC: using this module of spring we can connect our java
application with database using jdbc concept.

3. Spring MVC: this module helps us develop web application using spring
framework using MVC Approach

4. Spring ORM: this module helps us to connect our java application with
database application using ORM concept.

5. Spring Security: provide security constrain to java application.

6. Spring AOP: Aspect oriented programming approach.

etc

How to Create Spring core application

__________________________________________
Steps to develop the spring core application

1. Create Maven project using eclipse

____________________________________________

file – new --- other --- maven --- maven project -- click on next – select create
simple project (skip archtetype selection) --- click on next button --- give group
id(group id known as package name) and artifact id --- known as project name
and click on next and finish button.

Once we create maven project we get following folder structure by spring


framework.

2. Add following maven dependencies

If we want to work with spring core application we required to add following


maven dependencies

A. spring core maven dependency

B. spring context maven dependency

<dependencies>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->


<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>5.3.20</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.3.20</version>

</dependency>

</dependencies>

3. Create POJO class

package org.techhub;

public class Employee {

private int id;

public int getId() {

return id;

}
public void setId(int id) {

this.id = id;

private String name;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

4. Configure POJO class as bean class

If we want to configure bean class using spring framework two ways

1. By using XML file

2. By using Annotations.
So we want to configure bean using XML file

Steps to configure using XML


__________________________________________

a. create xml file under src/main/resources folder

right click on src/main/resources --- select new ---- xml -- xml file--- click on
next and give filename.xml and click on finish

Once we create bean xml file you can write DOCTYPE in XML for configure bean

Q. what is doc type & why use it?

doc type stands document type basically XML language help us to design custom
tag or own tags ad if we want to reuse the custom tag for that we can create doc
type and we can reuse it

Means if we think about spring framework so spring framework provide some


inbuilt tag to us for configure java class as bean in XML file and we want to reuse
the tags design by spring for that purpose we required to use doc type design by
spring core library here
Q. How to write doc type for bean configuration?

open maven dependencies folder --- open spring-beans.jar --- open package
org.springframework.beans.factory.xml ---open spring-beans.dtd file ---copy
following doctype from comment section of file and paste in top section of xml
file.

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"

"https://www.springframework.org/dtd/spring-beans-2.0.dtd">

Example test.xml file

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"

"https://www.springframework.org/dtd/spring-beans-2.0.dtd">

once we copy the doc type you can use the spring bean tags for configure bean
class

<beans>

<bean id=”ref” class=”qualified class name”>

<property name=”variablename” value=”value”/>

</bean>

</beans>

Tag Description

<bean id=”ref” class=”qualified class name”>:


here <bean> tag indicate the class id attribute means reference of bean class and
class attribute means we need to specify class name whose object want to create

<property name=”variablename” value=”value”/> :

here property indicate the set keyword here name attribute indicate variable
name means we use set keyword with variable name and value attribute indicate
parameter value to setter method.

5. Write client application and perform dependency injection and inversion of


control operations

Steps to write client application

1. Write class with main method

2. call XML file in class

if we want to call XML file from classpath we have Resource interface and we
have to use implementer class name as ClassPathResource

Syntax: Resource ref = new ClassPathResource(String path);

Sample code

package org.techhub;
import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

public class ClientApplication {

public static void main(String[] args) {

Resource r= new ClassPathResource("test.xml");

3. Create Reference of spring container

Q. What is spring container?

Spring container is inbuilt API which is used for manage the overall activity of
spring framework or spring bean in spring core like as creating instance of bean
class or managing dependency injection etc

There are two types of spring container

a. BeanFactory

b. ApplicationContext.

But now we want to work with bean Factory and if we want to create reference of
BeanFactory we have to use XmlBeanFactory class

Syntax: BeanFactory ref=new XmlBeanFactory(Resource);

package org.techhub;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

public class ClientApplication {

public static void main(String[] args) {

Resource r= new ClassPathResource("test.xml");

BeanFactory bf = new XmlBeanFactory(r);

4. call getBean() method for create bean instance

______________________________________________________

getBean() method help us to access the bean id and create instance of bean
class as well as call the setter method at the time of instance creation means
getBean() method is internally perform inversion control process and manage
dependency injection.

Syntax : Object ref=container.getBean(“beanid”);

Example with source code

package org.techhub;

import org.springframework.beans.factory.BeanFactory;

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

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClientApplication {

public static void main(String[] args) {

Resource r= new ClassPathResource("test.xml");

BeanFactory bf = new XmlBeanFactory(r);

Employee emp=(Employee)bf.getBean("e");

emp.show();

Constructor Injection or Constructor Dependency

___________________________________________________________

Constructor injection means if we pass parameter to constructor by using spring


framework called as constructor injection.

Note: when we have default constructor in class then we not required to write
constructor injection or constructor dependency spring container automatically
call constructor when we create object of class shown in following code.
Example with source code

Test.java

package org.techhub;

public class Test {

public Test() {

System.out.println("I am constructor");

test.xml

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"

"https://www.springframework.org/dtd/spring-beans-
2.0.dtd">

<beans>

<bean id="t" class="org.techhub.Test">


</bean>

</beans>

ClientApplication

package org.techhub;

import org.springframework.beans.factory.BeanFactory;

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

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

public class ClientApplication {

public static void main(String[] args) {

Resource r = new ClassPathResource("test.xml");

BeanFactory bf = new XmlBeanFactory(r);

Test t1=(Test)bf.getBean("t");//new Test();

Following diagram shows the constructor injection


Note: if we have parameterized constructor in your class then we must be
manage the constructor injection technique.

How to manage the constructor injection

_____________________________________________________________

if we want to manage the constructor injection we have to use following tags

<bean id=”idname” class=”classname”>

<constructor-arg value=”parameter value” type=”datatype”


index=”paramindex”/>

</bean>

Note: if we think about index parameter then using index parameter we can
decide the parameter number and index start from 0th location.

Example with source code

test.xml

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"

"https://www.springframework.org/dtd/spring-beans-
2.0.dtd">
<beans>

<bean id="t" class="org.techhub.Test">

<constructor-arg value="1" type="int" index="0"></constructor-arg>

<constructor-arg value="ABC" type="java.lang.String"


index="1"></constructor-arg>

<constructor-arg value="10000" type="int" index="2"></constructor-arg>

</bean>

</beans>

Test.java

package org.techhub;

public class Test {

public Test(int id,String name,int sal) {

System.out.println("Id is "+id);

System.out.println("Name is "+name);

System.out.println("Salary is "+sal);

ClientApplication.java

package org.techhub;

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

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

public class ClientApplication {

public static void main(String[] args) {

Resource r = new ClassPathResource("test.xml");

BeanFactory bf = new XmlBeanFactory(r);

Test t1=(Test)bf.getBean("t");//new Test();

Collection Dependency

_______________________________________________

Collection dependency means pass collection as parameter to method or


constructor using spring container called as collection dependency

Syntax of collection dependency using setter injection

<bean id=”idname” class=”classname”>

<property name=”propertyname”>

<collectionname>

<value>data</value>

</collectionname>

</property>
</bean>

Example with diagram

Example with source code

test.xml

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"

"https://www.springframework.org/dtd/spring-beans-
2.0.dtd">

<beans>

<bean id="tm" class="org.techhub.Team">

<property name="playerNames">

<list>

<value>ABC</value>

<value>MNO</value>

<value>PQR</value>
</list>

</property>

</bean>

</beans>

Team.java

package org.techhub;

import java.util.*;

public class Team {

private List <String> pList;

public void setPlayerNames(List<String> list) {

this.pList=list;

public void showList() {

pList.forEach((s)->System.out.println(s));

ClientApplication.java

package org.techhub;

import org.springframework.beans.factory.BeanFactory;

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

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ClientApplication {

public static void main(String[] args) {

Resource r = new ClassPathResource("test.xml");

BeanFactory bf = new XmlBeanFactory(r);

Team t=(Team)bf.getBean("tm");

t.showList();

Map dependencies

_____________________________________________________

Map as parameter in function or in constructor called as map dependency

Example with source code

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"

"https://www.springframework.org/dtd/spring-beans-
2.0.dtd">

<beans>

<bean id="t" class="org.techhub.Test">

<constructor-arg value="1" type="int" index="0"></constructor-arg>

<constructor-arg value="ABC" type="java.lang.String"


index="1"></constructor-arg>

<constructor-arg value="10000" type="int" index="2"></constructor-arg>

</bean>

<bean id="tm" class="org.techhub.Team">

<property name="playerNames">

<map>

<entry key="1" value="ABC"></entry>

<entry key="2" value="MNO"></entry>

<entry key="3" value="PQR"></entry>

</map>

</property>

</bean>

</beans>
Team.java

package org.techhub;

import java.util.*;

public class Team {

private Map<Integer,String> playerNames;

public void setPlayerNames(Map<Integer,String> playerNames) {

this.playerNames=playerNames;

public void showList() {

Set<Map.Entry<Integer, String>> s=playerNames.entrySet();

for(Map.Entry<Integer, String> sd:s) {

System.out.println(sd.getKey()+"\t"+sd.getValue());

ClientApplication.java

package org.techhub;

import org.springframework.beans.factory.BeanFactory;

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

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;
public class ClientApplication {

public static void main(String[] args) {

Resource r = new ClassPathResource("test.xml");

BeanFactory bf = new XmlBeanFactory(r);

Team t=(Team)bf.getBean("tm");

t.showList();

You might also like