What is Spring
Dependency Injection
Compliments JEE
Dependency Injection
class A{ class B{
B b;
doAStuff(){ doBStuff(){
……….
b.doBStuff();
}
}
} }
Inversion Of Control
Spring and JEE
Struts/JSF Integration
UI Layer
OrderController
Spring MVC
security
Services/Business Layer
transactions OrderService
Spring JDBC
Data Access Layer
OrderDAO
Spring ORM
DB
Constructor Injection
<constructor-arg>
As Tag
<value> b </value>
<constructor-arg>
As Attribute
<constructor-arg>
<ref bean=“a”/> C Shema/C Namespace
</constructor-arg>
MIX
Primitive Types
<bean class=“Product”> value as element
<property name=“id”>
<value>10</value> value as attribute
p schema/p namespace
Collection Types
List
Set
Map
Properties
List
<bean>
<property name=“productIds”>
<list>
<value>10 </value>
<value> 20 </value>
<null/>
Set
<bean>
<property name=“productIds”>
<set>
<value>10 </value>
<value> 20 </value>
Map
<bean>
<property name=“productsInOrder”>
<map>
<entry key="100" value="IPhone" />
Properties
<bean>
<property name=“languages”>
<props>
<prop key="USA">English</prop>
Reference Types
HAS - A
A B
B b;
<bean class=“B” name=“b”/>
<bean class=“A” name=“a”>
<property name=“b”>
<ref bean=“b”/>
</property>
</bean>
Life Cycle Methods
public void init()
public void destroy()
Spring Container:
Spring
Bean
(.java) 123
init
Config File read and use the bean
(.xml)
destroy
1) XML Configuration
2)Spring Interfaces
3)Annotations
Dependency Check
Product Connection
id username
name password
dburl
Container
2.x<= XML Configuration
3.0 @Required
Stereotype Annotations
XML <bean…>
@Component
class Instructor
<context:component-scan base-
package=“com.bharath”/>
com
one two
B E
three four five six
C D G
F
@Component
class Instructor
Instructor instructor = new Instructor();
User Defined Types
SpEL Spring Expression Language
Expression @Value
Classes,Variable,Methods,Constructors and Objects
and symbols
char,numerics,operators,keywords and special
symbols which return a value
@value(“#{66+44}”)
@value(“#{5>6?22:33”})
static methods object methods variables
T(class).method(param)
singleton
prototype
@Scope request
session
globalsession
@Value
Primitive Types @Value(“20”)
@Value(“Core Java”)
Collection Types util:Cn id=“myList”
@Value(“#{myList}”)
Object Types @Autowired
Wiring and Auto Wiring
A B A B
Setter or Constructor Setter or Constructor
Programmer Container
Autowiring
XML Annotations
@Autowired
No (Default)
By Type @Qualifier
By Name
Auto Detect
By Constructor
Bean Externalization or Reading Properties
database.properties property place holder configuration
dbName Create the properties
port
Link the properties
userName
password
User properties in xml and inject
Spring JDBC
SQL
Java
JDBC App
DB
Connection con = DriverManager.getConnection….
Statement statement = con.createStatement…
Spring JDBC JDBCTemplate
JDBC + Template
Technology Design Pattern
Template Common Code
Developer loves JDBCTemplate
JDBCTemplate javax.sql.DataSource(I)
DriverManagerDataSource
driverClassName
url
userName
password
JDBCTemplate
update(String sql) int
update(String sql,Object…args) int
insert,update and delete
DriverManagerDataSource dataSource
driverClassName com.mysql.jdbc.Driver
url jdbc:mysql://localhost/mydb
username root
password test
JDBCTemplate jdbcTemplate
dataSource
Create the test class and use the JDBCTemplate
Spring ORM
Obj Primitive Types
SQL
Java
JDBC DB
Mapping:
@Entity
@Table(name=“product”)
class Product{
@Id
@Column
int id;
@Column
String name;
Class
class Employee
ORM
eid ename esal
int empId; M
A
P
String empName; P
I
N
double empSal; G
row
10 bharath 10000
Product
ProductDao HibernateTemplate SessionFactory
ProductDaoImpl LocalSessionFactoryBean
DataSource TransactionManager
AnnotationSessionFactoryBean
dataSource
hibernateProperties
annotatedClasses
ORM Mapping:
XML and Annotations
@Entity
@Table
@Id
@Column
@Entity
@Table
public class Employee{
@Id
@Column(name=“id”)
private int id;
@Column(name=“firstname”)
private int firstName;
@Column(name=“lastName”)
private int lastName;
Spring MVC
Front Controller
Handler Mapper
View Resolver
web.xml
controller 2(b)
2(a)
HandlerMapper ModelAndView
1 Data
Front
Controller
4
Client Dispatcher view model
Servlet
5
7
3
View Resolver
6
prefix view suffix
ViewResolver
prefix view suffix
views/ hello .jsp
Spring MVC Application Creation Steps:
Configure the dispatcher servlet
Create the spring configuration
Configure the View Resolver
Create the controller
Create the folder structure and view
Sending Data
Controller to the UI
UI to the Controller
Controller to the UI
ModelAndView addObject(key,value)
String Object
request.getAttribute(“key”)
Primitive Object Collection
Sending data from UI to Controller:
HTML Form
Query Parameters
Controller
User
123
123
John
John
[email protected] [email protected]
submit
Container:
Reads Converts Creates Object Sets Values
55
id 55
John
email [email protected]
Controller
class User{
submit
@ModelAttribute
int id;
String name;
String email;
}
Usecase
userReg.jsp id 55
55
name John
John
1 john@gmail [email protected]
email
.com
3
submit
2 4
Browser
class User{ Controller
@ModelAttribute
int id;
String name;
String email;
}
Coding Steps
Model
Controller Services DAL
UserController UserService UserDao HibernateTemplate
UserServiceImpl UserDaoImpl
Create the Maven Project
web.xml DispatcherServlet
HibernateTemplate
SessionFactory
dispatcher-servlet.xml
DataSource
ViewResolver
Model
DAL Layer
Code
Services Layer
Controller Layer
AJAX
Synchronous
Browser Server
request
response Application
CLIENT
request
response
Asynchronous
Browser Server
request
request
CLIENT Application
response
request
Browser
Server
JQuery Java Script
${“#userId”}
$.ajax({
url:””
data:{key:value}
success:function(){}
UseCase Steps
Implement the backend validation
Controller Service DAO
Make the AJAX Call
Use JQuery
onChange
Controller
AJAX Call
Handle Response