๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] ๋นˆ์„ ์„ค์ •ํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ• - XML, JAVA, Component Scan

by Leica 2020. 3. 1.
๋ฐ˜์‘ํ˜•

[Spring] ๋นˆ์„ ์„ค์ •ํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ• - XML, JAVA, Component Scan

 

์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ์— ํ•„์š”ํ•œ ์˜์กด์„ฑ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด spring boot ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ด๋‹ค.

 

์ฐธ๊ณ ๋กœ ์˜ˆ์ œ ์ฝ”๋“œ์—๋Š” ์ด ์˜์กด์„ฑ์ด ํ•„์š”ํ•˜๋‹ค.

 

Spring Initializr๋ฅผ ์„ ํƒํ•˜๊ณ  Next๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

Group, Artifact, Name์„ ์ž…๋ ฅํ•˜๊ณ  Next๋ฅผ ํด๋ฆญํ•œ๋‹ค.

Group์€ ํŒจํ‚ค์ง€๋ช…์„ ์ ์–ด์ค€๋‹ค. (๋ณดํ†ต ๋„๋ฉ”์ธ๋ช…์„ ๋’ค์ง‘์–ด์„œ..)

 

 

Web - Spring Web์„ ๋„ฃ๊ณ  Next๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

Finish๋ฅผ ํด๋ฆญํ•ด์„œ ์‹ ๊ทœ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์„ ์™„๋ฃŒํ•œ๋‹ค.

 

Spring boot ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

pom.xml์„ ์—ด์–ด๋ณด๋ฉด spring boot 2.2.5 ๋ฒ„์ „์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์šฐ์ธก์˜ Maven์„ ํด๋ฆญํ•˜์—ฌ maven ๋ทฐ๋ฅผ ์—ด์–ด๋ณด๋ฉด spring 5.2.4 ์˜์กด์„ฑ์ด ๋“ค์–ด์˜จ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Spring boot 2.2์—์„œ๋Š” spring 5.2 ์˜์กด์„ฑ์ด ๋“ค์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

BookRepository, BookService ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

BookService.java

1
2
3
4
5
6
7
8
public class BookService {
 
    BookRepository bookRepository;
 
    public void setBookRepository(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }
}
cs

BookService๊ฐ€ BookRepository๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

 

์ด์ œ ApplicationContext๋กœ ๋‘ ํด๋ž˜์Šค๋ฅผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด๋ณด์ž.

 

1. ๊ณ ์ „์ ์ธ ๋ฐฉ๋ฒ• - Spring Bean XML ์„ค์ • ํŒŒ์ผ

 

resources ํด๋”์— 'application.xml' ์ด๋ฆ„์œผ๋กœ Spring Config XMl ํŒŒ์ผ์„ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.

 

application.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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="bookService"
          class="com.atoz_develop.springapplicationcontext.BookService"/>
 
    <bean id="bookRepository"
          class="com.atoz_develop.springapplicationcontext.BookRepository"/>
    
</beans>
cs

<bean>์— ๋นˆ์„ ๋“ฑ๋กํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

id, class ์†์„ฑ์€ ํ•„์ˆ˜ ์†์„ฑ์ด๋‹ค.

id์— ๋นˆ์˜ id๋ฅผ, class์— ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•  ํด๋ž˜์Šค์˜ QName(ํŒจํ‚ค์ง€๋ช…์„ ํฌํ•จํ•œ ์ „์ฒด ์ด๋ฆ„)์„ ์ž…๋ ฅํ•œ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ ์„ค์ •ํ•œ ๋‚ด์šฉ์€ BookService์™€ BookRepository๋ฅผ ๋นˆ์œผ๋กœ ๋งŒ๋“ค๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

BookService๊ฐ€ BookRepository ๋นˆ์„ ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

application.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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="bookService"
          class="com.atoz_develop.springapplicationcontext.BookService">
        <property name="bookRepository" ref="bookRepository"/>
    </bean>
 
    <bean id="bookRepository"
          class="com.atoz_develop.springapplicationcontext.BookRepository"/>
 
</beans>
cs

๋นˆ ์ฃผ์ž…์€ ์ฃผ์ž…๋ฐ›์„ ๋นˆ์˜ <bean> ํƒœ๊ทธ ํ•˜์œ„์— <property> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

<property>์˜ name ์†์„ฑ์— ํ”„๋กœํผํ‹ฐ(setter) ์ด๋ฆ„์„, ref ์†์„ฑ์—๋Š” ์ฃผ์ž…ํ•  ๋นˆ์˜ id๋ฅผ ์ง€์ •ํ•œ๋‹ค.

 

๋“ฑ๋กํ•œ ๋นˆ์€ ApplicationContext๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

DemoApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import java.util.Arrays;
 
public class DemoApplication {
 
    public static void main(String[] args) {
 
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));
 
    }
}
cs

ClassPathXmlApplicationContext()์— classpath์˜ xmlํŒŒ์ผ์„ ๋„˜๊ฒจ ApplicationContext๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

ApplicationContext์˜ getBeanDefinitionNames()๋Š” IoC ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ๋นˆ์˜ id ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜จ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

ApplicationContext์˜ getBean()์œผ๋กœ ๋นˆ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

String ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋นˆ์˜ id๋ฅผ ๋„˜๊ธด๋‹ค. (bookService, bookRepository)

 

DemoApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import java.util.Arrays;
 
public class DemoApplication {
 
    public static void main(String[] args) {
 
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));
 
        BookService bookService = (BookService) context.getBean("bookService");
        System.out.println(bookService.bookRepository != null);
    }
}
cs

ApplicationContext์—์„œ bookService ๋นˆ์„ ๊บผ๋‚ด bookRepository๊ฐ€ null์ด ์•„๋‹Œ์ง€ ํ™•์ธํ•ด๋ด„์œผ๋กœ์จ ์˜์กด์„ฑ ์ฃผ์ž…์ด ์ œ๋Œ€๋กœ ๋๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

์•„๊นŒ ์ž‘์„ฑํ•œ xml ์„ค์ • ํŒŒ์ผ์— ์˜ํ•ด BookService๊ฐ€ BookRepository ๋นˆ์„ ์ฃผ์ž…๋ฐ›์€ ๊ฒƒ์ด๋‹ค.

์ด ๋ฐฉ๋ฒ•์€ ๋ชจ๋“  ๋นˆ์„ <bean>์œผ๋กœ ์ผ์ผ์ด ๋“ฑ๋กํ•ด์ค˜์•ผํ•ด์„œ ๊ต‰์žฅํžˆ ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

๊ทธ๋ž˜์„œ ๋‹ค์Œ์œผ๋กœ ๋“ฑ์žฅํ•œ ๊ฒƒ์ด component scan์ด๋‹ค.

application.xml์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•œ๋‹ค.

 

application.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:component-scan base-package="com.atoz_develop.springapplicationcontext"/>
 
</beans>
cs

์œ„ ์„ค์ •์€ base-package์— ์ง€์ •ํ•œ ํŒจํ‚ค์ง€์—์„œ๋ถ€ํ„ฐ ๋ชจ๋“  ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ scanningํ•ด์„œ ๋นˆ์„ ๋“ฑ๋กํ•˜๋„๋ก ํ•œ๋‹ค.

๋‹น์—ฐํžˆ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๊ฑด ์•„๋‹ˆ๊ณ  @Component ์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

@Component๋ฅผ ํ™•์žฅํ•œ ๋ช‡๊ฐ€์ง€ ์• ๋…ธํ…Œ์ด์…˜์ด ๋” ์žˆ๋Š”๋ฐ ๊ทธ ์ค‘ @Service, @Repository๊ฐ€ ์žˆ๋‹ค.

๊ทธ๋Ÿฌํ•œ @Component๋ฅผ ํ™•์žฅํ•œ ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.

 

BookService.java

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.stereotype.Service;
 
@Service
public class BookService {
 
