×
DocsConsole

DocsConsole

Spring Bean Scopes - Request

By | | Updated : 2018-01-07 | Viewed : 340 times

Spring Bean Scopes Request

When we want create the Request typed scoped bean, we need to set up the Web aware ApplicationContext. We should have to use web-aware Spring ApplicationContex. Standard ApplicationContext will not be not useful here. Because Request, Response and Session are available for web-aware ApplicationContex. These all three scoped bean only available with Web aware ApplicationContext/XmlWebApplicationContext.

Please create maven project as given below.

Spring Scopes Request

Please modify the pom.xml as given below.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.docsconsole.spring</groupId>
	<artifactId>SpringBeanScopes-Request</artifactId>
	<version>1.0</version>
	<packaging>war</packaging>

	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-asm</artifactId>
			<version>3.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>

	</dependencies>

	<build>
		<finalName>SpringBeanScopes-Request</finalName>
	</build>
</project>

Please create the folder WEB-INF folder create spring-servlet.xml as given below.

spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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
 	http://www.springframework.org/schema/mvc
 	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 	http://www.springframework.org/schema/context
 	http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.docsconsole.spring" />
	<mvc:annotation-driven />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/views/</value>
		</property>
		
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	
	<bean id = "springBean" class="com.docsconsole.spring.ASpringBean" scope = "request">
	</bean>
	<bean id = "bspringBean" class="com.docsconsole.spring.BSpringBean" scope = "prototype">
	</bean>

</beans>

Please create the web.xml and add the below given content.

web.xml
 <web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 				http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Spring4MVCHelloWorldDemo Web Application</display-name>

	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>

</web-app>

Please create the ASpringBean.java as given below.

ASpringBean.java
/**
* 
* @author:Amar
* @since:2018-02-02 
*
*/
package com.docsconsole.spring;

public class ASpringBean {

}

Please create the BSpringBean.java as given below.

BSpringBean.java
/**
* 
* @author:Amar
* @since:2018-02-02 
*
*/

package com.docsconsole.spring;

public class BSpringBean {

}

Please create ControllerBean.java file as shown directory and add below given code.

ControllerBean.java
/**
* 
* @author:AmarSivas
*
*/

package com.docsconsole.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.XmlWebApplicationContext;

@Controller
@RequestMapping("/")
public class ControllerBean {

	@Autowired
	private ASpringBean aSpringBean;

	@Autowired
	private ASpringBean a1SpringBean;

	@Autowired
	private BSpringBean bSpringBean;

	@Autowired
	private BSpringBean b1SpringBean;

	@RequestMapping(value = "/index")
	public String index(ModelMap model) {
		System.out.println("Request type scoped bean SpringBean");
		ApplicationContext context = new XmlWebApplicationContext();
		System.out.println("ASpringBean hashcode is" + aSpringBean.hashCode());
		System.out.println("ASpringBean hashcode is" + a1SpringBean.hashCode());
		System.out.println("BSpringBean hashcode is" + bSpringBean.hashCode());
		System.out.println("BSpringBean hashcode is" + b1SpringBean.hashCode());
		return "welcome";
	}

}

Please find out which is given below. Here ASpringBean has same hashcode. But if you observe the BSpringBean has two different hashcode.

Spring Scopes Request Result
Leave A Reply