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

新手上路,请多包涵

这是我的 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>

下面是我的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();
        }
    }
}

我的 App.java 类中的导入语句出现错误,这是消息:

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

我在 POM.xml 中明确提到了依赖项,但我的 java 类仍然无法从那里选择依赖项。

原文由 Vaibhav 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

您需要按照几个步骤正确调试。

  1. mvn clean dependency:tree 查看输出以准确了解您得到的内容并验证您的依赖项是否全部存在。

  2. mvn clean compile 。这会失败吗?如果不是,那是否意味着您只会在 Eclipse 中遇到错误?

您在评论中提到“我运行上面的两个命令,但出现此错误”。 mvn clean compile 工作了吗?还是您也因此收到错误消息?如果它有效,那么它只是一个 IDE 问题,我会查看 m2eclipse 插件。更好的是,使用 IntelliJ,因为免费版本比 Eclipse 有更好的 Maven 支持;-)

一些风格的东西…

人们经常在他们不需要的时候在他们的 pom 文件中添加太多的依赖。如果您查看 mavenrepository.com 中的几个链接,您会看到 spring-oxmspring-jdbc 都依赖于 spring-core 所以你不需要明确添加(例如)。 mvn clean dependency:tree 会告诉你所有这些之后会发生什么,但这更整洁。

spring-batch-test 应该是 test 范围。

原文由 Matt Byrne 发布,翻译遵循 CC BY-SA 3.0 许可协议

最后我的问题得到解决。我正在将项目导入为“现有项目到工作区”。这是完全错误的。之后我选择了“现有的 Maven 项目”,之后一些小问题和所有错误都被删除了。在这个过程中,我在 Maven 中学到了很多东西,这对于 Maven 项目的新手来说很重要。

原文由 Vaibhav 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题