From c3abba5f2abe5dbdfa08ce5f6cbbb1546458ccf9 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 27 Apr 2016 08:33:56 +0300 Subject: [PATCH 1/3] scopes examples --- .../scopes/HelloMessageGenerator.java | 15 ++++++ .../main/java/org/baeldung/scopes/Person.java | 37 +++++++++++++ .../org/baeldung/scopes/ScopesController.java | 46 ++++++++++++++++ .../baeldung/spring/config/ScopesConfig.java | 39 ++++++++++++++ spring-all/src/main/resources/scopes.xml | 10 ++++ .../webapp/WEB-INF/view/scopesExample.jsp | 10 ++++ .../WEB-INF/view/scopesFirstRequest.jsp | 11 ++++ .../WEB-INF/view/scopesSecondRequest.jsp | 10 ++++ .../java/org/baeldung/scopes/ScopesTest.java | 52 +++++++++++++++++++ 9 files changed, 230 insertions(+) create mode 100644 spring-all/src/main/java/org/baeldung/scopes/HelloMessageGenerator.java create mode 100644 spring-all/src/main/java/org/baeldung/scopes/Person.java create mode 100644 spring-all/src/main/java/org/baeldung/scopes/ScopesController.java create mode 100644 spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java create mode 100644 spring-all/src/main/resources/scopes.xml create mode 100644 spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp create mode 100644 spring-all/src/main/webapp/WEB-INF/view/scopesFirstRequest.jsp create mode 100644 spring-all/src/main/webapp/WEB-INF/view/scopesSecondRequest.jsp create mode 100644 spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java 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(); + } + +} From e792db4d6d890fbf56ca74024c8d811433020d05 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 5 May 2016 20:19:48 +0300 Subject: [PATCH 2/3] revised bean scopes examples --- .../src/main/java/org/baeldung/scopes/Person.java | 12 +----------- .../org/baeldung/spring/config/ScopesConfig.java | 6 ++++++ .../test/java/org/baeldung/scopes/ScopesTest.java | 9 --------- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/spring-all/src/main/java/org/baeldung/scopes/Person.java b/spring-all/src/main/java/org/baeldung/scopes/Person.java index 993921abff..e6139c31dd 100644 --- a/spring-all/src/main/java/org/baeldung/scopes/Person.java +++ b/spring-all/src/main/java/org/baeldung/scopes/Person.java @@ -8,9 +8,7 @@ public class Person { } public Person(final String name, final int age) { - super(); this.name = name; - this.age = age; } public String getName() { @@ -21,17 +19,9 @@ public class Person { 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 + "]"; + return "Person [name=" + name + "]"; } } 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 index 48820636dd..1de7b32720 100644 --- a/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java +++ b/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java @@ -36,4 +36,10 @@ public class ScopesConfig { return new HelloMessageGenerator(); } + @Bean + @Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) + public HelloMessageGenerator globalSessionMessage() { + return new HelloMessageGenerator(); + } + } diff --git a/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java b/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java index f86fda66ba..b1dd248c26 100644 --- a/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java +++ b/spring-all/src/test/java/org/baeldung/scopes/ScopesTest.java @@ -9,10 +9,7 @@ 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() { @@ -22,10 +19,7 @@ public class ScopesTest { 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(); } @@ -38,10 +32,7 @@ public class ScopesTest { 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()); From abb64b29a75656cd620ad99926e257d2d12a66ff Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Tue, 31 May 2016 21:50:25 +0300 Subject: [PATCH 3/3] modifications to request and session scopes examples --- .../org/baeldung/scopes/ScopesController.java | 37 ++++++------------- .../baeldung/spring/config/ScopesConfig.java | 12 ++++++ .../webapp/WEB-INF/view/scopesExample.jsp | 4 +- .../WEB-INF/view/scopesFirstRequest.jsp | 11 ------ .../WEB-INF/view/scopesSecondRequest.jsp | 10 ----- 5 files changed, 25 insertions(+), 49 deletions(-) delete mode 100644 spring-all/src/main/webapp/WEB-INF/view/scopesFirstRequest.jsp delete mode 100644 spring-all/src/main/webapp/WEB-INF/view/scopesSecondRequest.jsp diff --git a/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java b/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java index 5f29d0333f..bf733b75f9 100644 --- a/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java +++ b/spring-all/src/main/java/org/baeldung/scopes/ScopesController.java @@ -2,45 +2,30 @@ 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 firstRequestMessage; - - @Resource(name = "requestMessage") - HelloMessageGenerator secondRequestMessage; + HelloMessageGenerator requestMessage; @Resource(name = "sessionMessage") - HelloMessageGenerator firstSessionMessage; - - @Resource(name = "sessionMessage") - HelloMessageGenerator secondSessionMessage; + HelloMessageGenerator sessionMessage; @RequestMapping("/scopes") - public String getScopes() { + 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"; } - @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 index 1de7b32720..5a9b266388 100644 --- a/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java +++ b/spring-all/src/main/java/org/baeldung/spring/config/ScopesConfig.java @@ -1,6 +1,7 @@ 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; @@ -42,4 +43,15 @@ public class ScopesConfig { return new HelloMessageGenerator(); } + @Bean + @Scope("prototype") + public Person personPrototype() { + return new Person(); + } + + @Bean + @Scope("singleton") + public Person personSingleton() { + return new Person(); + } } diff --git a/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp b/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp index 624ff46d5c..7974cf0220 100644 --- a/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp +++ b/spring-all/src/main/webapp/WEB-INF/view/scopesExample.jsp @@ -4,7 +4,7 @@

Bean Scopes Examples


- First Request

- Second Request + Request Message: ${requestMessage }
+ Session Message: ${sessionMessage } \ 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 deleted file mode 100644 index 3804dfd1c6..0000000000 --- a/spring-all/src/main/webapp/WEB-INF/view/scopesFirstRequest.jsp +++ /dev/null @@ -1,11 +0,0 @@ - - - - -

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 deleted file mode 100644 index 8e9d727661..0000000000 --- a/spring-all/src/main/webapp/WEB-INF/view/scopesSecondRequest.jsp +++ /dev/null @@ -1,10 +0,0 @@ - - - - -

Bean Scopes Second Request

- - Request bean message: ${requestMessage } - Session bean message: ${sessionMessage } - - \ No newline at end of file