4. 카페 메뉴얼관리 ( 스프링 초기 설정 코드 )
2024. 3. 3. 21:15ㆍ언어/java
728x90
SpringStarter 클래스 에 대해서 설명 하겠다.
이 코드는 Spring Framework를 사용하는 웹 어플리케이션의 초기화와 설정을 담당한다.
package spring;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
/**
* SpringFramework Context 구동 Class
*
* @author 하석형
* @since 2023.08.14
*/
public class SpringStarter implements WebApplicationInitializer {
/**
* SpringFramework Context 구동 시, 동작하는 이벤트 기능 (web.xml의 기능을 대체함)
*
* @author 하석형
* @since 2022.08.14
*/
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(
spring.config.RootContextConfig.class,//Root Context
spring.config.DataSourceContextConfig.class,//DataSource Context
spring.config.MybatisContextConfig.class//Mybatis Context
);
servletContext.addListener(new ContextLoaderListener(rootContext));
// Web(Servlet) Context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(spring.config.servlet.WebContextConfig.class);
// Dispatcher Servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
// Filter
FilterRegistration.Dynamic filter = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
filter.setInitParameter("encoding","utf-8");
filter.addMappingForServletNames(null,false,"dispatcher");
}
}
onStartup 이 함수는 웹 이 시작될때 자동으로 실행된다. 여러가지 설정이 들어가있다.
AnnotationConfigWebApplicationContext 이 함수는 데이터베이스 연결 정보나 웹 관련 설정을 담고 있다.
ContextLoaderListener 이 함수는 어플이 시작될 때 등록된 설정 정보를 읽어서 스프링이 사용 할 수 있도록 한다.
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
이 부분은 클라이언트( 프론트엔드 ) 의 요청을 적절하게 컨트롤러로 보내 주는 역할을 한다.
CharacterEncodingFilter 이 필터는 문자 인코딩을 처리 하는 역할을 한다. /한국/
자세한 설명
전반적인 설정에 대해서 프로젝트를 동작하는데
spring.config.RootContextConfig.class
이 부분 같은 경우에는 Bean 설정 해준다. 스프링에서는 객체를 bean이라고 부른다.
빈은 스프링이 관리하는 객체로 필요할때 스프링이 알아서 생성하고 제공 해준다.
728x90
'언어 > java' 카테고리의 다른 글
프로젝트 2 날라가기전에 복구파일 (0) | 2024.01.18 |
---|---|
프로젝트 1 날라가기전에 복구 파일 (0) | 2024.01.18 |
1. 자바스프링 정리 (0) | 2024.01.16 |
카카오 지도 api / 쿼리문 (0) | 2024.01.12 |
8. CRUD 연동 연습 (1) | 2024.01.12 |