Merge pull request #5263 from dkapil/task/BAEL-8965
BAEL-8965 Update "Spring Bean Scopes" article
This commit is contained in:
commit
41b2495dcc
|
@ -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>
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue