While defining a bean, developer will have the option to describe the scope of the bean. The spring framework will support the following five scopes.
- Singleton:A singleton is by default scope if the developer doesn't mention scope which means there one instance per Spring container.
- Prototype:Instead of having one instance per container, with prototype developer gets a new instance per request.
- Request:This scope depends on the HTTP request.
- Session:This scope depends on the HTTP session.
- Global-session:This scope depends on the global HTTP session. This scope will be available in a web-aware Spring ApplicationContext.
A singleton is by default scope if the developer doesn't mention scope which means there one instance per Spring container.
[java]<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
<!-- collaborators and configuration for this bean go here -->
</bean>[/java]
Each bean will consists of unique id in the container. Id can not contain special characters like ‘/’ and pattern matching like ‘/*. html'. But name property can follow this feature best form spring move. Name property can set with special characters like “/*. html”. Which is directly called in the URL also.
Helloword.Java
[java]
package splessons;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
[/java]
Here the developer just created data member and performed SET and GET methods to get the data.
MainApp.Java
[java]
package splessons;
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 h1 = (HelloWorld) context.getBean("helloWorld");
h1.setMessage("SPLESSONS");
h1.getMessage();
HelloWorld h2 = (HelloWorld) context.getBean("helloWorld");
h2.getMessage();
}
}
[/java]
The
Application Context is spring's more best in class holder. Like
BeanFactory it can stack bean definitions, wire beans together and administer beans upon solicitation. Also it includes more enterprise-specific usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application events to interested event listeners. This container is characterized by the
org.springframework.context.ApplicationContext interface.
Beans.xml
[java]
<?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="splessons.HelloWorld"
scope="singleton">
</bean>
</beans>
[/java]
Output
[java]
Your Message :SPLESSONS
Your Message :SPLESSONS
[/java]
In case of prototype scope, some pool will be maintained by core container. It will serve the bean instance from that pool. Example as follows.
Helloword.Java
[java]
package splessons;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
[/java]
MainApp.Java
[java]
package splessons;
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("SPLESSONS");
objA.getMessage();
HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
objB.getMessage();
}
}
[/java]
The classpath syntax means that Spring will search the classpath for all resources called /META-INF/spring.xml and
myapplication-application-context.xml, and will amalgamate them into the context. This includes looking through JAR files inside the project, so there may not be any visible within your main project files.
Beans.xml
[java]
<?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="splessons.HelloWorld" scope="singleton">
</bean>
</beans>
[/java]
Output
[java]
Your Message :SPLESSONS
Your Message :null
[/java]