java Spring 中的 ApplicationContext 导入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39702430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ApplicationContext import in spring
提问by Michal Ryzio
I'm learning Spring from this tutorial:
我正在从本教程中学习 Spring:
http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024
http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024
In this tutorial, instructor downloads spring dependencies (spring-beans, spring context, spring-core) in version 3.2.3.RELEASE.
在本教程中,讲师下载 3.2.3.RELEASE 版本的 spring 依赖项(spring-beans、spring context、spring-core)。
and then writes this code:
然后编写以下代码:
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();
}
}
When I use: spring-context, spring-beans and spring-core in last version 4.3.3.RELEASE then ApplicationContext import doesn't work. It works when I change it to the old version. "Doesn't work" means that eclips doesn't know what I should import when I write "ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");" and when I write "import org.springframework.context.ApplicationContext" by myself it's underline.
当我在最新版本 4.3.3.RELEASE 中使用:spring-context、spring-beans 和 spring-core 时,ApplicationContext 导入不起作用。当我将其更改为旧版本时,它可以工作。“不起作用”意味着当我写“ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");”时,eclips 不知道我应该导入什么。当我自己写“import org.springframework.context.ApplicationContext”时,它是下划线。
What should I do to import ApplicationContext with newest version dependencies?
我应该怎么做才能导入具有最新版本依赖项的 ApplicationContext?
Edit: This is the error:
编辑:这是错误:
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)
and this is my pom file:
这是我的 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>
Edit 2: I also saw that program accepts 4.1.1.RELEASE version. Mabye the newest version of dependencies isn't necessary? I'm just starting with Spring and everyone says that I should work on the newest version.
编辑 2:我还看到该程序接受 4.1.1.RELEASE 版本。Mabye 最新版本的依赖项不是必需的吗?我刚开始使用 Spring,每个人都说我应该在最新版本上工作。
Edit 3;
编辑 3;
The only solution which I found is using spring-context 4.1.1.RELEASE
我发现的唯一解决方案是使用 spring-context 4.1.1.RELEASE
回答by Vivek Kumar
Either add these jars in your class path org.springframework.context.support-3.1.0.RELEASE.jar and org.springframework.context-3.1.0.RELEASE.jar
在你的类路径 org.springframework.context.support-3.1.0.RELEASE.jar 和 org.springframework.context-3.1.0.RELEASE.jar 中添加这些 jars
Or add this dependency if you are using maven and update the project.
或者,如果您使用 Maven 并更新项目,请添加此依赖项。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.x.x.RELEASE</version>
</dependency>
回答by kuhajeyan
You have to add dependency
你必须添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.x.x</version>
</dependency>
回答by Tobi Nonymous
This might be a problem with eclipse, especially if you are using an older version. Eclipse used to have issues with Maven projects, when you added new dependencies.
这可能是 eclipse 的问题,尤其是当您使用旧版本时。当您添加新的依赖项时,Eclipse 曾经有 Maven 项目的问题。
You can force Eclipse to update it's Maven dependencies by right clicking your project and selecting Maven->Update project
您可以通过右键单击您的项目并选择 Maven->Update project 来强制 Eclipse 更新它的 Maven 依赖项
After that your project should compile just fine:
PS: Check the current documentationfor up-to-date setup of XML-based application context setup
PS:检查当前文档以了解基于 XML 的应用程序上下文设置的最新设置