java Spring在路径中找不到配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13784612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:58:17  来源:igfitidea点击:

Spring can't find configuration in path

javaspringconfigurationdependency-injection

提问by Arturas M

well I'm runnning this code and it cant' find the springconfig4.xml file:

好吧,我正在运行此代码,但找不到 springconfig4.xml 文件:

package com.nortal.pirs.test.independant;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test4 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("/com/nortal/pirs/beans/springconfig4.xml");
        BeanFactory factory = context;
        Test3 instance = (Test3) factory.getBean("Test3");

        instance.run();
    }
}

I mean I was kind of expecting it not to work, because in Java it never works when you try to use a path that is not in your current package. However as many Spring tutorials I've seen, they all show this way of specifying the configuration file.

我的意思是我有点期待它不起作用,因为在 Java 中,当您尝试使用不在当前包中的路径时,它永远不会起作用。然而,正如我所看到的许多 Spring 教程,它们都展示了这种指定配置文件的方式。

Now my springconfig4.xml is in my applications' src/com/nortal/pirs/beans folder. So how do I specify it so that it can be found here?

现在我的 springconfig4.xml 位于我的应用程序的 src/com/nortal/pirs/beans 文件夹中。那么我如何指定它以便可以在这里找到它?

The current Test4 class is located in src/com/nortal/pirs/test/independant folder.

当前的 Test4 类位于 src/com/nortal/pirs/test/independant 文件夹中。

My stacktrace:

我的堆栈跟踪:

    2012-12-09 06:16:15,734 [main] INFO  org.springframework.context.support.AbstractApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b24044e: startup date [Sun Dec 09 06:16:15 EET 2012]; root of context hierarchy
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/asm/ClassVisitor
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:121)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.<init>(AbstractAutowireCapableBeanFactory.java:168)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.<init>(DefaultListableBeanFactory.java:167)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:195)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:128)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.nortal.pirs.test.independant.Test4.main(Test4.java:9)
Caused by: java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 10 more

My springconfig4.xml:

我的 springconfig4.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

  <context:component-scan base-package="com.nortal.pirs.businesslogic.logic"></context:component-scan>
  <context:component-scan base-package="com.nortal.pirs.test.independant"></context:component-scan>

</beans>

Or is the problem somewhere else maybe?

或者可能是其他地方的问题?

回答by Subin Sebastian

Exception says it is not finding a class, not Spring context xml

异常表示它没有找到一个类,而不是 Spring 上下文 xml

Caused by: java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor

Caused by: java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor

please add asm dependency

请添加 asm 依赖项

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>org.springframework.asm</artifactId>
    <version>3.1.0.RELEASE</version>
</dependency>

回答by ElderMael

Your stacktrace points this:

您的堆栈跟踪指出:

java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor

You need to download the spring-asm-3.1.2.RELEASE.jarfile from the maven repo(supposing that you are using spring 3.1.2) or if you are using maven, edit your pom.xmlfile and add the next lines:

您需要从maven 存储库下载spring-asm-3.1.2.RELEASE.jar文件(假设您使用的是 spring 3.1.2),或者如果您使用的是 maven,请编辑您的文件并添加下一行:pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-asm</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>

Also, this line:

此外,这一行:

ApplicationContext context = new ClassPathXmlApplicationContext("/com/nortal/pirs/beans/springconfig4.xml");

Has an error, the path must not start with a trailing /., so the path to the file becomes: "com/nortal/pirs/beans/springconfig4.xml".

有错误,路径不能以/.开头,所以文件路径变成:"com/nortal/pirs/beans/springconfig4.xml".

The ClassPathResource javadocstates that it will be removed anyways:

使用ClassPathResource的javadoc说,它会被删除反正:

Create a new ClassPathResource for ClassLoader usage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it.

为 ClassLoader 使用创建一个新的 ClassPathResource。前导斜杠将被删除,因为 ClassLoader 资源访问方法将不接受它。

回答by sheetal

Add spring-asm-3.0.1.RELEASE.jarand spring-expression-3.0.1.RELEASE.jar.

添加spring-asm-3.0.1.RELEASE.jarspring-expression-3.0.1.RELEASE.jar

回答by Kiran Kawade

java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor

java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor

Just add the spring-asm-3.1.2.RELEASE.jar file. Place it in your lib folder.

只需添加 spring-asm-3.1.2.RELEASE.jar 文件。把它放在你的 lib 文件夹中。

回答by vikky

For the above problem as mentioned above these jars are missing namely 1.org.springframework.asm-3.0.1.RELEASE-A.jar 2.org.springframework.expression-3.0.1.RELEASE-A.jar or Add all the core Jars of Springso that any new programmer can Run all basic level programs.

对于上面提到的上述问题,这些 jar 丢失即 1.org.springframework.asm-3.0.1.RELEASE-A.jar 2.org.springframework.expression-3.0.1.RELEASE-A.jar 或添加所有Spring 的核心 Jars以便任何新程序员都可以运行所有基本级别的程序。

There is no need of below line in your program. insted of that you can use contextinsted of factoryin the above program.

您的程序中不需要以下行。insted的,你可以使用上下文insted的的工厂在上面的程序。

BeanFactory factory = context

BeanFactory 工厂 = 上下文

We usually use ApplicationContextinsted of BeanFactory. Even if you use BeanFactory there will be no effect for the program.

我们通常使用BeanFactory 的ApplicationContext插入。即使您使用 BeanFactory 也不会对程序产生任何影响。

回答by Vikram B

Add the Jars which is shown in the Screen short and it will workenter image description here

添加屏幕短片中显示的罐子,它将起作用在此处输入图片说明