Spring Slides
Spring Slides
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
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;
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
C D G
F
@Component
class Instructor
Expression @Value
and symbols
prototype
@Scope request
session
globalsession
@Value
Primitive Types @Value(“20”)
@Value(“Core Java”)
A B A B
Programmer Container
Autowiring
XML Annotations
@Autowired
No (Default)
By Type @Qualifier
By Name
Auto Detect
By Constructor
Bean Externalization or Reading Properties
password
User properties in xml and inject
Spring JDBC
SQL
Java
JDBC App
DB
DriverManagerDataSource
driverClassName
url
userName
password
JDBCTemplate
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
@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
ProductDaoImpl LocalSessionFactoryBean
DataSource TransactionManager
AnnotationSessionFactoryBean
dataSource
hibernateProperties
annotatedClasses
ORM Mapping:
@Table
@Id
@Column
@Entity
@Table
@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
5
7
3
View Resolver
6
prefix view suffix
ViewResolver
Controller to the UI
UI to the Controller
Controller to the UI
ModelAndView addObject(key,value)
String Object
request.getAttribute(“key”)
HTML Form
Query Parameters
Controller
User
123
123
John
John
[email protected] [email protected]
submit
Container:
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
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
Use JQuery
onChange
Controller
AJAX Call
Handle Response