컴퓨터/spring

[etc] spring framework와 boot 차이와 동작

김크리 2021. 11. 16. 21:34

Spring framework

 Spring framework는 자바 생태계에서 대중적인 응용 프로그램 개발 프레임워크이다. 특징으로는 IOC, DI, 컨테이너, 생명주기 등 다양한 내용이 나올 수 있다. 

동작

 Spring framework로 개발된 web application은 servletContainer에 올라가 동작을 한다. spring framework는 servlet부터 datasource, bean 등 의존성 관리를 할 수 있으며, 해당 내용을 셋팅해주어야 한다.

web.xml을 통해 servlet을 container를 설정하고, servlet-context, application-xontext 등을 사용하여 application 내의 의존성을 설정한다.

 

Spring boot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

 Spring boot 는 Spring 기반 애플리케이션을 쉽게 만들 수 있는 프레임워크이다.  Tomcat, Jetty, Undertow등을 직접 포함하여 war 파일로 build 하여 배포할 필요가 없다. 

 

 스프링은 많은 기능을 가지고 있는 만큼 Session Factory, Entity Manager, Datasource 등 프로젝트를 구축하는데 많은 시간이 걸린다. Spring boot는 최소한의 기능으로 Spring MVC를 사용하여 기본 프로젝트를 셋팅할 수 있도록 지원한다. Spring boot는 자동설정(AutoConfiguration)을 지원하고 있다. AutoConfiguration에서는 개발에 필요한 dependency를 관리한다. 또한, Dispatcher Servlet, Datasource 등을 설정에 따라 자동으로 구축해준다.

동작

 Spring boot 는 main 메소드가 선언된 클래스를 기준으로 실행된다.

@SpringBootApplication은 jar 의존성에 의거하여 자동으로 스프링 application을 설정해준다. 초기에 spring boot 프로젝트를 생성하면 main 메소드가 있는 클래스를 기준으로 생성되어있다. 해당 어노테이션이 있는 곳에서 spring 기반의application이 시작된다.

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

SpringApplication.run(~

 Spring application을 재구성하고 실행한다. 

@SpringBootApplication에는 대표적으로 @SpriungBootConfiguration, @EnableautoConfiguration, @ComponentScan 이 설정되어있다. Spring framework에서 프로젝트를 구성할때 사용한 내용을 자동으로 구성해준다.

해당 프로젝트를 실행하면 자동으로 구성한 애플리케이션을 실행해준다.

	/**
	 * Static helper that can be used to run a {@link SpringApplication} from the
	 * specified source using default settings.
	 * @param primarySource the primary source to load
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return the running {@link ApplicationContext}
	 */
	public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
		return run(new Class<?>[] { primarySource }, args);
	}
    /**
	 * Run the Spring application, creating and refreshing a new
	 * {@link ApplicationContext}.
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return a running {@link ApplicationContext}
	 */
	public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
		ConfigurableApplicationContext context = null;
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting(bootstrapContext, this.mainApplicationClass);
		try {
    /*....*/

war 배포

 servlet 3.0 부터 application이 시작될때 servlet container를 구성할 수 있게 되었다. 내장 was 가 아닌 외부 was를 이용하여 배포하게 되면, ServletInitializer를 구현하고 main 메소드(@SpringBootApplication 가 있어, 설정 정보를 구성하는 시작점)에 해당 구현체를 override 해야한다.

이를 통해 서블릿 컨테이너에서 application을 시작할 때 application을 구성할 수 있다.

 

 

참고

https://spring.io/projects/spring-boot

https://steady-hello.tistory.com/53

https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/#howto-create-a-deployable-war-file