Spring Framework를 사용할 때 xml로 application context를 설정하는 것보다 JavaConfig와 Annotation 기반 설정을 사용하는 것이 훨씬 편하다. 하지만 대부분의 설명은 서블릿 3.0 이상에서 설정하는 것만 볼 수 있다. 서블릿 3.0 이전 버전의 구형 서블릿 엔진에서는 다음처럼 설정하면 된다.
xml로 설정할 때와 같이 web.xml
에 ContextLoaderListener
와
DispatcherServlet
을 등록하는데 설정값이 다르다.
먼저 ContextLoaderListener
는 다음과 같다.
1 <context-param>
2 <param-name>contextClass</param-name>
3 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
4 </context-param>
5
6 <context-param>
7 <param-name>contextConfigLocation</param-name>
8 <param-value>my.package.RootConfig</param-value>
9 </context-param>
10
11 <listener>
12 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
13 </listener>
여기서 my.package.RootConfig
는 JavaConfig 설정을 하는 java 클래스이다.
DispatcherServlet
은 다음과 같이 설정한다.
1 <servlet>
2 <servlet-name>dispatcher</servlet-name>
3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4 <init-param>
5 <param-name>contextClass</param-name>
6 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
7 </init-param>
8 <init-param>
9 <param-name>contextConfigLocation</param-name>
10 <param-value>my.package.MvcConfig</param-value>
11 </init-param>
12 <load-on-startup>1</load-on-startup>
13 </servlet>
위의 경우와 마찬가지로 my.package.MvcConfig
는 JavaConfig 설정을 하는 클래스이다.
즉 contextClass
파라미터로 AnnotationConfigWebApplicationContext
를 지정하고
contextConfigLocation
파라미터로 JavaConfig 설정하는 클래스를 등록하면 된다.
이제 application context를 JavaConfig로 설정할 수 있게 되었다.
이 예제에서는 RootConfig
로 기본 설정을 하고 MvcConfig
로 Spring MVC 관련한
설정을 하는 식으로 구성하였다. 먼저 RootConfig
는 다음과 같다.
1 @Configuration
2 @ComponentScan(
3 basePackages = "my.package",
4 excludeFilters = @ComponentScan.Filter(
5 value = { Controller.class, Configuration.class }
6 )
7 )
8 @EnableTransactionManagement
9 public class RootConfig {
10 @Bean
11 public DataSource dataSource() {
12 // DataSource 설정 jndi를 사용하는 예
13 final JndiDataSourceLookup lookup = new JndiDataSourceLookup();
14 Properties prop = new Properties();
15 prop.setProperty("lookupOnStartup", "false");
16 lookup.setJndiEnvironment(prop);
17 lookup.setResourceRef(true);
18 return lookup.getDataSource("jdbc/jndiname");
19 }
20
21 // .... 기타 설정
22
23 // profile 별로 설정값을 셋업하기 위해 이렇게 지정
24 @Configuration
25 @Profile("default")
26 @PropertySource("classpath:/application.properties")
27 static class DefaultPropertiesConfig {
28 }
29
30 @Configuration
31 @Profile("dev")
32 @PropertySource({ "classpath:/application.properties", "classpath:/application-dev.properties" })
33 static class DevPropertiesConfig {
34 }
35
36 @Configuration
37 @Profile("test")
38 @PropertySource({ "classpath:/application.properties", "classpath:/application-test.properties" })
39 static class TestPropertiesConfig {
40 }
41 }
MvcConfig
클래스는 Spring MVC 설정을 하는데 ViewResolver
, HandlerInterceptor
등등을 설정한다.
1 @Configuration
2 @EnableWebMvc
3 @ComponentScan(basePackages = "my.package.controller")
4 public class MvcConfig extends WebMvcConfigurerAdapter {
5
6 @Override
7 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
8 configurer.enable();
9 }
10
11 @Override
12 public void addInterceptors(InterceptorRegistry registry) {
13 registry.addInterceptor(myCustomerInterceptor())
14 .addPathPatterns("/includePattern/**").excludePathPatterns("/excludePattern/**");
15 // 그외 계속 추가
16 }
17
18 @Bean
19 public MyCustomInterceptor myCustomInterceptor() {
20 return new MyCustomInterceptor();
21 }
22
23 @Override
24 public void configureViewResolvers(ViewResolverRegistry registry) {
25 registry.viewResolver(viewResolver());
26 }
27
28 @Bean
29 public ViewResolver viewResolver() {
30 // ViewResolver 설정. JSP 나 JSTL, Velocity, Freemarker 등등
31 // ...
32 return resolver;
33 }
34
35 @Override
36 public void addResourceHandlers(ResourceHandlerRegistry registry) {
37 // 정적 파일을 서비스하는 경로 지정
38 registry.addResourceHandler("/static/**").addResourceLocations("/static/");
39 // ...
40 }
41
42 @Override
43 public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
44 // ExceptionHandler 지정
45 }
46
47 // 기타 등등 설정
48
49 }
여기서 컨트롤러 클래스는 모두 my.package.controller
패키지에 두도록 했다.