@pashaa

ClassPathXml ApplicationContext?

public class HelloWorldSpringDI  {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("src/resources/app-context.xml");
        MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
        mr.render();
    }}

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="provider"
          class="HelloWorldМesProvider"/>
    <bean id="renderer"
          class="StandardOutMesRenderer"
          p:messageProvider-ref="provider"/>
</beans>

Почему ошибка ?
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/resources/app-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [src/resources/app-context.xml] cannot be opened because it does not exist
1a9aecbaab2c4a51bb5df1baff4141f6.PNG
  • Вопрос задан
  • 653 просмотра
Решения вопроса 1
Указан неверный путь. То, что находится в src добавляется в корень classpath. Должно быть так:
public class HelloWorldSpringDI  {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("resources/app-context.xml");
        MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
        mr.render();
    }}

Если вы вообще добавляете resources в classpath
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы