Trouble-Shooting

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์‹คํ–‰์‹œ ApplicationContext ๋กœ๋“œ ์‹คํŒจ ์—๋Ÿฌ

์ฑ”๐Ÿป 2024. 1. 24. 19:55

์ž‘์„ฑ์ผ์ž: 2023๋…„ 7์›” 17์ผ

โœ”๊ฒฐ๋ก 


WebMvcConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์— @Configuration, @EnableWebMvc ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ  root-context.xml์— component-scan์ด ๋ชจ๋“  ํŒจํ‚ค์ง€๋กœ ์„ค์ •์ด ๋˜์–ด์žˆ๋‹ค๋ฉด WebMvcConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ฅผ ์Šค์บ” ์ œ์™ธํ•ด์ฃผ๋ฉด๋œ๋‹ค.

<context:component-scan base-package="com.teamProject.syusyu" />

๐Ÿ‘€๋ฌธ์ œ ์ •์˜

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์‹คํ–‰ ์‹œ java.lang.IllegalStateException: Failed to load ApplicationContext ์—๋Ÿฌ ๋ฐœ์ƒ

โ“์›์ธ

WebConfig.java์—์„œ @EnableWebMvc ์–ด๋…ธํ…Œ์ด์…˜์ด ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ•„์š”ํ•œ ์„ค์ •์„ ํ™œ์„ฑํ™”ํ•˜๋ ค ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ–ˆ๋‹ค. ์‹ค์ œ๋กœ ์ด ์„ค์ •์€ ์›น์„œ๋ฒ„(ํ†ฐ์บฃ)๊ฐ€ ์‹คํ–‰์ค‘์ธ ํ™˜๊ฒฝ์—์„œ๋งŒ ํ•„์š”ํ•˜๋ฉฐ, ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ๋Š” ์›น์„œ๋ฒ„(ํ†ฐ์บฃ)๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/alertPopup").setViewName("/fos/common/alertPopup");
        registry.addViewController("/dlvAddrPopup").setViewName(ViewPath.FOS_POPUP + "dlvAddrPopup");
        registry.addViewController("/couponPopup").setViewName(ViewPath.FOS_POPUP + "couponPopup");
        registry.addViewController("/orderComplete").setViewName(ViewPath.FOS_ORDER + "orderComplete");
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/root-context.xml"})
public class CartProdDAOImplTest extends TestCase {
<context:component-scan base-package="com.teamProject.syusyu" />

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์‹คํ–‰ ์‹œ root-context.xml์ด ๋กœ๋“œ๋˜๊ณ , context:component-scan์ด com.teamProject.syusyu ํŒจํ‚ค์ง€์™€ ๊ทธ ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ ์Šค์บ”ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ, WebConfig.java๊ฐ€ @Configuration๊ณผ @EnableWebMvc ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ธํ•ด ๋กœ๋“œ๋˜์–ด ์›น ๊ด€๋ จ ์„ค์ •์ด ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ๋„ ์ ์šฉ๋˜์–ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

โœ”ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

1. ์›น ํ™˜๊ฒฝ ์‹œ๋ฎฌ๋ ˆ์ด์…˜

ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค์— @WebAppConfiguration ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด, ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ๋„ ์›น ํ™˜๊ฒฝ์„ ๊ฐ€์งœ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/root-context.xml"})
public class CartProdDAOImplTest extends TestCase {
    //...
}

2. ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ WebConfig.java ์Šค์บ” ์ œ์™ธ

context:component-scan์— exclude-filter๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด, ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ WebConfig.java๊ฐ€ ์Šค์บ”๋˜์ง€ ์•Š๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

<context:component-scan base-package="com.teamProject.syusyu">
    <context:exclude-filter type="assignable" expression="com.teamProject.syusyu.common.WebConfig"/>
</context:component-scan>


Uploaded by N2T