Spring Module (1)
Spring Module (1)
___________________________________
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.
C. object dependency
___________________________________
___________________________________________
__________________________________________
1. Able develop any kind of application like as console or web or enterprise etc
2. Provide dependency injection and inversion of control.
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
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.
etc
__________________________________________
Steps to develop the spring core application
____________________________________________
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.
<dependencies>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
</dependencies>
package org.techhub;
return id;
}
public void setId(int id) {
this.id = id;
return name;
this.name = name;
2. By using Annotations.
So we want to configure bean using XML file
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
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
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.
"https://www.springframework.org/dtd/spring-beans-2.0.dtd">
"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>
</beans>
Tag Description
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.
if we want to call XML file from classpath we have Resource interface and we
have to use implementer class name as ClassPathResource
Sample code
package org.techhub;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
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
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
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;
______________________________________________________
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.
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;
Employee emp=(Employee)bf.getBean("e");
emp.show();
___________________________________________________________
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 Test() {
System.out.println("I am constructor");
test.xml
"https://www.springframework.org/dtd/spring-beans-
2.0.dtd">
<beans>
</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;
_____________________________________________________________
</bean>
Note: if we think about index parameter then using index parameter we can
decide the parameter number and index start from 0th location.
test.xml
"https://www.springframework.org/dtd/spring-beans-
2.0.dtd">
<beans>
</bean>
</beans>
Test.java
package org.techhub;
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;
Collection Dependency
_______________________________________________
<property name=”propertyname”>
<collectionname>
<value>data</value>
</collectionname>
</property>
</bean>
test.xml
"https://www.springframework.org/dtd/spring-beans-
2.0.dtd">
<beans>
<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.*;
this.pList=list;
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;
Team t=(Team)bf.getBean("tm");
t.showList();
Map dependencies
_____________________________________________________
test.xml
<?xml version="1.0" encoding="UTF-8"?>
"https://www.springframework.org/dtd/spring-beans-
2.0.dtd">
<beans>
</bean>
<property name="playerNames">
<map>
</map>
</property>
</bean>
</beans>
Team.java
package org.techhub;
import java.util.*;
this.playerNames=playerNames;
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 {
Team t=(Team)bf.getBean("tm");
t.showList();