scopes examples
This commit is contained in:
parent
29deacf3b9
commit
c3abba5f2a
|
@ -0,0 +1,15 @@
|
|||
package org.baeldung.scopes;
|
||||
|
||||
public class HelloMessageGenerator {
|
||||
|
||||
private String message;
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(final String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package org.baeldung.scopes;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(final String name, final int age) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [name=" + name + ", age=" + age + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.baeldung.scopes;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class ScopesController {
|
||||
|
||||
@Resource(name = "requestMessage")
|
||||
HelloMessageGenerator firstRequestMessage;
|
||||
|
||||
@Resource(name = "requestMessage")
|
||||
HelloMessageGenerator secondRequestMessage;
|
||||
|
||||
@Resource(name = "sessionMessage")
|
||||
HelloMessageGenerator firstSessionMessage;
|
||||
|
||||
@Resource(name = "sessionMessage")
|
||||
HelloMessageGenerator secondSessionMessage;
|
||||
|
||||
@RequestMapping("/scopes")
|
||||
public String getScopes() {
|
||||
return "scopesExample";
|
||||
}
|
||||
|
||||
@RequestMapping("/scopes/firstRequest")
|
||||
public String getFirstRequest(final Model model) {
|
||||
firstRequestMessage.setMessage("Good morning!");
|
||||
firstSessionMessage.setMessage("Good afternoon!");
|
||||
model.addAttribute("requestMessage", firstRequestMessage.getMessage());
|
||||
model.addAttribute("sessionMessage", firstSessionMessage.getMessage());
|
||||
return "scopesFirstRequest";
|
||||
}
|
||||
|
||||
@RequestMapping("/scopes/secondRequest")
|
||||
public String getSecondRequest(final Model model) {
|
||||
secondRequestMessage.setMessage("Good evening!");
|
||||
model.addAttribute("requestMessage", secondRequestMessage.getMessage());
|
||||
model.addAttribute("sessionMessage", secondSessionMessage.getMessage());
|
||||
return "scopesSecondRequest";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package org.baeldung.spring.config;
|
||||
|
||||
import org.baeldung.scopes.HelloMessageGenerator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
import org.springframework.web.servlet.view.UrlBasedViewResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.scopes")
|
||||
@EnableWebMvc
|
||||
public class ScopesConfig {
|
||||
@Bean
|
||||
public UrlBasedViewResolver setupViewResolver() {
|
||||
final UrlBasedViewResolver resolver = new UrlBasedViewResolver();
|
||||
resolver.setPrefix("/WEB-INF/view/");
|
||||
resolver.setSuffix(".jsp");
|
||||
resolver.setViewClass(JstlView.class);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
public HelloMessageGenerator requestMessage() {
|
||||
return new HelloMessageGenerator();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
public HelloMessageGenerator sessionMessage() {
|
||||
return new HelloMessageGenerator();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?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.xsd">
|
||||
|
||||
<bean id="personSingleton" class="org.baeldung.scopes.Person" scope="singleton"/>
|
||||
|
||||
<bean id="personPrototype" class="org.baeldung.scopes.Person" scope="prototype"/>
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,10 @@
|
|||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>Bean Scopes Examples</h1>
|
||||
<br>
|
||||
<a href="${pageContext.request.contextPath}/scopes/firstRequest">First Request</a><br><br>
|
||||
<a href="${pageContext.request.contextPath}/scopes/secondRequest">Second Request</a>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,11 @@
|
|||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>Bean Scopes First Request</h1>
|
||||
|
||||
Request bean message: ${requestMessage }
|
||||
Session bean message: ${sessionMessage }
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,10 @@
|
|||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>Bean Scopes Second Request</h1>
|
||||
|
||||
Request bean message: ${requestMessage }
|
||||
Session bean message: ${sessionMessage }
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,52 @@
|
|||
package org.baeldung.scopes;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class ScopesTest {
|
||||
|
||||
private static final String NAME = "John Smith";
|
||||
private static final int AGE = 30;
|
||||
|
||||
private static final String NAME_OTHER = "Anna Jones";
|
||||
private static final int AGE_OTHER = 40;
|
||||
|
||||
@Test
|
||||
public void testScopeSingleton() {
|
||||
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("scopes.xml");
|
||||
|
||||
final Person personSingletonA = (Person) applicationContext.getBean("personSingleton");
|
||||
final Person personSingletonB = (Person) applicationContext.getBean("personSingleton");
|
||||
|
||||
personSingletonA.setName(NAME);
|
||||
personSingletonB.setAge(AGE);
|
||||
|
||||
Assert.assertEquals(NAME, personSingletonB.getName());
|
||||
Assert.assertEquals(AGE, personSingletonB.getAge());
|
||||
|
||||
((AbstractApplicationContext) applicationContext).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScopePrototype() {
|
||||
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("scopes.xml");
|
||||
|
||||
final Person personPrototypeA = (Person) applicationContext.getBean("personPrototype");
|
||||
final Person personPrototypeB = (Person) applicationContext.getBean("personPrototype");
|
||||
|
||||
personPrototypeA.setName(NAME);
|
||||
personPrototypeA.setAge(AGE);
|
||||
|
||||
personPrototypeB.setName(NAME_OTHER);
|
||||
personPrototypeB.setAge(AGE_OTHER);
|
||||
|
||||
Assert.assertEquals(NAME, personPrototypeA.getName());
|
||||
Assert.assertEquals(NAME_OTHER, personPrototypeB.getName());
|
||||
|
||||
((AbstractApplicationContext) applicationContext).close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue