Java STS Spring 项目:为什么那个 HelloWorld 示例不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18784174/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:02:04  来源:igfitidea点击:

STS Spring Project: Why is that HelloWorld example not working?

javaspringmaven

提问by achingfingers

I am new to Spring and currently trying the following Hello World examplefrom the book ProSpring3:

我是 Spring 的新手,目前正在尝试ProSpring3 一书中的以下Hello World 示例

package com.tutorials.prospring3.ch2;

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

    public class HelloWorldSpringDI {
        public static void main(String[] args) {
            // Initialize Spring ApplicationContext
            ApplicationContext ctx = new ClassPathXmlApplicationContext(
                    "META-INF/spring/app-context.xml");
            MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
            mr.render();
        }
    }

The ClassPathXmlApplicationContext instantiation is marked as the following error: "The constructor ClassPathXmlApplicationContext(String) refers to the missing type BeansException"

ClassPathXmlApplicationContext 实例化被标记为以下错误:“构造函数 ClassPathXmlApplicationContext(String) 引用了缺少的类型 BeansException”

In addiation, when running the application the IDE displays a build path problem:"Archive for required library: 'C:/Users/Admin/.m2/repository/org/springframework/spring-beans/3.0.6.RELEASE/spring-beans-3.0.6.RELEASE.jar' in project 'ch2' cannot be read or is not a valid ZIP file"

此外,在运行应用程序时,IDE 显示构建路径问题:“所需库的存档:'C:/Users/Admin/.m2/repository/org/springframework/spring-beans/3.0.6.RELEASE/spring-项目 'ch2' 中的 beans-3.0.6.RELEASE.jar' 无法读取或不是有效的 ZIP 文件"

As the spring-beans.jar (to which the BeansException class belongs) is part of my Maven Dependenciesi wonder how that could happen.

由于 spring-beans.jar(BeansException 类所属)是我的Maven 依赖项的一部分,我想知道这是怎么发生的。

STS screenshot

STS截图

Has anyone a suggestion how to solve that problem?

有没有人建议如何解决这个问题?

Thanks in advance!

提前致谢!

Jonas

乔纳斯

采纳答案by jeroen_de_schutter

try to open the archive C:/Users/Admin/.m2/repository/org/springframework/spring-beans/3.0.6.RELEASE/spring-beans-3.0.6.RELEASE.jarwith an application like WinZip. If that application indicates that the archive is corrupted as well, you should delete it and try again. (delete the entire C:/Users/Admin/.m2/repository/org/springframework/spring-beans/3.0.6.RELEASEdirectory and all its contents). On the next build of your project, the archive will be downloaded again. If it still does not work and you are connecting to a local repository, you should contact the administrator.

尝试C:/Users/Admin/.m2/repository/org/springframework/spring-beans/3.0.6.RELEASE/spring-beans-3.0.6.RELEASE.jar使用 WinZip 等应用程序打开存档。如果该应用程序表明存档也已损坏,则应将其删除并重试。(删除整个C:/Users/Admin/.m2/repository/org/springframework/spring-beans/3.0.6.RELEASE目录及其所有内容)。在项目的下一次构建中,将再次下载存档。如果它仍然不起作用并且您正在连接到本地存储库,您应该联系管理员。

回答by Mohit Singh

Look like the dependency is missing from your project's classpath. To resolve it

看起来您的项目的类路径中缺少依赖项。解决它

Right click on your project
Navigate to **Maven**
Click on **Update project**

If it does not resolve this problem then

如果它不能解决这个问题,那么

Go to the path

去路径

C:/Users/Admin/.m2/repository/org/springframework/ and delete the folder named as spring-beans

C:/Users/Admin/.m2/repository/org/springframework/ 并删除名为的文件夹 spring-beans

Now again Right click on your project
Navigate to **Maven**
Click on **Update project**

It will update/add the orphan/missing dependency of your project

它将更新/添加项目的孤立/缺失依赖项

回答by user6458893

I konw your problem have out of date, i just want to help someone who gets the same problem, as i got the same problem and finished it.It's easy,just add the org.springframework.beans.jar to your project.

我知道你的问题已经过时了,我只是想帮助遇到同样问题的人,因为我遇到了同样的问题并完成了它。很简单,只需将 org.springframework.beans.jar 添加到您的项目中。

回答by Sameer Sukumaran

Two causes:

两个原因:

  1. The issue could be with the corrupted Spring JARs . Remove the existing Spring dependancies Add dependencies from : http://mvnrepository.com/artifact/org.springframework/spring-beans/5.0.0.M5It ll solve the issue.
  1. 问题可能与损坏的 Spring JAR 有关。删除现有的 Spring 依赖项从以下位置添加依赖项:http: //mvnrepository.com/artifact/org.springframework/spring-beans/5.0.0.M5它将解决问题。

2.The indirect dependancies for beans are not sufficiently added.

2.对bean的间接依赖添加不充分。

Add the dependancies .

添加依赖项。

Sameer Sukumaran

萨米尔·苏库马兰

回答by Sabunkar Tejas Sahailesh

Go to :C:\Users(user_name(or)admin_name).m2\repository\org\springframework

转到 :C:\Users(user_name(or)admin_name).m2\repository\org\springframework

Now, delete spring-beans and spring-expression folder.

现在,删除 spring-beans 和 spring-expression 文件夹。

Go to :Eclipse and then update the project by right clicking on the project maven>update project. Eclipse will download the required files.ISSUE SOLVED :-)

转到:Eclipse,然后通过右键单击项目 maven>update project 来更新项目。Eclipse 将下载所需的文件。问题已解决 :-)

回答by Youness Marhrani

I had the same issue when I was working with 4.2.2.RELEASE version of Spring, once I changed it to 4.2.0.RELEASE, the error dispeared. I hope this can help in your case too.

当我使用 4.2.2.RELEASE 版本的 Spring 时,我遇到了同样的问题,一旦我将其更改为 4.2.0.RELEASE,错误就会消失。我希望这对你的情况也有帮助。