使用Spring集成传统代码:java.lang.NoClassDefFoundError:org / springframework / context / ApplicationContextAware

时间:2015-12-20 00:33:14

标签: java spring noclassdeffounderror applicationcontext

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

Legacy项目的 classpath 包含基于Spring的项目的jar文件,旧项目的测试代码如下所示;

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的项目添加到Legacy项目中。

注意#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;
    }

}

主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>

1 个答案:

答案 0 :(得分:1)

将以下文件添加到旧项目类路径应修复 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