Spring Introduction-1
Spring Introduction-1
====================================================
* You can say that the Spring Framework is a comprehensive tool for supporting
applications using Java programming language.
4. Spring Security.
5. Spring Transaction.
* 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.
* The application which is accessed via web browser then it is called Web
application.
Ans:-
Example:-
* Technology:-
* Technology is a specification given by an organization and it will provide
a functionality.
* Framework:-
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.
}else{
System.out.println("This is new Student");
}
st.close();
-------------------------------------------------------
Ans:- To Know the exact answer of this question let's Know the Java class.
class Employee{
OR
default Constructors
setter/getter methods
* All JavaBean properties must have public setter and getter methods.
class Hello{
class Stdent{
Example:-
@Entity
public class Employee implements Serializable{
@Id
private int id;
private String name;
private int salary;
public Employee() {}
Ans:-
* if you written any class in spring franework that is called Spring Bean.
Spring Lab1:-
===========================================
A.java
==========================================
public class A {
private int a;
public A() {
System.out.println("A--setA()");
this.a=a;
}
System.out.println("A---setMsg()");
this.msg=msg;
}
}
=================================================
B.java
=================================================
public class B {
private int b;
=============================================
Hello.java
==============================================
package com.example.springiocexamples;
================================
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">
Lab1.java
========================================
package com.example.springiocexamples;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//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>
Ans:-
* once Developer was responsible to create the object of any class
st.setSid(101);
st.setSname("Ajay");
st.setCity("Bangalore");
if(st.getSname().equals("Ajay")){
st.setFee(25000);
totalAmount-25000;
System.out.println("Dues Balance"+st.getDuesBalance());
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
Ans:-
class A{
private int a;
A(){
this.a=a;
}
this.msg=msg;
class B{
private int b;
in bellow class there are two dependencies which are aobj and bobj respectively.
1. Setter Injection.
Ans:- once the Dependencies are Initialized/Inject by setter method then this is
called setter Injection.
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{
//setter Injection
public void setAobj(A aobj){
this.aobj=aobj;
//Constructor Injection
TestDependencyInjection(B bobj,int ab){
this.bobj=bobj;
this.ab=ab;
}
aobj.showA();
bobj.showB();
aobj.setA(10);
aobj.setMsg("This is Dependency Injection");
Step 2:-
Step 3:-
applicationContext.xml:-
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");