简体   繁体   English

“导入 org.springframework 无法解析。”

[英]“The import org.springframework cannot be resolved.”

Here is my POM.xml file:这是我的 POM.xml 文件:

<project>
    <properties>
        <jdk.version>1.6</jdk.version>
        <spring.version>3.2.2.RELEASE</spring.version>
        <spring.batch.version>2.2.0.RELEASE</spring.batch.version>
        <mysql.driver.version>5.1.25</mysql.driver.version>
        <junit.version>4.11</junit.version>
    </properties>

    <dependencies>

        <!-- Spring Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring jdbc, for database -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring XML to/back object -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.driver.version}</version>
        </dependency>

        <!-- Spring Batch dependencies -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-infrastructure</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>

        <!-- Spring Batch unit test -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>spring-batch</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


And below there is my java class:下面是我的java类:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        String[] springConfig  =
            {
                "spring/batch/jobs/job-hello-world.xml"
            };
        ApplicationContext context =
            new ClassPathXmlApplicationContext(springConfig);
        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("helloWorldJob");
        try {
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Exit Status : " + execution.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I am getting an error in import statements in my App.java classnd this is the message:我在 App.java 类中的导入语句中遇到错误,这是消息:

"The import org.springframework cannot be resolved." “无法解析导入 org.springframework。”

I clearly mentioned the dependencies in POM.xml, but my java class still cannot pick the dependency from there.我清楚地提到了 POM.xml 中的依赖项,但是我的 java 类仍然无法从那里选择依赖项。

You need to follow a few steps to debug properly.您需要遵循几个步骤才能正确调试。

1) mvn clean dependency:tree Take a look at the output to see exactly what you get and verify your dependencies are all there. 1) mvn clean dependency:tree查看输出以确切了解您得到的内容并验证您的依赖项都在那里。

2) mvn clean compile . 2) mvn clean compile Does this fail?这会失败吗? If not does that mean you only get the error in Eclipse?如果不是,那是否意味着您只会在 Eclipse 中收到错误?

You mentioned in a comment "And I run both commands above but I am getting this error".您在评论中提到“我运行了上面的两个命令,但出现此错误”。 Did mvn clean compile work? mvn clean compile吗? Or did you get an error for that as well?或者你也有错误? If it worked then it's just an IDE problem and I'd look at the m2eclipse plugin.如果它有效,那么它只是一个 IDE 问题,我会查看m2eclipse插件。 Better still, use IntelliJ as the free version has better maven support than Eclipse ;-)更好的是,使用 IntelliJ,因为免费版本比 Eclipse 有更好的 Maven 支持;-)

Some style things ...一些风格的东西...

People often add too many dependencies in their pom file when they don't need to.人们经常在不需要的时候在他们的 pom 文件中添加太多的依赖项。 If you take a look at a couple of links in mavenrepository.com you can see that spring-oxm and spring-jdbc both depend on spring-core so you don't need to add that explicitly (for example).如果您查看 mavenrepository.com 中的几个链接,您会发现spring-oxmspring-jdbc都依赖于spring-core因此您无需明确添加(例如)。 mvn clean dependency:tree will show you what is coming in after all of that, but this is more tidying. mvn clean dependency:tree将向您展示所有这些之后的内容,但这更整洁。

spring-batch-test should be test scope. spring-batch-test应该是test范围。

Finally my issue got resolved.最后我的问题得到了解决。 I was importing the project as "Existing project into workspace".我将项目作为“现有项目到工作区”导入。 This was completely wrong.这是完全错误的。 After that I selected "Existing Maven project" and after that some few hiccups and all errors were removed.之后,我选择了“Existing Maven project”,之后一些小问题和所有错误都被删除了。 In this process I got to learn so many things in Maven which are important for a new comer in Maven project.在这个过程中,我学到了很多 Maven 的东西,这些东西对于 Maven 项目的新手来说很重要。

对我有用的解决方案是右键单击项目 --> Maven --> 更新项目,然后单击确定。

Add these dependencies添加这些依赖项

</dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
</dependencies>

My direct solution for this issue : right click the project --> Maven ---> Add Dependency == then choose the name or parent name of missing dependency我对此问题的直接解决方案:右键单击项目--> Maven ---> 添加依赖项== 然后选择缺少的依赖项的名称或父名称

在此处输入图片说明

Right click project name in Eclipse, -->Maven-->Select Maven Profiles... Then tick the maven profile you want to set.在Eclipse中右键项目名称,-->Maven-->Select Maven Profiles... 然后勾选你要设置的maven profile。 After click OK, Eclipse will automatically import the maven setting to your project.单击确定后,Eclipse 会自动将 maven 设置导入到您的项目中。 If you check your project's Property, you will find Maven Dependencies Library has been added.如果您检查项目的属性,您会发现已添加 Maven 依赖项库。

This answer from here helped me:这里的答案对我有帮助:

You should take a look at the build path of your project to check whether the referenced libraries are still there.您应该查看项目的构建路径以检查引用的库是否仍然存在。 So right-click on your project, then "Properties -> Java Build Path -> Libraries" and check whether you still have the spring library JARs in the place that is mentioned there.因此,右键单击您的项目,然后“属性 -> Java 构建路径 -> 库”并检查在那里提到的位置是否仍然有 spring 库 JAR。 If not, just re-add them to your classpath within this dialog.如果没有,只需在此对话框中将它们重新添加到您的类路径中。

