SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
Spring Training
TechFerry Infotech Pvt. Ltd.
(http://www.techferry.com/)
Conversations
o Introduction to Spring
o Concepts: Annotations, MVC, IOC/DI, Auto wiring
o Spring Bean/Resource Management
o Spring MVC, Form Validations.
o Unit Testing
o Spring Security – Users, Roles, Permissions.
o Code Demo
 CRUD using Spring, Hibernate, MySQL.
 Spring security example.
 REST/jQuery/Ajax example
Spring - Introduction
Exercise: What do we need in an enterprise application?
• Database Access, Connection Pools?
• Transactions?
• Security, Authentication, Authorization?
• Business Logic Objects?
• Workflow/Screen Flow?
• Messaging/emails?
• Service Bus?
• Concurrency/Scalability?
Can somebody wire all the needed components?
Do we have to learn everything before we can start?
Hello Spring
• Spring is potentially a one-stop shop, addressing most infrastructure
concerns of typical web applications
o so you focus only on your business logic.
• Spring is both comprehensive and modular
o use just about any part of it in isolation, yet its architecture is
internally consistent.
o maximum value from your learning curve.
What is Spring?
• Open source and lightweight web-application framework
• Framework for wiring the entire application
• Collection of many different components
• Reduces code and speeds up development
Spring is essentially a technology dedicated to enabling you to build
applications using POJOs.
Why Spring?
• Spring Enables POJO Programming
o Application code does not depend on spring API’s
• Dependency Injection and Inversion of Control simplifies coding
o Promotes decoupling and re-usability
Features:
• Lightweight
• Inversion of Control (IoC)
• Aspect oriented (AOP)
• MVC Framework
• Transaction Management
• JDBC
• Ibatis / Hibernate
Spring Modules
What else Spring do?
Spring Web Flow
Spring Integration
Spring Web-Services
Spring MVC
Spring Security
Spring Batch
Spring Social
Spring Mobile
... and let it ever expand ...
Inversion of Control/Dependency Injection
"Don't call me, I'll call you."
• IoC moves the responsibility for making things happen into the
framework
• Eliminates lookup code from within the application
• Loose coupling, minimum effort and least intrusive mechanism
IOC/DI
IOC/DI
Non IOC Example:
class MovieLister...
private MovieFinder finder;
public MovieLister() {
finder = new MovieFinderImpl();
}
public interface MovieFinder {
List findAll();
}
class MovieFinderImpl ... {
public List findAll() {
...
}
}
IOC/DI
IoC Example: DI exists in major two variants:
Setter Injection
public class MovieLister {
private MovieFinder movieFinder;
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
Constructor Injection
public class MovieLister{
private MovieFinder movieFinder;
public MovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
Code Demo ....
• Annotations: @Component, @Service, @Repository
• Annotation: @Autowire
• web.xml - Context loader listener to scan components
• <context:annotation-config />
<context:component-scan base-package="..." />
Spring Bean Management
Bean Scopes
singleton
Scopes a single bean definition to a single object instance per Spring
IoC container.
prototype
Scopes a single bean definition to any number of object instances.
request
Scopes a single bean definition to the lifecycle of a single HTTP
request.
session
Scopes a single bean definition to the lifecycle of a HTTP Session.
global session
Scopes a single bean definition to the lifecycle of a global HTTP
Session. Typically only valid when used in a portlet context.
Singleton Bean
Prototype Beans
• Use @Scope("prototype")
• Caution: dependencies are resolved at instantiation time. It
does NOT create a new instance at runtime more than once.
Bean Scopes Contd..
• As a rule of thumb, you should use the prototype scope for all
beans that are stateful, while the singleton scope should be used for
stateless beans.
• RequestContextListener is needed in web.xml for request/session
scopes.
• Annotation:@Scope("request") @Scope("prototype")
Homework:
• Singleton bean referring a prototype/request bean?
• @Qualifier, Method Injection.
Hate Homework?
• Stick to stateless beans. :)
Wiring Beans
no
No autowiring at all. Bean references must be defined via a ref
element. This is the default.
byName
Autowiring by property name.
byType
Allows a property to be autowired if there is exactly one bean of the
property type in the container. If there is more than one, a fatal
exception is thrown.
constructor
This is analogous to byType, but applies to constructor arguments.
autodetect
Chooses constructor or byType through introspection of the bean
class.
Homework :)
– What wiring method is used with @Autowire annotation?
– Other annotations you may find useful:
o @Required
o @Resource
Also review the Spring annotation article:
http://www.techferry.com/articles/spring-annotations.html
MVC - Model View Controller
• Better organization and code reuse.
• Separation of Concern
• Can support multiple views
Spring MVC
Code Demo ....
• Annotations: @Controller, @RequestMapping, @ModelAttribute,
@PathVariable
• Spring DispatcherServlet config - just scan controllers
• web.xml - Context loader listener to scan other components
• ResourceBundleMessageSource and <spring:message> tag
Reference: http://static.springsource.org/spring/docs/3.0.x/spring-
framework-reference/html/mvc.html
• @RequestMapping Details
• Handler method arguments and Return Types
Pre-populate Model and Session Objects
@Controller
@RequestMapping("/owners/{ownerId}/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetForm {
@ModelAttribute("types")
public Collection<PetType> populatePetTypes() {
return this.clinic.getPetTypes();
}
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result,
SessionStatus status) {
new PetValidator().validate(pet, result);
if (result.hasErrors()) {
return "petForm";
}else {
this.clinic.storePet(pet);
status.setComplete();
return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
}
}
}
Form Validation
Code Demo ...
• BindingResult
• Validator.validate()
• <form:errors> tag
Alternative: Hibernate Validator can also be used for annotation
based validation.
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
}
@RequestMapping("/foo")
public void processFoo(@Valid Foo foo) {
/* ... */
}
Unit Testing
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring-servlet-test.xml" })
@Test
Other useful Annotations:
@DirtiesContext
@ExpectedException(SomeBusinessException.class)
@Timed(millis=1000)
@NotTransactional
Spring Security
Code Demo ...
• <sec:authorize> tag
• Annotations: @PreAuthorize
• applicationContext-security.xml
• DB Schema: Users, Authorities
Thank you and Questions?

