250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- JavaScript
- Tomcat
- MSSQL
- IntelliJ
- Source
- 설정
- jpa
- ubuntu
- Spring Boot
- Python
- spring
- Eclipse
- maven
- STS
- 오픈소스
- MySQL
- JDBC
- myBatis
- error
- git
- PostgreSQL
- oracle
- SpringBoot
- Thymeleaf
- Core Java
- Open Source
- Docker
- AJAX
- 문서
- Exception
Archives
- Today
- Total
헤르메스 LIFE
[OpenSource] 토비 Spring 3 - ConfigurableDispatcherServlet 본문
728x90
토비 Spring 3 - ConfigurableDispatcherServlet.java Sample
package
springbook.learningtest.spring.web;
import
java.io.IOException;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.beans.BeansException;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory;
import
org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import
org.springframework.util.ClassUtils;
import
org.springframework.web.context.WebApplicationContext;
import
org.springframework.web.context.support.AbstractRefreshableWebApplicationContext;
import
org.springframework.web.servlet.DispatcherServlet;
import
org.springframework.web.servlet.ModelAndView;
@SuppressWarnings
(
"serial"
)
public
class
ConfigurableDispatcherServlet
extends
DispatcherServlet {
private
Class<?>[] classes;
private
String[] locations;
private
ModelAndView modelAndView;
public
ConfigurableDispatcherServlet(String[] locations) {
this
.locations = locations;
}
public
ConfigurableDispatcherServlet(Class<?> ...classes) {
this
.classes = classes;
}
public
void
setLocations(String ...locations) {
this
.locations = locations;
}
@SuppressWarnings
(
"rawtypes"
)
public
void
setRelativeLocations(Class clazz, String ...relativeLocations) {
String[] locations =
new
String[relativeLocations.length];
String currentPath = ClassUtils.classPackageAsResourcePath(clazz) +
"/"
;
for
(
int
i =
0
; i < relativeLocations.length; i++) {
locations[i] = currentPath + relativeLocations[i];
}
this
.setLocations(locations);
}
public
void
setClasses(Class<?> ...classes) {
this
.classes = classes;
}
public
void
service(ServletRequest req, ServletResponse res)
throws
ServletException, IOException {
modelAndView =
null
;
super
.service(req, res);
}
protected
WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
AbstractRefreshableWebApplicationContext wac =
new
AbstractRefreshableWebApplicationContext() {
protected
void
loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
throws
BeansException, IOException {
if
(locations !=
null
) {
XmlBeanDefinitionReader xmlReader =
new
XmlBeanDefinitionReader(beanFactory);
xmlReader.loadBeanDefinitions(locations);
}
if
(classes !=
null
) {
AnnotatedBeanDefinitionReader reader =
new
AnnotatedBeanDefinitionReader(beanFactory);
reader.register(classes);
}
}
};
wac.setServletContext(getServletContext());
wac.setServletConfig(getServletConfig());
wac.refresh();
return
wac;
}
protected
void
render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response)
throws
Exception {
this
.modelAndView = mv;
super
.render(mv, request, response);
}
public
ModelAndView getModelAndView() {
return
modelAndView;
}
}
728x90
'Core Java' 카테고리의 다른 글
Web 개발환경 설정 (0) | 2013.06.24 |
---|---|
[Core Java] Map or List에 값이 포함되어 있는지 확인 (0) | 2012.07.03 |
[Source] Common Java Cookbook (0) | 2012.06.20 |
[Source] EncodingFilter.java (0) | 2012.05.15 |
[Source] A test of the Thin driver for an application (0) | 2011.11.07 |