modifications to request and session scopes examples

This commit is contained in:
Loredana Crusoveanu 2016-05-31 21:50:25 +03:00
parent e792db4d6d
commit abb64b29a7
5 changed files with 25 additions and 49 deletions

View File

@ -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";
}
}

View File

@ -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();
}
}

View File

@ -4,7 +4,7 @@
<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>
Request Message: ${requestMessage }<br>
Session Message: ${sessionMessage }
</body>
</html>

View File

@ -1,11 +0,0 @@
<html>
<head></head>
<body>
<h1>Bean Scopes First Request</h1>
Request bean message: ${requestMessage }
Session bean message: ${sessionMessage }
</body>
</html>

View File

@ -1,10 +0,0 @@
<html>
<head></head>
<body>
<h1>Bean Scopes Second Request</h1>
Request bean message: ${requestMessage }
Session bean message: ${sessionMessage }
</body>
</html>