编辑:当我从SPRING DEPENDENCY @ POM.XML中删除提供时,它突然有效,有人可以告诉我它为什么会起作用吗?
嗨,大家好我一直
Caused by: java.lang.NoClassDefFoundError: org/springframework/beans/factory/support/BeanDefinitionRegistry
每当我尝试执行
时mvn exec:java -Dexec.mainClass="org.carlos.spring.FirstSpringHelloWorld"
我的代码
代码如下
package org.carlos.spring;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.carlos.decoupled.MessageSource;
import org.carlos.decoupled.MessageDestination;
public class FirstSpringHelloWorld {
public static void main(String[] args) {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(bf);
reader.loadBeanDefinitions(new ClassPathResource("/META-INF/spring/helloworld-context.properties"));
MessageSource source = (MessageSource) bf.getBean("source");
MessageDestination destination = (MessageDestination) bf.getBean("destination");
destination.write(source.getMessage());
}
}
package org.carlos.decoupled;
public interface MessageSource {
String getMessage();
}
package org.carlos.decoupled;
public interface MessageDestination {
void write(String message);
}
这是我的POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.carlos.hello</groupId>
<artifactId>hello</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>hello Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>2.5.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>hello</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
hello-world-context.properties:
source.(class)=org.carlos.decoupled.SimpleMessageSource
destination.(class)=org.carlos.decoupled.StdoutMessageDestination
有人可以告诉我如何解决这个问题吗?我的智慧结束了:(
答案 0 :(得分:1)
maven控制编译时类路径,但不控制运行时类路径。检查运行时类路径(通常是名为CLASSPATH的环境变量)以确保所有依赖项都在运行时类路径中(包括spring jar)。