์์ฑ์ผ์: 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