Contenu connexe

Similaire à springtraning-7024840-phpapp01.pdf

Introduction to Dependency Injection
Introduction to Dependency InjectionIntroduction to Dependency Injection
Introduction to Dependency InjectionSolTech, Inc.
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Hitesh-Java
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansPawanMM
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOCCarl Lu
 
Introduction to Spring
Introduction to SpringIntroduction to Spring
Introduction to SpringSujit Kumar
 
Polaris presentation ioc - code conference
Polaris presentation   ioc - code conferencePolaris presentation   ioc - code conference
Polaris presentation ioc - code conferenceSteven Contos
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi ApplicationsReal World Asp.Net WebApi Applications
Real World Asp.Net WebApi ApplicationsEffie Arditi
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applicationsBabak Naffas
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignJames Phillips
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0Michael Vorburger
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency InjectionTheo Jungeblut
 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAOAnushaNaidu
 
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1Rich Helton
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC frameworkMohit Gupta
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckTheo Jungeblut
 
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstElements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstEnea Gabriel
 

Similaire à springtraning-7024840-phpapp01.pdf (20)

Introduction to Dependency Injection
Introduction to Dependency InjectionIntroduction to Dependency Injection
Introduction to Dependency Injection
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI Beans
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
 
Introduction to Spring
Introduction to SpringIntroduction to Spring
Introduction to Spring
 
Polaris presentation ioc - code conference
Polaris presentation   ioc - code conferencePolaris presentation   ioc - code conference
Polaris presentation ioc - code conference
 
