commit
4b4b09adc0
|
@ -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,27 @@
|
|||
package org.baeldung.scopes;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(final String name, final int age) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.baeldung.scopes;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class ScopesController {
|
||||
public static final Logger LOG = Logger.getLogger(ScopesController.class);
|
||||
|
||||
@Resource(name = "requestMessage")
|
||||
HelloMessageGenerator requestMessage;
|
||||
|
||||
@Resource(name = "sessionMessage")
|
||||
HelloMessageGenerator sessionMessage;
|
||||
|
||||
@RequestMapping("/scopes")
|
||||
public String getScopes(final Model model) {
|
||||
LOG.info("Request Message:" + requestMessage.getMessage());
|
||||
LOG.info("Session Message" + sessionMessage.getMessage());
|
||||
requestMessage.setMessage("Good morning!");
|
||||
sessionMessage.setMessage("Good afternoon!");
|
||||
model.addAttribute("requestMessage", requestMessage.getMessage());
|
||||
model.addAttribute("sessionMessage", sessionMessage.getMessage());
|
||||
return "scopesExample";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package org.baeldung.spring.config;
|
||||
|
||||
import org.baeldung.scopes.HelloMessageGenerator;
|
||||
import org.baeldung.scopes.Person;
|
||||
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();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
public HelloMessageGenerator globalSessionMessage() {
|
||||
return new HelloMessageGenerator();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
public Person personPrototype() {
|
||||
return new Person();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope("singleton")
|
||||
public Person personSingleton() {
|
||||
return new Person();
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
Request Message: ${requestMessage }<br>
|
||||
Session Message: ${sessionMessage }
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,43 @@
|
|||
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 String NAME_OTHER = "Anna Jones";
|
||||
|
||||
@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);
|
||||
Assert.assertEquals(NAME, personSingletonB.getName());
|
||||
|
||||
((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);
|
||||
personPrototypeB.setName(NAME_OTHER);
|
||||
|
||||
Assert.assertEquals(NAME, personPrototypeA.getName());
|
||||
Assert.assertEquals(NAME_OTHER, personPrototypeB.getName());
|
||||
|
||||
((AbstractApplicationContext) applicationContext).close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue