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> <artifactId>spring-shell</artifactId>
<version>${org.springframework.shell.version}</version> <version>${org.springframework.shell.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
<!-- aspectj --> <!-- aspectj -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>

View File

@ -13,21 +13,36 @@ import org.springframework.web.bind.annotation.RequestMapping;
public class ScopesController { public class ScopesController {
public static final Logger LOG = LoggerFactory.getLogger(ScopesController.class); public static final Logger LOG = LoggerFactory.getLogger(ScopesController.class);
@Resource(name = "requestMessage") @Resource(name = "requestScopedBean")
HelloMessageGenerator requestMessage; HelloMessageGenerator requestScopedBean;
@Resource(name = "sessionMessage") @Resource(name = "sessionScopedBean")
HelloMessageGenerator sessionMessage; HelloMessageGenerator sessionScopedBean;
@RequestMapping("/scopes") @Resource(name = "applicationScopedBean")
public String getScopes(final Model model) { HelloMessageGenerator applicationScopedBean;
LOG.info("Request Message:" + requestMessage.getMessage());
LOG.info("Session Message" + sessionMessage.getMessage()); @RequestMapping("/scopes/request")
requestMessage.setMessage("Good morning!"); public String getRequestScopeMessage(final Model model) {
sessionMessage.setMessage("Good afternoon!"); model.addAttribute("previousMessage", requestScopedBean.getMessage());
model.addAttribute("requestMessage", requestMessage.getMessage()); requestScopedBean.setMessage("Request Scope Message!");
model.addAttribute("sessionMessage", sessionMessage.getMessage()); model.addAttribute("currentMessage", requestScopedBean.getMessage());
return "scopesExample"; 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 @Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator requestMessage() { public HelloMessageGenerator requestScopedBean() {
return new HelloMessageGenerator(); return new HelloMessageGenerator();
} }
@Bean @Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator sessionMessage() { public HelloMessageGenerator sessionScopedBean() {
return new HelloMessageGenerator(); return new HelloMessageGenerator();
} }
@Bean @Bean
@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS) @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(); return new HelloMessageGenerator();
} }

View File

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