【问题标题】:ApplicationContext import in springSpring中的ApplicationContext导入
【发布时间】:2017-02-03 18:33:21
【问题描述】:

我正在通过本教程学习 Spring:

http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024

在本教程中,讲师下载版本为 3.2.3.RELEASE 的 spring 依赖项(spring-beans、spring context、spring-core)。

然后写下这段代码:

package com.caveofprogramming.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

   public static void main(String[] args) {

      ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");

      Person person = (Person)context.getBean("person");
      person.speak();
}
}

当我在最新版本 4.3.3.RELEASE 中使用:spring-context、spring-beans 和 spring-core 时,ApplicationContext 导入不起作用。当我将其更改为旧版本时,它可以工作。 “不起作用”意味着当我编写“ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");”时 eclips 不知道我应该导入什么当我自己写“import org.springframework.context.ApplicationContext”时,它是下划线。

如何导入最新版本依赖的ApplicationContext?

编辑: 这是错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
ApplicationContext cannot be resolved to a type
FileSystemXmlApplicationContext cannot be resolved to a type

at com.caveofprogramming.spring.test.App.main(App.java:10)

这是我的 pom 文件:

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.caveofprogramming.spring.test</groupId>
<artifactId>spring-tutorial-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>

编辑 2:我还看到该程序接受 4.1.1.RELEASE 版本。也许不需要最新版本的依赖项?我刚开始使用 Spring,每个人都说我应该在最新版本上工作。

编辑 3;

我找到的唯一解决方案是使用 spring-context 4.1.1.RELEASE

【问题讨论】:

  • 包含您正在使用的确切 pom.xml。还要详细描述问题,“import doesn't work”不够具体。

标签: java spring maven dependencies


【解决方案1】:

在类路径 org.springframework.context.support-3.1.0.RELEASE.jar 和 org.springframework.context-3.1.0.RELEASE.jar 中添加这些 jar

如果你正在使用maven并更新项目,请添加此依赖项。

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

【讨论】:

    【解决方案2】:

    你必须添加依赖

    <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.x.x</version>
    </dependency>
    

    【讨论】:

    • 我拥有所有依赖项。我编辑了我的帖子并添加了这些信息
    【解决方案3】:

    这可能是 eclipse 的问题,特别是如果您使用的是旧版本。当您添加新的依赖项时,Eclipse 曾经在 Maven 项目中遇到问题。

    您可以通过右键单击您的项目并选择 Maven->更新项目来强制 Eclipse 更新其 Maven 依赖项

    之后,您的项目应该可以正常编译:

    PS:查看current documentation 以获取基于 XML 的应用程序上下文设置的最新设置

    【讨论】:

    • 我的 Eclipse 版本:4.6.0(霓虹灯)。 “更新项目...”不起作用。我还删除并导入了现有的 Maven 项目,但没有结果。当我使用 4.1.1.RELEASE spring 依赖版本时,程序可以工作。这将是一个巨大的错误?
    【解决方案4】:

    我在 dependencies 标记内的pom.xml 文件中添加了这个。

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.7</version>
        <type>pom</type>
    </dependency>
    

    它的作用是下载 spring-context-5.3.7.jar 文件,从中可以导入 ApplicationContext 类。 希望它也对你有用。

    这是我的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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.Sharma</groupId>
      <artifactId>NachoVarga</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>NachoVarga</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.7</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.7</version>
        <type>pom</type>
    </dependency>
    
      </dependencies>
    </project>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2012-03-28
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多