简体   繁体   English

Spring在路径中找不到配置

[英]Spring can't find configuration in path

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. 我的意思是我有点期待它不起作用,因为在Java中,当您尝试使用当前软件包中没有的路径时,它将永远不会起作用。 However as many Spring tutorials I've seen, they all show this way of specifying the configuration file. 但是,正如我所见过的许多Spring教程一样,它们都显示了这种指定配置文件的方式。

Now my springconfig4.xml is in my applications' src/com/nortal/pirs/beans folder. 现在,我的springconfig4.xml位于应用程序的src / com / nortal / pirs / beans文件夹中。 So how do I specify it so that it can be found here? 那么我如何指定它以便可以在这里找到呢?

The current Test4 class is located in src/com/nortal/pirs/test/independant folder. 当前的Test4类位于src / com / nortal / pirs / test / independentant文件夹中。

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$1.run(Unknown Source)
    at java.net.URLClassLoader$1.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? 还是其他地方的问题?

Exception says it is not finding a class, not Spring context xml 异常表示未找到类,而不是Spring上下文xml

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>

Your stacktrace points this: 您的堆栈跟踪指出:

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

You need to download the spring-asm-3.1.2.RELEASE.jar file from the maven repo (supposing that you are using spring 3.1.2) or if you are using maven, edit your pom.xml file 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 javadoc states that it will be removed anyways: ClassPathResource Javadoc指出无论如何将其删除:

Create a new ClassPathResource for ClassLoader usage. 创建一个新的ClassPathResource以用于ClassLoader。 A leading slash will be removed, as the ClassLoader resource access methods will not accept it. 前导斜杠将被删除,因为ClassLoader资源访问方法将不接受它。

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

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. 只需添加spring-asm-3.1.2.RELEASE.jar文件。 Place it in your lib folder. 将其放在您的lib文件夹中。

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 Spring so 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 context insted of factory in the above program. 可以在上述程序中使用工厂插入的上下文

BeanFactory factory = context BeanFactory工厂=上下文

We usually use ApplicationContext insted of BeanFactory . 我们通常使用BeanFactory插入的ApplicationContext Even if you use BeanFactory there will be no effect for the program. 即使使用BeanFactory,该程序也不会起作用。

Add the Jars which is shown in the Screen short and it will work 添加在屏幕简短显示的罐子,它将起作用 在此处输入图片说明

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

相关问题 Spring Batch CommandLineJobRunner找不到.xml配置文件 - Spring Batch CommandLineJobRunner can't find .xml configuration file Spring 引导集成测试找不到配置 - Spring Boot integration test can't find configuration intellij运行配置找不到spring boot类 - intellij run configuration can't find spring boot class 春季:Mongo配置找不到“ mongo:repositories” - Spring: Mongo configuration can't find “mongo:repositories” Spring&jQuery:AJAX调用找不到Controller函数的路径 - Spring & jQuery: AJAX call can't find path to Controller function 基于Java的Spring MVC Java配置无法看到类路径属性文件 - Spring MVC Java-based configuration can't see class-path properties file Spring AspecJ AOP配置中的问题:java.lang.IllegalArgumentException::: 0处的错误找不到引用的切入点 - Problems whit AspecJ AOP Configuration in Spring : java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut Spring无法加载XML配置文件 - Spring can't load xml configuration file Spring FileSystemXmlApplicationContext找不到Bean配置文件并出错 - Spring FileSystemXmlApplicationContext don't find the bean configuration file and go into error Spring Security XML配置找不到我的dataSource bean定义 - Spring Security XML configuration can not find my dataSource beans definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM