Unit 2 Java
Unit 2 Java
• Need the use the API the way it is, Cannot modify the API
• Code is public: Same code is exposed to the public
• Internals of the API is not exposed to the developer
(specially web service)
Java Framework
Spring
}
Introduction to Spring Framework
• Five steps Spring Coding
Lightweight Containers
• Spring IOC container
• How Class is constructed?
– Attribute
– methods
• Object Construction
– Classname refname=new Classname()
– Refname.arrtibute1= value
– Refname.arrtibute1= value
– ………….
• Spring
– Core : IOC: Inversion of Control
– No need to create objects
– Objects shall be configured in an XML file by the developer
– Spring container module: responsible to construct java objects by parsing XML
file
Lightweight Containers
• Spring IOC container
• Benefit
• The XML file is not part of source code so it
can grow and shrink
• Values can be manipulated accrodingly
Spring IOC container
• Example
Public void setName(String name)
{ this.name=name;}
public class Employee
{ public String getName()
String name; {return name;}
int id;
double salary; Public void setId(int id)
{ this.id=id;}
public Employee()
{ public String getId()
} {return id;}
public Employee(String name, String id, Public void setSalary(int salary)
double salary) { this.salary=salary;}
{
this.name = name; public Double getSalary()
this.id = id; {return salary;}
this.salary = salary;
} }
Spring IOC container
• Example
public class Myprogram
{
public static void main(String[] args) {
//Object Construction by Developer
Employee e1= new Employee();
e1.setName(“Geeta”);
e1.setId(21);
e1.setSalary(21000.20);
System.out.println(“EmpDetails:”+e1);
//Objects are constructed by spring core container and we are using the
reference to the object
System.out.println(“EmpDetails:”+e2);
}
Spring IOC container
ApplicationContext API is implementation of Bean Package
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Reduce Dependency
Achieve loose coupling
Constructor Injection
Public class Address{ Public class employee{
String city; Int id;
String state; String name;
int pin; Address address;
Public Address(){ Public Employees(){
} }
Public Address(String city, String state, int pin){ // Constructor Injection
This.city=city; Public Employee(Address
This.state=tate; address){
This.pin=pin; This.address=address;
} }
// getter and setter methods // getter and setter methods
} }
Constructor Injection
<?xml version = "1.0" encoding = "UTF-8"?>
➔ Advantage
◆ It requires the less code because we don't need to write the code
to inject the dependency explicitly.
➔ Disadvantage
◆ No control of programmer.
◆ It can't be used for primitive and string values.
Auto Wiring
Auto Wiring
@Autowired
Autowiring Modes XML
Autowiring Modes XML (Example)
Autowiring Modes XML (Example)
package edu.vesit.autowire;
@Override
public String toString() {
return "Employee [address=" + address + "]";
}
}
Autowiring Modes XML (Example)
package edu.vesit.autowire;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
}
Autowiring Modes XML (Example)
Employee [address=Address [street=Bell Pepper, city=Thane]]
Autowiring Modes XML (Example)
<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">
OUTPUT
Employee [address=null]
Autowiring Modes XML (Example)
<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">
OUTPUT
Employee [address=Address [street=Bell Pepper, city=Thane]]
Autowiring Modes XML (Example)
<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">
OUTPUT
Error creating bean with name 'emp1‘
No qualifying bean of type 'edu.vesit.autowire.Address' available: expected single matching bean but found
2: address1,address2
Autowiring Modes XML (Example)
Call byName and byType use setter injection
Autowiring Modes XML (Example)
<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">
<context:annotation-config/>
@Configuration
@ComponentScan("guru.springframework.autowiringdemo")
• https://www.youtube.com/watch?v=rMLP-
NEPgnM
• https://www.thejavaprogrammer.com/sprin
g-hello-world-example/
• https://www.youtube.com/watch?v=TtikLlZs
tkE