简体   繁体   English

如何在 Intellij 中使用 bean 创建基本的 spring 应用程序

[英]How to create a basic spring application using beans in Intellij

I tried a basic and my first spring application using the Intellij IDEA community edition 2019.3.1.我使用 Intellij IDEA 社区版 2019.3.1 尝试了一个基本的和我的第一个 spring 应用程序。 I passed the class argument to the main class using ApplicationContext, beans, and the XML file to parse the data value.我使用 ApplicationContext、bean 和 XML 文件将 class 参数传递给主 class 以解析数据值。 But when running the application it shows a FileNotFound Exception, even though the XML file exists there.但是在运行应用程序时,它会显示 FileNotFound 异常,即使 XML 文件存在于那里。 The below image shows my project structure.下图显示了我的项目结构。
在此处输入图像描述

Below are my codes from App.java and luxan.xml以下是我来自 App.java 和 luxan.xml 的代码

package org.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("org/example/luxan.xml");
        Vehicle obj = (Vehicle)context.getBean("vehicle");
        obj.drive();
    }
}

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-5.1.xsd">

    <bean id="vehicle" class="org.example.Car"></bean>

</beans>

And Below is the error I got when running the application以下是我在运行应用程序时遇到的错误

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [org/example/luxan.xml]; nested exception is java.io.FileNotFoundException: class path resource [org/example/luxan.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:345)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:305)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:637)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at org.example.App.main(App.java:10)
Caused by: java.io.FileNotFoundException: class path resource [org/example/luxan.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:331)
    ... 13 more

For your current location of luxan.xml in the above picture.上图中你的luxan.xml当前位置。 you need to provide the whole classpath to the application context.您需要为应用程序上下文提供整个类路径。

ApplicationContext context = new ClassPathXmlApplicationContext("main/java/org/example/luxan.xml");

if luxan.xml is located as the child of the src, we need not provide any path to the application context, just the name of XML is sufficient.如果 luxan.xml 位于 src 的孩子,我们不需要提供任何应用程序上下文的路径,只需 XML 的名称就足够了。

ApplicationContext context = new ClassPathXmlApplicationContext("luxan.xml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM