diff --git a/spring-all/src/main/java/org/baeldung/scopes/HelloMessageGenerator.java b/spring-all/src/main/java/org/baeldung/scopes/HelloMessageGenerator.java new file mode 100644 index 0000000000..ae1c6157db --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scopes/HelloMessageGenerator.java @@ -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; + } + +} diff --git a/spring-all/src/main/java/org/baeldung/scopes/Person.java b/spring-all/src/main/java/org/baeldung/scopes/Person.java new file mode 100644 index 0000000000..993921abff --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scopes/Person.java @@ -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 + "]"; + } + +} diff --git a/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java b/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java new file mode 100644 index 0000000000..5f29d0333f --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java @@ -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"; + } + +} diff --git a/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java b/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java new file mode 100644 index 0000000000..48820636dd --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java @@ -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(); + } + +} diff --git a/spring-all/src/main/resources/scopes.xml b/spring-all/src/main/resources/scopes.xml new file mode 100644 index 0000000000..faecd727fa --- /dev/null +++ b/spring-all/src/main/resources/scopes.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp b/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp new file mode 100644 index 0000000000..624ff46d5c --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp @@ -0,0 +1,10 @@ + + + + +

Bean Scopes Examples

+
+ First Request

+ Second Request + + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/view/scopesFirstRequest.jsp b/spring-all/src/main/webapp/WEB-INF/view/scopesFirstRequest.jsp new file mode 100644 index 0000000000..3804dfd1c6 --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/view/scopesFirstRequest.jsp @@ -0,0 +1,11 @@ + + + + +

Bean Scopes First Request

+ + Request bean message: ${requestMessage } + Session bean message: ${sessionMessage } + + + \ No newline at end of file diff --git a/spring-all/src/main/webapp/WEB-INF/view/scopesSecondRequest.jsp b/spring-all/src/main/webapp/WEB-INF/view/scopesSecondRequest.jsp new file mode 100644 index 0000000000..8e9d727661 --- /dev/null +++ b/spring-all/src/main/webapp/WEB-INF/view/scopesSecondRequest.jsp @@ -0,0 +1,10 @@ + + + + +

Bean Scopes Second Request

+ + Request bean message: ${requestMessage } + Session bean message: ${sessionMessage } + + \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java b/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java new file mode 100644 index 0000000000..f86fda66ba --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java @@ -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(); + } + +}