사용 환경
- Windows 10
- Java 1.8
- Maven 3.8.2
- Spring 5.0.6
Hello 프로젝트 따라하기
-
프로젝트 생성
>mvn archetype:generate -DgroupId=trvoid -DartifactId=hello -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
생성된 프로젝트 폴더 구조는 아래와 같습니다.
hello |-src |-main |-java |-trvoid |-App.java |-test |-java |-trvoid |-AppTest.java |-pom.xml
-
의존성 추가
pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.6.RELEASE</version> </dependency>
-
서비스 추가
src/main/java/trvoid/HelloManager.java
package trvoid; public interface HelloManager { public String getServiceName(); }
src/main/java/trvoid/HelloManagerImpl.java
package trvoid; public class HelloManagerImpl implements HelloManager { @Override public String getServiceName() { return "My Hello Service"; } }
-
빈 추가
src/main/java/trvoid/AppConfig.java
package trvoid; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean(name="helloService") public HelloManager getHelloManager() { return new HelloManagerImpl(); } }
-
메인 클래스 수정
src/main/java/trvoid/App.java
package trvoid; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); HelloManager hello = (HelloManager)context.getBean("helloService"); System.out.println(hello.getServiceName()); } }
-
컴파일
>mvn compile
-
실행
>mvn exec:java -Dexec.mainClass=trvoid.App
실행 결과
[INFO] Scanning for projects... [INFO] [INFO] ----------------------------< trvoid:hello >---------------------------- [INFO] Building hello 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ hello --- 9월 07, 2021 10:30:09 오전 org.springframework.context.support.AbstractApplicationContext prepareRefresh 정보: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2695bde4: startup date [Tue Sep 07 10:30:09 KST 2021]; root of context hierarchy My Hello Service [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.773 s [INFO] Finished at: 2021-09-07T10:30:09+09:00 [INFO] ------------------------------------------------------------------------
Written with StackEdit.
댓글 없음:
댓글 쓰기