如何通过ClassPathXmlApplicationContext类创建对象?

时间:2019-07-15 13:17:45

标签: java spring javabeans

当我尝试执行时,我的spring项目出错。这是我的第一个春季项目。您能帮我跳过此错误吗?我的代码如下。

beans.xml

<?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.xsd">

    <bean id="user" class="com.beans.demo.User">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="Smith"></constructor-arg>
        <property name="emp" ref="emp"></property>
    </bean>

    <bean id="emp" class="com.beans.demo.Employee">
       <property name="emp_id" value="11"></property>
       <property name="emp_name" value="Martin"></property>
       <property name="all_employer">
           <list>
               <value>Google</value>
               <value>Facebook</value>
               <value>Microsoft</value>
           </list>
       </property>
    </bean>
</beans>

BeansDemo.java

package com.beans.demo;

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

public class BeansDemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/beans/config/beans.xml");
        User user = (User) applicationContext.getBean("user");

        System.out.println("User id and name: " + user.getId() + " and " + user.getName());

        ((ClassPathXmlApplicationContext)applicationContext).close();
    }
}

project structure

我收到此错误:

  

/库/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java   “ -javaagent:/ Applications / IntelliJ   IDEA.app/Contents/lib/idea_rt.jar=55724:/Applications/IntelliJ   IDEA.app/Contents/bin“ -Dfile.encoding = UTF-8 -classpath   /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/deploy。 jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/ lib / ext / dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/目录/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/Java/JavaVirtualMachines/jdk1 .8.0_181.jdk / Contents / Home / jre / lib / ext / nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunec.jar:/ Library /Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/ sunpkcs11.jar:/库/ Java / JavaVir tualMachines / jdk1.8.0_181.jdk / Contents / Home / jre / lib / ext / zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/javaws.jar:/库/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jfr.jar :/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jsse .jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre /lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home /jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents /home/lib/dt.jar:/库/ Java / Jav aVirtualMachines / jdk1.8.0_181.jdk / Contents / Home / lib / javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/jconsole.jar:/ Library / Java / JavaVirtualMachines / jdk1.8.0_181.jdk / Contents / Home / lib / packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/sa-jdi.jar:/ Library / Java / JavaVirtualMachines / jdk1.8.0_181.jdk / Contents / Home / lib / tools.jar:/ Users / semihkirdinli / IntellijProjects / Beans_Demo / target / classes:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-aspects-4.3.18 .RELEASE.jar:/用户/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-context-support-4.3.18.RELEASE.jar:/用户/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-beans-4.3.18.RELEASE .jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-aop-4.3.18.RELEASE.jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-expression-4.3.18.RELEASE.jar:/用户/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-instrument-4.3.18.RELEASE.jar:/用户/ semihkirdi nli / IntellijProjects / Beans_Demo / lib / spring-instrument-tomcat-4.3.18.RELEASE.jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-core-4.3.18.RELEASE.jar:/ Users / semihkirdinli / IntellijProjects / Beans_Demo / lib / spring-jms-4.3.18.RELEASE.jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-context-4.3.18.RELEASE.jar:/ Users / semihkirdinli / IntellijProjects / Beans_Demo / lib / spring-oxm-4.3.18.RELEASE.jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-messaging-4.3.18.RELEASE.jar:/ Users / semihkirdinli / IntellijProjects / Beans_Demo / lib / spring- orm-4.3.18.RELEASE.jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-jdbc-4.3.18.RELEASE.jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/commons-logging-1.2。 jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/spring-tx-4.3.18.RELEASE.jar:/Users/semihkirdinli/IntellijProjects/Beans_Demo/lib/aopalliance-1.0.jar:/ Users / semihkirdinli / IntellijProjects / Beans_Demo /lib/spring-test-4.3.18.RELEASE.jar:/用户/半hkirdinli / .m2 / repository / org / springframework / spring-beans / 5.1.3.RELEASE / spring-beans-5.1.3.RELEASE.jar:/Users/semihkirdinli/.m2/repository/org/springframework/spring-context /5.1.3.RELEASE/spring-context-5.1.3.RELEASE.jar:/Users/semihkirdinli/.m2/repository/org/springframework/spring-aop/5.1.3.RELEASE/spring-aop-5.1.3 .RELEASE.jar:/Users/semihkirdinli/.m2/repository/org/springframework/spring-expression/5.1.3.RELEASE/spring-expression-5.1.3.RELEASE.jar:/Users/semihkirdinli/.m2/repository /org/springframework/spring-core/5.1.3.RELEASE/spring-core-5.1.3.RELEASE.jar:/Users/semihkirdinli/.m2/repository/org/springframework/spring-jcl/5.1.3.RELEASE /spring-jcl-5.1.3.RELEASE.jar   com.beans.demo.BeansDemo 2019年7月15日下午2:31:24   org.springframework.context.support.AbstractApplicationContext   prepareRefresh INFO:刷新   org.springframework.context.support.ClassPathXmlApplicationContext@14514713:   启动日期[2019年7月15日星期一14:31:24];上下文层次结构的根   2019年7月15日下午2:31:24   org.springframework.beans.factory.xml.XmlBeanDefinitionReader   loadBeanDefinitions INFO:从类路径加载XML bean定义   资源[com / beans / config / beans.xml]线程“ main”中的异常   org.springframework.beans.factory.BeanDefinitionStoreException:   IOException从类路径资源解析XML文档   [com / beans / config / beans.xml];嵌套异常为   java.io.FileNotFoundException:类路径资源   [com / beans / config / beans.xml]无法打开,因为它没有   存在于   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)     在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)     在   org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)     在   org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)     在   org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)     在   org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)     在   org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515)     在   org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)     在   org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)     在com.beans.demo.BeansDemo.main(BeansDemo.java:13)上,由以下原因引起:   java.io.FileNotFoundException:类路径资源   [com / beans / config / beans.xml]无法打开,因为它没有   存在于   org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)     在   org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)     ...还有13个

     

以退出代码1完成的过程

0 个答案:

没有答案