Dependency Injection Styles
Dependency Injection StylesDependency Injection Styles
Dependency Injection Styles
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi ApplicationsReal World Asp.Net WebApi Applications
Real World Asp.Net WebApi Applications
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class Design
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency Injection
 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAO
 
Dependency Injection with Apex
Dependency Injection with ApexDependency Injection with Apex
Dependency Injection with Apex
 
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
 
Hybernat and structs, spring classes in mumbai
Hybernat and structs, spring classes in mumbaiHybernat and structs, spring classes in mumbai
Hybernat and structs, spring classes in mumbai
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC framework
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
 
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstElements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code First
 

Plus de BruceLee275640

introductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdfintroductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdfBruceLee275640
 
fdocuments.in_introduction-to-ibatis.ppt
fdocuments.in_introduction-to-ibatis.pptfdocuments.in_introduction-to-ibatis.ppt
fdocuments.in_introduction-to-ibatis.pptBruceLee275640
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxBruceLee275640
 

Plus de BruceLee275640 (7)

Git_new.pptx
Git_new.pptxGit_new.pptx
Git_new.pptx
 
introductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdfintroductiontogitandgithub-120702044048-phpapp01.pdf
introductiontogitandgithub-120702044048-phpapp01.pdf
 
68837.ppt
68837.ppt68837.ppt
68837.ppt
 
fdocuments.in_introduction-to-ibatis.ppt
fdocuments.in_introduction-to-ibatis.pptfdocuments.in_introduction-to-ibatis.ppt
fdocuments.in_introduction-to-ibatis.ppt
 
Utility.ppt
Utility.pptUtility.ppt
Utility.ppt
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
life science.pptx
life science.pptxlife science.pptx
life science.pptx
 

Dernier

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 

Dernier (20)

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 

