Spring Boot는 Spring 애플리케이션을 빠르게 시작할 수 있도록 돕는 Opinionated(선호 기반) 접근 방식을 제공합니다.
application.properties
와 클래스패스를 기반으로 필요한 빈(bean)을 자동 등록myapp.jar
로 실행 가능# 실행 예시
java -jar mycoolapp.jar
기존 Spring MVC나 JPA 설정을 반복적으로 작성하던 것을 Spring Boot가 자동화합니다.
spring-webmvc
가 있으면 DispatcherServlet
자동 등록DataSource
자동 생성<!-- 기존 web.xml에 작성하던 DispatcherServlet 설정 예 -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Spring Boot에서는 다음과 같이 간단히 애너테이션만 추가하면 자동 구성이 활성화됩니다.
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Spring Boot 스타터(starter)는 자주 사용하는 라이브러리를 호환 가능한 버전으로 묶어 제공합니다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-data-jpa
, spring-boot-starter-security
등)main()
실행 시 내장 Tomcat 구동jar
로 배포 가능 (WAR 불필요)<packaging>jar</packaging>
애플리케이션 모니터링·관리용 REST 엔드포인트를 자동 노출합니다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
엔드포인트 | 설명 |
---|---|
/actuator/health | 애플리케이션 상태 확인 |
/actuator/beans | 등록된 빈 목록 확인 |
/actuator/mappings | URL 매핑 정보 확인 |
/actuator/metrics | 메트릭 정보 확인 |
# application.properties 예시
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
info.app.name=MyApp
info.app.version=1.0.0
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="secret"/>
</bean>
@Service
public class UserService {
@Autowired
private UserDao userDao;
}
@Repository
public class UserDao {
@Autowired
private DataSource dataSource;
}
@Configuration
public class AppConfig {
@Bean
public DataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/test");
ds.setUsername("root");
ds.setPassword("secret");
return ds;
}
@Bean
public UserDao userDao(DataSource dataSource) {
return new JdbcUserDao(dataSource);
}
@Bean
public UserService userService(UserDao dao) {
return new UserService(dao);
}
}