사용 환경
- Windows 10
- Java 1.8
- Gradle 7.2
- Spring 5.0.6
Hello 프로젝트 따라하기
-
프로젝트 생성
>gradle init
선택 항목
Starting a Gradle Daemon (subsequent builds will be faster) Select type of project to generate: 1: basic 2: application 3: library 4: Gradle plugin Enter selection (default: basic) [1..4] 2 Select implementation language: 1: C++ 2: Groovy 3: Java 4: Kotlin 5: Scala 6: Swift Enter selection (default: Java) [1..6] 3 Split functionality across multiple subprojects?: 1: no - only one application project 2: yes - application and library projects Enter selection (default: no - only one application project) [1..2] 1 Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Groovy) [1..2] 1 Select test framework: 1: JUnit 4 2: TestNG 3: Spock 4: JUnit Jupiter Enter selection (default: JUnit Jupiter) [1..4] 1 Project name (default: hello-gradle): hello Source package (default: hello): trvoid > Task :init Get more help with your project: https://docs.gradle.org/7.2/samples/sample_building_java_applications.html
생성된 프로젝트 폴더 구조는 아래와 같습니다.
hello |-app |-src |-main |-java |-trvoid |-App.java |-resources |-test |-java |-trvoid |-AppTest.java |-resources |-build.gradle |-gradle |-wrapper |-gradle-wrapper.jar |-gradle-wrapper.properties |-gradlew |-gradlew.bat |-settings.gradle
-
의존성 추가
app/build.gradle
implementation 'org.springframework:spring-core:5.0.6.RELEASE' implementation 'org.springframework:spring-context:5.0.6.RELEASE'
-
서비스 추가
app/src/main/java/trvoid/HelloManager.java
package trvoid; public interface HelloManager { public String getServiceName(); }
app/src/main/java/trvoid/HelloManagerImpl.java
package trvoid; public class HelloManagerImpl implements HelloManager { @Override public String getServiceName() { return "My Hello Service"; } }
-
빈 추가
app/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(); } }
-
메인 클래스 수정
app/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()); } }
-
빌드
>gradlew build
-
실행
>gradlew run
실행 결과
> Task :app:run 9월 07, 2021 11:29:27 오전 org.springframework.context.support.AbstractApplicationContext prepareRefresh 정보: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@45ff54e6: startup date [Tue Sep 07 11:29:27 KST 2021]; root of context hierarchy My Hello Service BUILD SUCCESSFUL in 6s 2 actionable tasks: 2 executed
Written with StackEdit.
댓글 없음:
댓글 쓰기