D
D
ddddd tttt2017-01-03 11:33:48
Java
ddddd tttt, 2017-01-03 11:33:48

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>

Why error?
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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Lopatin, 2017-01-03
@pashaa

Invalid path specified. What is in src is added to the classpath root. It should be like this:

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();
    }}

If you add resources to the classpath at all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question