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 javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @Controller
public class ScopesController { public class ScopesController {
public static final Logger LOG = Logger.getLogger(ScopesController.class);
@Resource(name = "requestMessage") @Resource(name = "requestMessage")
HelloMessageGenerator firstRequestMessage; HelloMessageGenerator requestMessage;
@Resource(name = "requestMessage")
HelloMessageGenerator secondRequestMessage;
@Resource(name = "sessionMessage") @Resource(name = "sessionMessage")
HelloMessageGenerator firstSessionMessage; HelloMessageGenerator sessionMessage;
@Resource(name = "sessionMessage")
HelloMessageGenerator secondSessionMessage;
@RequestMapping("/scopes") @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"; 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; package org.baeldung.spring.config;
import org.baeldung.scopes.HelloMessageGenerator; import org.baeldung.scopes.HelloMessageGenerator;
import org.baeldung.scopes.Person;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -42,4 +43,15 @@ public class ScopesConfig {
return new HelloMessageGenerator(); 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> <body>
<h1>Bean Scopes Examples</h1> <h1>Bean Scopes Examples</h1>
<br> <br>
<a href="${pageContext.request.contextPath}/scopes/firstRequest">First Request</a><br><br> Request Message: ${requestMessage }<br>
<a href="${pageContext.request.contextPath}/scopes/secondRequest">Second Request</a> Session Message: ${sessionMessage }
</body> </body>
</html> </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>