springtraning-7024840-phpapp01.pdf

  • 1. Spring Training TechFerry Infotech Pvt. Ltd. (http://www.techferry.com/)
  • 2. Conversations o Introduction to Spring o Concepts: Annotations, MVC, IOC/DI, Auto wiring o Spring Bean/Resource Management o Spring MVC, Form Validations. o Unit Testing o Spring Security – Users, Roles, Permissions. o Code Demo  CRUD using Spring, Hibernate, MySQL.  Spring security example.  REST/jQuery/Ajax example
  • 3. Spring - Introduction Exercise: What do we need in an enterprise application? • Database Access, Connection Pools? • Transactions? • Security, Authentication, Authorization? • Business Logic Objects? • Workflow/Screen Flow? • Messaging/emails? • Service Bus? • Concurrency/Scalability? Can somebody wire all the needed components? Do we have to learn everything before we can start?
  • 4. Hello Spring • Spring is potentially a one-stop shop, addressing most infrastructure concerns of typical web applications o so you focus only on your business logic. • Spring is both comprehensive and modular o use just about any part of it in isolation, yet its architecture is internally consistent. o maximum value from your learning curve.
  • 5. What is Spring? • Open source and lightweight web-application framework • Framework for wiring the entire application • Collection of many different components • Reduces code and speeds up development Spring is essentially a technology dedicated to enabling you to build applications using POJOs.
  • 6. Why Spring? • Spring Enables POJO Programming o Application code does not depend on spring API’s • Dependency Injection and Inversion of Control simplifies coding o Promotes decoupling and re-usability Features: • Lightweight • Inversion of Control (IoC) • Aspect oriented (AOP) • MVC Framework • Transaction Management • JDBC • Ibatis / Hibernate
  • 8. What else Spring do? Spring Web Flow Spring Integration Spring Web-Services Spring MVC Spring Security Spring Batch Spring Social Spring Mobile ... and let it ever expand ...
  • 9. Inversion of Control/Dependency Injection "Don't call me, I'll call you." • IoC moves the responsibility for making things happen into the framework • Eliminates lookup code from within the application • Loose coupling, minimum effort and least intrusive mechanism
  • 11. IOC/DI Non IOC Example: class MovieLister... private MovieFinder finder; public MovieLister() { finder = new MovieFinderImpl(); } public interface MovieFinder { List findAll(); } class MovieFinderImpl ... { public List findAll() { ... } }
  • 12. IOC/DI IoC Example: DI exists in major two variants: Setter Injection public class MovieLister { private MovieFinder movieFinder; public void setMovieFinder(MovieFinder movieFinder) { this.movieFinder = movieFinder; } } Constructor Injection public class MovieLister{ private MovieFinder movieFinder; public MovieLister(MovieFinder movieFinder) { this.movieFinder = movieFinder; } }
  • 13. Code Demo .... • Annotations: @Component, @Service, @Repository • Annotation: @Autowire • web.xml - Context loader listener to scan components • <context:annotation-config /> <context:component-scan base-package="..." /> Spring Bean Management
  • 14. Bean Scopes singleton Scopes a single bean definition to a single object instance per Spring IoC container. prototype Scopes a single bean definition to any number of object instances. request Scopes a single bean definition to the lifecycle of a single HTTP request. session Scopes a single bean definition to the lifecycle of a HTTP Session. global session Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context.
  • 16. Prototype Beans • Use @Scope("prototype") • Caution: dependencies are resolved at instantiation time. It does NOT create a new instance at runtime more than once.
  • 17. Bean Scopes Contd.. • As a rule of thumb, you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans. • RequestContextListener is needed in web.xml for request/session scopes. • Annotation:@Scope("request") @Scope("prototype") Homework: • Singleton bean referring a prototype/request bean? • @Qualifier, Method Injection. Hate Homework? • Stick to stateless beans. :)
  • 18. Wiring Beans no No autowiring at all. Bean references must be defined via a ref element. This is the default. byName Autowiring by property name. byType Allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown. constructor This is analogous to byType, but applies to constructor arguments. autodetect Chooses constructor or byType through introspection of the bean class.
  • 19. Homework :) – What wiring method is used with @Autowire annotation? – Other annotations you may find useful: o @Required o @Resource Also review the Spring annotation article: http://www.techferry.com/articles/spring-annotations.html
  • 20. MVC - Model View Controller • Better organization and code reuse. • Separation of Concern • Can support multiple views
  • 21. Spring MVC Code Demo .... • Annotations: @Controller, @RequestMapping, @ModelAttribute, @PathVariable • Spring DispatcherServlet config - just scan controllers • web.xml - Context loader listener to scan other components • ResourceBundleMessageSource and <spring:message> tag Reference: http://static.springsource.org/spring/docs/3.0.x/spring- framework-reference/html/mvc.html • @RequestMapping Details • Handler method arguments and Return Types
  • 22. Pre-populate Model and Session Objects @Controller @RequestMapping("/owners/{ownerId}/pets/{petId}/edit") @SessionAttributes("pet") public class EditPetForm { @ModelAttribute("types") public Collection<PetType> populatePetTypes() { return this.clinic.getPetTypes(); } @RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { new PetValidator().validate(pet, result); if (result.hasErrors()) { return "petForm"; }else { this.clinic.storePet(pet); status.setComplete(); return "redirect:owner.do?ownerId=" + pet.getOwner().getId(); } } }
  • 23. Form Validation Code Demo ... • BindingResult • Validator.validate() • <form:errors> tag Alternative: Hibernate Validator can also be used for annotation based validation. public class PersonForm { @NotNull @Size(max=64) private String name; @Min(0) private int age; } @RequestMapping("/foo") public void processFoo(@Valid Foo foo) { /* ... */ }
  • 24. Unit Testing @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/spring-servlet-test.xml" }) @Test Other useful Annotations: @DirtiesContext @ExpectedException(SomeBusinessException.class) @Timed(millis=1000) @NotTransactional
  • 25. Spring Security Code Demo ... • <sec:authorize> tag • Annotations: @PreAuthorize • applicationContext-security.xml • DB Schema: Users, Authorities
  • 26. Thank you and Questions?