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>
|
<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>
|
||||||
|
|
|
@ -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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue