1. 개요
다음과 같은 환경에서 Spring Framework 4 기반의 Hello World 프로그램을 작성하고 빌드하여 실행할 수 있도록 안내합니다.
도구 | 버전 | 설명 |
---|---|---|
Windows | 10 | 운영체제 |
JDK | 1.6 | Java 컴파일러 및 실행 환경 |
IntelliJ IDEA | 2021.2.2 (Community Edition) | 통합 개발 환경 |
Maven | 3.2.5 (JDK 1.6에서 동작하는 마지막 버전) | 빌드 도구 |
Spring Framework | 4.3.30.RELEASE | 응용 프레임워크 |
2. 준비
아래 블로그 글을 참고하여 프로젝트를 생성합니다.
3. Spring Framework 사용
3.1. pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springframework4-study</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>6</maven.compiler.source>
<maven.compiler.target>6</maven.compiler.target>
<spring.version>4.3.30.RELEASE</spring.version>
</properties>
</project>
3.2. applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="serviceExecutor" class="com.example.ServiceExecutor">
</bean>
</beans>
3.3. ServiceExecutor.java
package com.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ServiceExecutor {
private static Logger LOGGER = LoggerFactory.getLogger(ServiceExecutor.class);
private boolean keepRunning = true;
public void start() throws Exception {
LOGGER.info("Service is starting...");
LOGGER.info("Service started.");
int count = 0;
while (keepRunning) {
Thread.sleep(1000);
LOGGER.info("count " + ++count);
}
}
public void stop() throws Exception {
LOGGER.info("Service is stopping...");
keepRunning = false;
LOGGER.info("Service stopped.");
}
}
3.4. HelloWorld.java
package com.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {
private static Logger LOGGER = LoggerFactory.getLogger(HelloWorld.class);
private BeanFactory beanFactory;
protected HelloWorld() {
initBeanFactory();
}
protected void initBeanFactory() {
beanFactory = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml"
);
}
protected void start() {
final ServiceExecutor serviceExecutor = beanFactory.getBean("serviceExecutor", ServiceExecutor.class);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
LOGGER.info("Application is stopping...");
serviceExecutor.stop();
LOGGER.info("Application stopped.");
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
});
try {
LOGGER.info("Application is starting...");
serviceExecutor.start();
LOGGER.info("Application started.");
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
System.exit(1);
}
}
public static void main(String[] args) {
LOGGER.info("Hello World!!!");
HelloWorld executor = new HelloWorld();
executor.start();
}
}
4. 참고 자료
Written with StackEdit.
댓글 없음:
댓글 쓰기