“导入的org.springframework无法解析." [英] "The import org.springframework cannot be resolved."

查看:253
本文介绍了“导入的org.springframework无法解析."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的POM.xml文件:

Here is my POM.xml file:

<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类:


And below there is my java class:

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类中的import语句中出现错误,这是消息:

I am getting an error in import statements in my App.java classnd this is the message:

导入的org.springframework无法解析."

我清楚地提到了POM.xml中的依赖项,但是我的java类仍然不能从那里选择依赖项.

I clearly mentioned the dependencies in POM.xml, but my java class still cannot pick the dependency from there.

推荐答案

您需要按照一些步骤进行正确的调试.

You need to follow a few steps to debug properly.

1)mvn clean dependency:tree查看输出内容,以确切地了解所获得的内容并验证依赖项是否存在.

1) mvn clean dependency:tree Take a look at the output to see exactly what you get and verify your dependencies are all there.

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

2) mvn clean compile. Does this fail? If not does that mean you only get the error in Eclipse?

您在注释中提到并且我在上面同时运行了两个命令,但出现此错误". mvn clean compile起作用了吗?还是您也为此得到了错误?如果它能正常工作,那只是一个IDE问题,我来看看m2eclipse插件.更好的是,使用IntelliJ,因为免费版本比Eclipse具有更好的maven支持;-)

You mentioned in a comment "And I run both commands above but I am getting this error". Did mvn clean compile work? 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. Better still, use IntelliJ as the free version has better maven support than Eclipse ;-)

一些风格的东西...

Some style things ...

人们经常在不需要时在其pom文件中添加过多的依赖项.如果查看mavenrepository.com中的几个链接,您会发现spring-oxmspring-jdbc都依赖于spring-core,因此您无需显式添加(例如). mvn clean dependency:tree将向您显示所有这些内容,但这更加整洁.

People often add too many dependencies in their pom file when they don't need to. 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). mvn clean dependency:tree will show you what is coming in after all of that, but this is more tidying.

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

这篇关于“导入的org.springframework无法解析."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