gpt4 book ai didi

java - 遗留代码与 Spring 的集成 : java. lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware

转载 作者:行者123 更新时间:2023-11-30 07:43:08 30 4
gpt4 key购买 nike

我有一个遗留代码,它将使用基于Spring的项目并希望集成它们。

Legacy项目的classpath包含Spring-Based项目的jar文件,Legacy项目的测试代码如下;

package com.levo.test;

import com.levo.legacy.MainGreeter;

public class Test {

public static void main(String[] args) {
MainGreeter m = new MainGreeter();

m.execute();
}

}

旧项目具有基于Spring的项目的.jar文件,如下所示;

enter image description here

输出是;

Main Greeter started!
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
...

为什么我收到 java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware 以及如何集成这两个项目?问题出在哪里。

注意:不能将基于 Spring 的项目添加到旧项目中。

注意#2:如果有详细解释的答案,我们将不胜感激。

基于Spring的项目文件如下;

项目树

enter image description here

SpringApplicationContext.java

package com.levo.springproj.util;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplicationContext implements ApplicationContextAware {

private static final Logger logger = Logger.getLogger(SpringApplicationContext.class);

private static ApplicationContext CONTEXT;
public static String ENVIRONMENT = "none";
public static String CONTEXTNAME = "main-context.xml";

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

}

public static Object getBean(String beanName) {
if (CONTEXT == null) {
System.setProperty("intMaximoEnvironment", ENVIRONMENT);
CONTEXT = new ClassPathXmlApplicationContext("classpath:" + CONTEXTNAME);
}
return CONTEXT.getBean(beanName);
}

}

MainGreeter.java

package com.levo.springproj;

import com.levo.springproj.service.Greeter;
import com.levo.springproj.util.SpringApplicationContext;

public class MainGreeter {

private static final String BEANSUFFIX = "Bean";

public void execute()
{
System.out.println( "Main Greeter started!" );

setEnvironment("Test");

System.out.println("Environment : " + SpringApplicationContext.ENVIRONMENT);
System.out.println("Context : " + SpringApplicationContext.CONTEXTNAME);

Greeter greeter = getBeanForEvent("Greeter");
greeter.greetSupporter();
}

private static Greeter getBeanForEvent(String eventName) {
try {
Greeter o = (Greeter) SpringApplicationContext.getBean(eventName + BEANSUFFIX);
return o;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

private void setEnvironment(String environment) {
if(environment==null)
{
return;
}

try{
SpringApplicationContext.ENVIRONMENT = environment;
} catch(Exception e) {
e.printStackTrace();
}

}

}

Greeter.java

package com.levo.springproj.service;

public interface Greeter {

public void greetSupporter();

}

GreeterImpl.java

package com.levo.springproj.service.impl;

import com.levo.springproj.service.Greeter;

public class GreeterImpl implements Greeter {

private String name;

public void greetSupporter() {
System.out.println("Hello " + name);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

main-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:integration="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util" xmlns:ws="http://www.springframework.org/schema/integration/ws"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/integration/ws http://www.springframework.org/schema/integration/ws/spring-integration-ws-3.0.xsd
http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-3.0.xsd
">

<bean id="GreeterBean"
class="com.levo.springproj.service.impl.GreeterImpl">
<property name="name" value="Levent" />
</bean>

</beans>

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>com.levo.legacy</groupId>
<artifactId>SpringDebug</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringDebug</name>
<url>http://maven.apache.org</url>

<build>
<finalName>SpringProj</finalName>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>

</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.4.RELEASE</version>
<scope>test</scope>

</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-stream</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ws</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

</dependencies>

<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Maven RELEASE Repository</name>
<url>http://maven.springframework.org/release</url>
</repository>

<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>

</repositories>

</project>

最佳答案

将以下文件添加到旧项目类路径应该可以修复 NoClassDefFoundError 异常。旧的遗留代码不知道基于 Spring 的项目中使用的 ApplicationContext;

spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar

关于java - 遗留代码与 Spring 的集成 : java. lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34376862/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com