    BookRepository bookRepository;
 
    public void setBookRepository(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }
}
cs

 

BookRepository.java

1
2
3
4
5
import org.springframework.stereotype.Repository;
 
@Repository
public class BookRepository {
}
cs

 

์ด๋ ‡๊ฒŒ BookService์™€ BookRepository ํด๋ž˜์Šค์— ๊ฐ๊ฐ @Service, @BookRepository ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์ค€๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๋‘ ํด๋ž˜์Šค๊ฐ€ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.

 

์˜์กด์„ฑ ์ฃผ์ž…์€ @Autowired ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

BookService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Service
public class BookService {
 
    @Autowired
    BookRepository bookRepository;
 
    public void setBookRepository(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }
}
cs

BookService.java์˜ BookRepository ๋ณ€์ˆ˜์— @Autowired๋ฅผ ๋ถ™์—ฌ์ฃผ์ž.

์ด์ œ ๋นˆ ๋“ฑ๋ก๊ณผ ์˜์กด์„ฑ ์ฃผ์ž…์„ ์œ„ํ•œ ์„ค์ •์ด ๋ชจ๋‘ ์™„๋ฃŒ๋˜์—ˆ๋‹ค.

main()์„ ์‹คํ–‰ํ•ด๋ณด๋ฉด ์ด์ „๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

application.xml์— ์ผ์ผ์ด ์„ค์ •ํ•˜๋Š” ๋Œ€์‹  ์• ๋…ธํ…Œ์ด์…˜์„ scanningํ•ด์„œ ๋นˆ์„ ๋“ฑ๋กํ•˜๊ณ  ์˜์กด์„ฑ ์ฃผ์ž…ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•œ ๊ฒƒ์ด๋‹ค.

 

๐Ÿ“ ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ๋นˆ ๋“ฑ๋ก ๋ฐ ์„ค์ •์€ spring 2.5๋ถ€ํ„ฐ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

 

2. JAVA ์„ค์ • ํŒŒ์ผ์˜ ์‚ฌ์šฉ

์œ„์—์„œ ์ž‘์„ฑํ•œ application.xml๊ณผ ๊ฐ™์€ XML ํŒŒ์ผ์ด ์•„๋‹Œ java๋กœ๋„ ์„ค์ •ํŒŒ์ผ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

ApplicationConfig ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.

 

ApplicationConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class ApplicationConfig {
 
    @Bean
    public BookRepository bookRepository() {
        return new BookRepository();
    }
 
    @Bean
    public BookService bookService() {
        BookService bookService = new BookService();
        bookService.setBookRepository(bookRepository());
        return bookService;
    }
}
cs

Java ์„ค์ • ํŒŒ์ผ์„ ๋งŒ๋“ค๋ ค๋ฉด @Configuration์„ ๋ถ™์—ฌ์ฃผ๊ณ  ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•  ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค.

๋ฉ”์†Œ๋“œ๋ช…์ด ๋นˆ id, ๋ฆฌํ„ด ํƒ€์ž…์ด ๋นˆ ํƒ€์ž…, ๋ฆฌํ„ดํ•œ ๊ฐ์ฒด๊ฐ€ ๋นˆ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ๋œ๋‹ค.

์˜์กด์„ฑ์€ ์ง์ ‘ setter๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ฃผ์ž…ํ•ด์ค€๋‹ค.

 

DemoApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import java.util.Arrays;
 
public class DemoApplication {
 
    public static void main(String[] args) {
 
//        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));
 
        BookService bookService = (BookService) context.getBean("bookService");
        System.out.println(bookService.bookRepository != null);
    }
}
cs

Java๋กœ ๋งŒ๋“  ์„ค์ • ํŒŒ์ผ์€ AnnotationConfigApplicationContext()์— class๋ฅผ ๋„˜๊ฒจ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“ AnnotationConfigApplicationContext๋ž€?

์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ๋นˆ config๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ApplicationContext ๊ตฌํ˜„์ฒด์ด๋‹ค.

 

๊ธฐ์กด์— ๋ถ™์–ด์žˆ๋˜ @Service, @Repository, @Autowired๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‹คํ–‰ํ•ด๋ณด์ž.

์—ญ์‹œ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๋‹ค.

 

Java ์„ค์ • ํŒŒ์ผ์—์„œ ์ง์ ‘ setter๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  @Autowired๋ฅผ ๋ถ™์—ฌ์„œ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

ApplicationConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class ApplicationConfig {
 
    @Bean
    public BookRepository bookRepository() {
        return new BookRepository();
    }
 
    @Bean
    public BookService bookService() {
        return new BookService();
    }
}
cs

 

BookService.java

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.beans.factory.annotation.Autowired;
 
public class BookService {
 
    @Autowired
    BookRepository bookRepository;
 
    public void setBookRepository(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }
}
cs

 

Java ์„ค์ • ํŒŒ์ผ์—์„œ ์ง์ ‘ setter๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์˜์กด ๊ด€๊ณ„๋ฅผ ์—ฎ์–ด์ฃผ์ง€ ์•Š์•„๋„ @Autowired ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์ฃผ๋ฉด ์˜์กด์„ฑ ์ฃผ์ž…์ด ๋œ๋‹ค.

 

3. JAVA ์„ค์ • ํŒŒ์ผ + Component Scan

์œ„ ๋ฐฉ๋ฒ•์€ XML ์„ค์ • ํŒŒ์ผ์—์„œ component scan์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

๊ทธ๋ž˜์„œ Java ์„ค์ •ํŒŒ์ผ์—์„œ๋„ component scan์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ApplicationConfig.java

1
2
3
4
5
6
7
8
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
 
@Configuration
//@ComponentScan(basePackages = "com.atoz_develop.springapplicationcontext")
@ComponentScan(basePackageClasses = ApplicationConfig.class)    // ๋” type safeํ•œ ๋ฐฉ๋ฒ•
public class ApplicationConfig {
}
cs

 

Java ์„ค์ • ํŒŒ์ผ ํด๋ž˜์Šค์— @ComponentScan ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ๋‹ค.

Scan์„ ์‹œ์ž‘ํ•˜๋Š” ํŒจํ‚ค์ง€๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ basePackages์™€ basePackagesClasses์˜ ๋‘ ๊ฐ€์ง€ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ basePackages์—๋Š” ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ๋ฌธ์ž์—ด๋กœ, basePackagesClasses์—๋Š” scan์„ ์‹œ์ž‘ํ•  ์œ„์น˜์˜ ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•œ๋‹ค.

basePackagesClasses๊ฐ€ ๋” type safeํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.

 

๋‹ค์‹œ BookService์™€ BookRepository์— @Service, @Repository ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์ค€๋‹ค.

 

์ด๋ ‡๊ฒŒ ์„ค์ •ํ•ด์ฃผ๋ฉด XML ์„ค์ • ํŒŒ์ผ์—์„œ <context:component-scan...>์„ ์„ค์ •ํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ java ์„ค์ • ํŒŒ์ผ์— @ComponentScan ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์„œ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด spring boot ๊ธฐ๋ฐ˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฐฉ๋ฒ•์ด๋‹ค.

 

์—ญ์‹œ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๋‹ค.

 

๐Ÿ“ ์ง€๊ธˆ๊นŒ์ง€ ApplicationContext๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ spring์€ ApplicationContext๋ฅผ ์•Œ์•„์„œ ๋งŒ๋“ค์–ด์ค€๋‹ค.
๋˜ํ•œ java ์„ค์ • ํŒŒ์ผ๋กœ ๋งŒ๋“ค์—ˆ๋˜ ApplicationConfig.java ๊ฐ™์€ ํŒŒ์ผ๋„ spring boot๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋”ฐ๋กœ ์ƒ์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

References

์ธํ”„๋Ÿฐ - ๋ฐฑ๊ธฐ์„ ๋‹˜์˜ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ํ•ต์‹ฌ ๊ธฐ์ˆ 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€