Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties;

SEOP·2023년 4월 9일
0

ERROR

Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/config/jdbc/jdbc.properties] cannot be opened because it does not exist
	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:89)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:283)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:163)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:106)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:249)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
	... 25 more

jdbc 연결 정보를 적어 놓은 jdbc.properties를 찾지 못하는 문제 발생.

나 같은 경우 현재 src>main>webapp>WEB-INF>config>jdbc>jdbc.properties 위치에 있다.
jdbc.properties 파일 경로를 지정해 두었던 xml파일 열어준다.
mybatis-context.xml 혹은 conetext-datasource.xml 확인

이번 프로젝트 같은 경우엔 spring폴더와 jdbc.properties파일을 INF-WEB폴더 아래에 두었기 때문에 src/main/resources 아래에 위치해두었을 때랑은 다르게 설정을 해줘야한다.

  <bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <value>/WEB-INF/config/jdbc/jdbc.properties</value>
    </property>
  </bean>

이 코드를 아래의 코드로 변경해주면 오류가 해결된다.

  <bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <value>file:src/main/webapp/WEB-INF/config/jdbc/jdbc.properties</value>
    </property>
  </bean>

src/main/resourecs 아래에 위치해 있을 경우?


앞서 언급했던 것처럼
src/main/resources 아래에 파일이 존재하면 코드 작성을 다르게 해주어야 한다.

  <bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <value>classpath:jdbc.properties</value>
    </property>
  </bean>

추가 내용

나는 MAC 환경이고, 팀원은 Windows 환경
나의 경우(MAC) 아래 코드가 properties 파일을 불러오지 못했고,

  <bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <value>/WEB-INF/config/jdbc/jdbc.properties</value>
    </property>
  </bean>

팀원의 경우(Windows) 아래의 코드가 properties 파일을 불러오지 못했다.

  <bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <value>file:src/main/webapp/WEB-INF/config/jdbc/jdbc.properties</value>
    </property>
  </bean>

그래서 결국 properties 파일을 src/main/resources 아래에 위치하여

  <bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <value>classpath:jdbc.properties</value>
    </property>
  </bean>

이렇게 설정하니 MAC/Windows 둘 다 잘 불러온다.

profile
응애 나 애기 개발자

0개의 댓글