|
| 1 | +package org.baeldung.spring.config; |
| 2 | + |
| 3 | +import org.baeldung.scopes.HelloMessageGenerator; |
| 4 | +import org.baeldung.scopes.Person; |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.context.annotation.ComponentScan; |
| 7 | +import org.springframework.context.annotation.Configuration; |
| 8 | +import org.springframework.context.annotation.Scope; |
| 9 | +import org.springframework.context.annotation.ScopedProxyMode; |
| 10 | +import org.springframework.web.context.WebApplicationContext; |
| 11 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 12 | +import org.springframework.web.servlet.view.JstlView; |
| 13 | +import org.springframework.web.servlet.view.UrlBasedViewResolver; |
| 14 | + |
| 15 | +@Configuration |
| 16 | +@ComponentScan("org.baeldung.scopes") |
| 17 | +@EnableWebMvc |
| 18 | +public class ScopesConfig { |
| 19 | + @Bean |
| 20 | + public UrlBasedViewResolver setupViewResolver() { |
| 21 | + final UrlBasedViewResolver resolver = new UrlBasedViewResolver(); |
| 22 | + resolver.setPrefix("/WEB-INF/view/"); |
| 23 | + resolver.setSuffix(".jsp"); |
| 24 | + resolver.setViewClass(JstlView.class); |
| 25 | + return resolver; |
| 26 | + } |
| 27 | + |
| 28 | + @Bean |
| 29 | + @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) |
| 30 | + public HelloMessageGenerator requestMessage() { |
| 31 | + return new HelloMessageGenerator(); |
| 32 | + } |
| 33 | + |
| 34 | + @Bean |
| 35 | + @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) |
| 36 | + public HelloMessageGenerator sessionMessage() { |
| 37 | + return new HelloMessageGenerator(); |
| 38 | + } |
| 39 | + |
| 40 | + @Bean |
| 41 | + @Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) |
| 42 | + public HelloMessageGenerator globalSessionMessage() { |
| 43 | + return new HelloMessageGenerator(); |
| 44 | + } |
| 45 | + |
| 46 | + @Bean |
| 47 | + @Scope("prototype") |
| 48 | + public Person personPrototype() { |
| 49 | + return new Person(); |
| 50 | + } |
| 51 | + |
| 52 | + @Bean |
| 53 | + @Scope("singleton") |
| 54 | + public Person personSingleton() { |
| 55 | + return new Person(); |
| 56 | + } |
| 57 | +} |
0 commit comments