Merge pull request #5263 from dkapil/task/BAEL-8965

BAEL-8965 Update "Spring Bean Scopes" article
This commit is contained in:
Loredana Crusoveanu 2018-09-16 09:38:30 +03:00 committed by GitHub
commit 41b2495dcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 19 deletions

View File

@ -45,6 +45,15 @@
<artifactId>spring-shell</artifactId>
<version>${org.springframework.shell.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>org.springframework</groupId>

View File

@ -13,21 +13,36 @@ import org.springframework.web.bind.annotation.RequestMapping;
public class ScopesController {
public static final Logger LOG = LoggerFactory.getLogger(ScopesController.class);
@Resource(name = "requestMessage")
HelloMessageGenerator requestMessage;
@Resource(name = "requestScopedBean")
HelloMessageGenerator requestScopedBean;
@Resource(name = "sessionMessage")
HelloMessageGenerator sessionMessage;
@Resource(name = "sessionScopedBean")
HelloMessageGenerator sessionScopedBean;
@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());
@Resource(name = "applicationScopedBean")
HelloMessageGenerator applicationScopedBean;
@RequestMapping("/scopes/request")
public String getRequestScopeMessage(final Model model) {
model.addAttribute("previousMessage", requestScopedBean.getMessage());
requestScopedBean.setMessage("Request Scope Message!");
model.addAttribute("currentMessage", requestScopedBean.getMessage());
return "scopesExample";
}
@RequestMapping("/scopes/session")
public String getSessionScopeMessage(final Model model) {
model.addAttribute("previousMessage", sessionScopedBean.getMessage());
sessionScopedBean.setMessage("Session Scope Message!");
model.addAttribute("currentMessage", sessionScopedBean.getMessage());
return "scopesExample";
}
@RequestMapping("/scopes/application")
public String getApplicationScopeMessage(final Model model) {
model.addAttribute("previousMessage", applicationScopedBean.getMessage());
applicationScopedBean.setMessage("Application Scope Message!");
model.addAttribute("currentMessage", applicationScopedBean.getMessage());
return "scopesExample";
}
}

View File

@ -27,19 +27,25 @@ public class ScopesConfig {
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator requestMessage() {
public HelloMessageGenerator requestScopedBean() {
return new HelloMessageGenerator();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator sessionMessage() {
public HelloMessageGenerator sessionScopedBean() {
return new HelloMessageGenerator();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator globalSessionMessage() {
public HelloMessageGenerator applicationScopedBean() {
return new HelloMessageGenerator();
}
@Bean
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator websocketScopedBean() {
return new HelloMessageGenerator();
}

View File

@ -2,9 +2,9 @@
<head></head>
<body>
<h1>Bean Scopes Examples</h1>
<br>
Request Message: ${requestMessage }<br>
Session Message: ${sessionMessage }
<h1>Bean Scopes Examples</h1>
<br> Previous Message: ${previousMessage }
<br> Current Message: ${currentMessage }
<br>
</body>
</html>