Spring μΈν 리μ μ΄ Service, Common, Batch λͺ¨λ λΆλ¦¬νκΈ° (Maven)
π λ°°κ²½
ν μ΄ νλ‘μ νΈμ νμ¬ νλ‘μ νΈμμ Spring, IntelliJ, Mavenμ κΈ°λ°μΌλ‘ λͺ¨λ λΆλ¦¬λ₯Ό νλ©΄μ μκ²λ λͺ¨λ λΆλ¦¬ λ°©λ²κ³Ό λͺ¨λ λΆλ¦¬ κ³Όμ μμ κ²ͺμ μ μλ λ¬Έμ λ₯Ό ν΄κ²°νλ μ€μ λ°©λ²μ λν΄ κ³΅μ νκ³ μ νλ€.
π λͺ¨λ λΆλ¦¬λ₯Ό ν΄μΌ νλ μ΄μ ?
- μμ‘΄μ± μ΅μν
μλ₯Ό λ€μ΄, λμΌν λͺ¨λμμ λ°°μΉ μλΉμ€λ₯Ό κ°λ°ν λλ μ€νλ§ μν리ν°κ° νμ μμ μ μλλ°, μ΄λ λμΌν λͺ¨λμ΄λΌλ μ΄μ λ‘ κ°μ λ‘ μ€νλ§ μνλ¦¬ν° μμ‘΄μ±μ ν¬ν¨νκ² λλ©΄μ κ°λ°/ν μ€νΈ κ³Όμ μμ μνλ¦¬ν° λλ¬Έμ μν₯μ λ°μ μ μλ€.
λͺ¨λμ λΆλ¦¬νλ©΄ νμν μ΅μ μμ‘΄μ±λ§ κ΄λ¦¬ν¨μΌλ‘μ¨ μ΄λ¬ν μμ‘΄μ± κ΄λ ¨ λ¬Έμ /μν₯μ κ³ λ €νμ§ μμλ λλ€.
- μ μ§λ³΄μ
νΉμ λͺ¨λμ μ₯μ κ° λ°μνλ€λ©΄ ν΄λΉ λͺ¨λλ§ νμΈνλ©΄ λκΈ° λλ¬Έμ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μ¬μμ§λ€. λν, μ₯μ λ‘ ν΄λΉ λͺ¨λμ΄ κ°μμ€λ½κ² μ’ λ£λλ€κ³ ν΄λ λ€λ₯Έ μλΉμ€ λͺ¨λλ€μ μν₯μ λ°μ§ μκΈ° λλ¬Έμ νΌν΄λ₯Ό μ΅μνν μ μλ€λ μ₯μ λ μλ€.
0. μ€λΉ
ν¬μ€ν μ μ¬μ©λ νκ²½μ λ€μκ³Ό κ°λ€.
- JDK17
- IntelliJ
- Maven
- Spring Boot 3.1.3
κ°μ₯ ννκ³ μμλ‘ μ μ νλ€κ³ μκ°νλ μλΉμ€, 곡ν΅, λ°°μΉ λͺ¨λ 3κ°λ‘ λΆλ¦¬ν΄λ³΄λ €κ³ νλ€.
1. νλ‘μ νΈ μμ±
2. λͺ¨λ μμ±νκΈ°
File > New > Module > New Moduleμ μ κ·Όν΄μ service, batch, commonμ μμ±νλ€.
λͺ¨λμ λͺ¨λ μμ±νλ©΄ μλμ κ°μ κ΅¬μ‘°κ° λλ€.
3. κ° λͺ¨λμ μμ‘΄μ± μΆκ°νκΈ°
κ° λͺ¨λμ νμν μμ‘΄μ±μ pom.xmlμ μΆκ°νλ€.
μ΄λ¦ | μΆκ°ν λͺ¨λ |
service | web, security, security-test, common |
batch | batch, batch-test, common |
common | data-jpa, mariadb, h2 |
parent | lombok, starter-test |
lombokκ³Ό starter-testλ μ΄λμμλ μΈ μ μκΈ° λλ¬Έμ parent λͺ¨λμ μμ‘΄μ±μ μΆκ°νλ€.
4. νλ¬κ·ΈμΈ μΆκ°νκΈ°
spring-boot-maven-plugin νλ¬κ·ΈμΈμ μΆκ°νλ€.
(ν΄λΉ νλ¬κ·ΈμΈμ ν¨ν€μ§ ν λ executable jar νμΌλ‘ ꡬ쑰λ₯Ό λ³κ²½ν΄ μ£Όλ μν μ νλ€.)
spring-boot-maven-pluginλ λ©μΈ ν΄λμ€κ° ν¬ν¨λ λͺ¨λμλ§ μΆκ°ν΄μΌ νλ―λ‘, λΌμ΄λΈλ¬λ¦¬ μν μ νλ common λͺ¨λ μΈμ service, batch λͺ¨λμ λ³λλ‘ μΆκ°νλ€.
(commonμλ ν¬ν¨νλ©΄ main ν΄λμ€λ₯Ό μ°Ύμ§ λͺ»ν΄ ν¨ν€μ§μ μ€ν¨νκ² λλ€.)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
νλ¬κ·ΈμΈκΉμ§ μΆκ°κ° μλ£λλ©΄, κΈ°λ³Έμ μΈ λͺ¨λ λΆλ¦¬λ μ΄κ±Έλ‘ λμ΄ λλ€.
νμ§λ§ μ΄λλ‘ μ ν리μΌμ΄μ μ μ€ννλ €κ³ νλ©΄ μλ¬κ° λ°μν μλ°μ μλ€.
μ΄μ μλΉμ€ λͺ¨λμμ JPAμ common λͺ¨λμ μ€μΊνλ μ€μ λ°©λ²μ λν΄μ μμ보μ.
5. μ€μ
serviceμμλ common λͺ¨λμ μλ ν΄λμ€λ€μ ν¨κ» μ¬μ©ν κ²μ΄λ―λ‘ λλ¬Έμ, common λͺ¨λ ν¨ν€μ§κΉμ§ ν¬ν¨ν΄μ μ»΄ν¬λνΈ μ€μΊμ ν΄μΌ νλ€.
@SpringBootApplication(scanBasePackages = {"com.example"})
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringApplication.class, args);
}
}
service λͺ¨λμ ν¨ν€μ§ κ²½λ‘λ com.example.serviceμ΄κ³ , common λͺ¨λμ ν¨ν€μ§ κ²½λ‘λ com.example.common μ΄λ―λ‘ μμ ν¨ν€μ§ κ²½λ‘μΈ com.example κΉμ§λ§ μ λ ₯νλ©΄ κ΄λ ¨ λͺ¨λ μ€μ μ κ°μ§κ³ μ¬ μ μλ€. λ§μ½, ν¨ν€μ§ κ²½λ‘κ° κ³΅ν΅λ λΆλΆμ΄ μκ±°λ λ€λ₯΄κ² λλ μΌ νλ λ± λ€λ₯Έ κ²½μ°κ° μλ€λ©΄ νμμ λ°λΌ μΆκ°μ μΌλ‘ κ²½λ‘λ₯Ό μμ±νλ©΄ λλ€.
νμ§λ§ μ»΄ν¬λνΈ μ€μΊμ νμμλ, JPAλ₯Ό μ¬μ©νλ κ²½μ°λ μ ν리μΌμ΄μ μ μ€νν λ Repository λΆλΆμμ ν΄λΉ Beanμ μ°Ύμ§ λͺ»νλ€λ λ¬Έμ κ° λ°μν μ μλ€.
λ³΄ν΅ JPAλ₯Ό μ¬μ©νλ€λ©΄ common λͺ¨λμ Entity, Repository λ± JPAμ κ΄λ ¨λ ν΄λμ€λ₯Ό μμ±ν ν λ°, μ΄λ€μ μ¬μ©νκΈ° μν΄μλ λ³λ μ€μ μ΄ νμνλ€.
@Configuration
@EntityScan(basePackages = "com.example.common.entity")
@EnableJpaRepositories(basePackages = "com.example.common.repository")
@EnableJpaAuditing
public class JpaConfig {
}
Spring Data JPAμμ μ§μνλ @EntityScan, @EnableJpaRepositories μ΄λ Έν μ΄μ μ μ¬μ©νμ¬ μν°ν° ν¨ν€μ§μ Repository ν¨ν€μ§ κ²½λ‘λ₯Ό λ°λ‘ μ€μ νμΌμ μμ±ν΄μΌ service λͺ¨λμμ μ¬μ©ν μ μλ€.
λ§μ½ BaseEntity λ±μ κ³΅ν΅ μν°ν°λ₯Ό μ¬μ©νλ κ²½μ°λΌλ©΄ @EnableJpaAuditingλ ν¨κ» μ¬μ©νμ¬ JPA κ΄λ ¨ μ€μ μ νλμ ν΄λμ€μ μμ±νλ©΄ κ΄λ¦¬νκΈ° μ’λ€.