Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Spring

No description
by

Sachin Ghalme

on 19 November 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Spring

Lightweight
Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB
Inversion Of Control
Dependency Injection
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor() {
spellChecker = new SpellChecker();
}
}
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
}
Type
Constructor-based dependency injection
Setter-based dependency injection
A parameter injection
An interface injection
factory pattern
Service Locator Pattern
Template Method Design Pattern
Strategy Design Pattern
Aspect Oriented Programming
cross-cutting concerns
conceptually separate from the application's business logic
logging, declarative transactions, security, and caching etc.
What is Spring ?
This tutorial has been written based on Spring Framework version 3.1.0 released in Dec 2011.
Spring framework is an open source Java platform that provides comprehensive infrastructure support for developing robust Java applications very easily and very rapidly.
Spring framework was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.
Spring MVC
Spring Container
Spring BeanFactory Container
Spring ApplicationContext Container
The ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over the BeanFactory.
BeanFactory is used for light weight applications like mobile devices or applet based applications where data volume and speed is significant.
public class HelloWorld {
private String message;

public void setMessage(String message) {
this.message = message;
}

public void getMessage() {
System.out.println("Your Message : " + message);
}
}

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>

</beans>

package com.tutorialspoint;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class MainApp {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
"Beans.xml"));

HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
obj.getMessage();
}
}

This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.
package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainApp {
public static void main(String[] args) {

ApplicationContext context = new FileSystemXmlApplicationContext(
"C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");

HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
public class HelloWorld {
private String message;

public void setMessage(String message) {
this.message = message;
}

public void getMessage() {
System.out.println("Your Message : " + message);
}
}

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>

</beans>
The most commonly used ApplicationContext implementations are:
FileSystemXmlApplicationContext: This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.
ClassPathXmlApplicationContext This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH.
WebXmlApplicationContext: This container loads the XML file with definitions of all beans from within a web application.
Exception Handling
Lightweight
Dependency Injection
Aspect Oriented Programming
Spring MVC
Spring Container
Exception Handling
Set Proper Java Class Path
Install Eclipse
Ready with Server
Apache web Server
JBoss Application Server
Download All Require Jar Files
antlr-runtime-3.0.1
org.springframework.aop-3.1.0.M2
org.springframework.asm-3.1.0.M2
org.springframework.aspects-3.1.0.M2
org.springframework.beans-3.1.0.M2
org.springframework.context.support-3.1.0.M2
org.springframework.context-3.1.0.M2
org.springframework.core-3.1.0.M2
org.springframework.expression-3.1.0.M2
commons-logging-1.1.1
Step 1 - Create Java Project:
Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your project as HelloSpring using the wizard window as follows:
Add Required Libraries
Build Path -> Configure Build Path to display the Java Build Path window as follows:
Create Source Files
We will create HelloWorld.java and MainApp.java files under the com.tutorialspoint package
package com.tutorialspoint;

public class HelloWorld {
private String message;

public void setMessage(String message){
this.message = message;
}

public void getMessage(){
System.out.println("Your Message : " + message);
}
}
package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");

HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

obj.getMessage();
}
}
Create Bean Configuration File
This file needs to be created under the src directory
<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>

</beans>
Running the Program:
Keep MainApp.Java file tab active and use either Run option available in the Eclipse IDE or use Ctrl + F11 to compile and run your MainApp application
If everything is fine with your application, this will print the following message in Eclipse IDE's console:
Your Message : Hello World!
Spring Beans
Spring Bean Definition
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.
The bean definition contains the information called configuration metadata which is needed for the container to know the followings:

How to create a bean
Bean's lifecycle details
Bean's dependencies

following three important methods to provide configuration metadata to the Spring Container:

XML based configuration file.
Annotation-based configuration
Java-based configuration

Spring Bean Scopes
When defining a <bean> in Spring, you have the option of declaring a scope for that bean.
The Spring Framework supports following five scopes, three of which are available only if you use a web-aware ApplicationContext.
singleton :-
This scopes the bean definition to a single instance per Spring IoC container (default).
prototype :-
This scopes a single bean definition to have any number of object instances.
request :-
This scopes a bean definition to an HTTP request. Only valid in the context of a web-aware Spring ApplicationContext.
session :-
This scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
global-session :-
This scopes a bean definition to a global HTTP session. Only valid in the context of a web-aware Spring ApplicationContext.
Create Source Files
We will create HelloWorld.java and MainApp.java files under the com.tutorialspoint package
package com.tutorialspoint;

public class HelloWorld {
private String message;

public void setMessage(String message){
this.message = message;
}

public void getMessage(){
System.out.println("Your Message :
" + message);
}
}
package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");

HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.setMessage("I'm object A");
objA.getMessage();

HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
objB.getMessage();
}
}
Create Bean Configuration File
This file needs to be created under the src directory
<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

<bean id="helloWorld" class="com.tutorialspoint.HelloWorld" scope="singleton">
</bean>
</beans>
Running the Program:
Keep MainApp.Java file tab active and use either Run option available in the Eclipse IDE or use Ctrl + F11 to compile and run your MainApp application
If everything is fine with your application, this will print the following message in Eclipse IDE's console:
Your Message : I'm object A
Your Message : I'm object A
Example
For Singleton
For Prototype
Your Message : I'm object A
Your Message : null
Spring Bean Life Cycle
The life cycle of a Spring bean is easy to understand. When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required
Initialization callbacks
Destruction callbacks
public class ExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
<bean id="exampleBean"
class="examples.ExampleBean" init-method="init"/>

public class ExampleBean {
public void init() {
// do some initialization work
}
}

public class ExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
}
}
<bean id="exampleBean"
class="examples.ExampleBean" destroy-method="destroy"/>

public class ExampleBean {
public void destroy() {
// do some destruction work
}
}
Spring Bean Life Cycle
The life cycle of a Spring bean is easy to understand. When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required
Initialization callbacks
Destruction callbacks
public class ExampleBean implements InitializingBean {
public void afterPropertiesSet() {
// do some initialization work
}
}
<bean id="exampleBean"
class="examples.ExampleBean" init-method="init"/>

public class ExampleBean {
public void init() {
// do some initialization work
}
}

public class ExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work
}
}
<bean id="exampleBean"
class="examples.ExampleBean" destroy-method="destroy"/>

public class ExampleBean {
public void destroy() {
// do some destruction work
}
}
Full transcript