http://forum.spring.io/forum/spring-projects/springsource-tool-suite/98653-springframework-cannot-be-resolved-error http://forum.spring.io/forum/spring-projects/springsource-tool-suite/98653-springframework-cannot-be-resolved-error

In my case I had to delete the jars inside .m2/repository and then did a Maven->Update Maven Project就我而言,我必须删除.m2/repository的 jars,然后执行Maven->Update Maven Project

Looks like the jars were corrupt and deleting and downloading the fresh jar fixed the issue.看起来 jar 已损坏,删除并下载新的 jar 解决了该问题。

if you're sure that your pom.xml is pretty good, then you have just to update the poject.如果您确定您的 pom.xml 非常好,那么您只需更新项目。 right click on the project - Maven - update project.右键单击项目 - Maven - 更新项目。 or simply alt+F5.或简单地 alt+F5。

Add the following JPA dependency.添加以下 JPA 依赖项。

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

For me, this problem occured when I forgot to add spring web dependency.对我来说,这个问题发生在我忘记添加 spring web 依赖时。 I checked it from eclipse's autocomplete that there are no org.springframework.web available for my project.我从eclipse's自动完成功能中检查到我的项目没有可用的org.springframework.web Then from project > spring > add starters I added web dependency to the pom.xml .然后从project > spring > add starters我添加了 web 依赖到pom.xml

org.springframework.beans.factory.support.beannamegenerator , was my error. org.springframework.beans.factory.support.beannamegenerator ,是我的错误。 I did a maven clean, maven build etc., which was not useful and I found that my .m2 folder is not present in my eclipse installation folder.我做了一个 maven clean、maven build 等,这没什么用,我发现我的 .m2 文件夹不在我的 eclipse 安装文件夹中。 I found the .m2 folder out side of the eclipse folder which I pasted in the eclipse folder and then in eclipse I happened to do this :-我在 eclipse 文件夹之外找到了 .m2 文件夹,我将其粘贴到 eclipse 文件夹中,然后在 eclipse 中我碰巧这样做了:-

Open configure build path maven Java EE integration Select Maven archiver generates files under the build directory apply and close打开配置构建路径 maven Java EE 集成 选择 Maven 归档器在构建目录下生成文件 apply 并关闭

My project is up and running now.我的项目现已启动并运行。

I imported a project as 'Existing Maven Project' and was getting this issue.我将一个项目作为“现有 Maven 项目”导入并遇到了这个问题。

Resolution: Changed Java Build Path of JRE System Library to Workspace defailt JRE [jdk 1.8]解决方案:将 JRE 系统库的 Java 构建路径更改为 Workspace defailt JRE [jdk 1.8]

Steps:步骤:

Right click on project -> build path -> configure build path -> Libraries Tab -> double click JRE System Library -> change to Workspace defailt JRE [jdk 1.8]右键单击项目 -> 构建路径 -> 配置构建路径 -> 库选项卡 -> 双击 JRE 系统库 -> 更改为 Workspace defailt JRE [jdk 1.8]

The only solution worked for me is add maven-compiler-plugin to the pom.xml对我有用的唯一解决方案是将 maven-compiler-plugin 添加到 pom.xml

<project ...>
    ...
    <build>
        ...
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <fork>true</fork>
                    <executable>C:\Program Files\Java\jdk1.7.0_79\bin\javac</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...
</project>

Had the same problem in Eclipse STS.在 Eclipse STS 中遇到了同样的问题。 Changing the scope in the pom from "provided" to "compile" fixed the problem and when I changed it back everything was still OK.将 pom 中的范围从“提供”更改为“编译”解决了问题,当我将其改回时,一切仍然正常。

There are few steps you can follow您可以遵循几个步骤

  1. remove repository folder删除存储库文件夹

    C:/Users/user_name/.m2 C:/Users/user_name/.m2

  2. Then run command using IDE terminal or open cmd in your project folder然后使用 IDE 终端运行命令或在项目文件夹中打开 cmd

    mvn clean install mvn 全新安装

  3. Restart your ide重启你的ide

  4. If not solve your problem then run this command如果不能解决您的问题,请运行此命令

    mvn idea:idea mvn 想法:想法

In my case I used the below pom.xml file here就我而言,我在这里使用了下面的 pom.xml 文件

and it worked for me.它对我有用。

In my case, this issue was resolved by updating maven's dependencies:就我而言,此问题已通过更新 maven 的依赖项解决:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入org.springframework无法解析 - Import org.springframework cannot be resolved 导入 org.springframework.web 无法解析。 蚀 - The import org.springframework.web cannot be resolved. Eclipse 即使添加依赖项后,也无法解析导入org.springframework - import org.springframework cannot be resolve even after adding dependencies 在 maven 项目中导入 org.springframework - Import org.springframework in a maven project Eclipse:Java:OpenCV:“无法解析导入组织。” - Eclipse : Java : OpenCV : “The import org cannot be resolved.” 导入 org.springframework.security 无法解析 - The import org.springframework.security cannot be resolved 常春藤无法解析org.springframework依赖项 - ivy cannot resolve org.springframework dependencies 导入 org.springframework.mail 无法解析 - The import org.springframework.mail cannot be resolved 无法解析org.springframework.context.ApplicationContextAware类型。 它是从所需的.class文件间接引用的 - The type org.springframework.context.ApplicationContextAware cannot be resolved. It is indirectly referenced from required .class files 无法解析org.springframework.data.repository.Repository类型。 从所需的.class文件间接引用它 - The type org.springframework.data.repository.Repository cannot be resolved. It is indirectly referenced from required .class files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM