java – “无法解析导入org.springframework”.

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

Below 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语句中遇到错误. “无法解析导入org.springframework.”我清楚地提到了POM.xml中的依赖关系,我的java类仍然无法从那里选择依赖.

最佳答案
您需要按照几个步骤进行正确调试.

1)mvn clean依赖:树看看输出,看看你得到了什么,并验证你的依赖是什么.

2)mvn clean编译.这会失败吗?如果不是这意味着你只在Eclipse中得到错误?

你在评论中提到“我在上面运行两个命令,但是我收到了这个错误”. mvn clean编译工作了吗?或者你也得到了错误?如果它工作,那它只是一个IDE问题,我会看看m2eclipse插件.更好的是,使用IntelliJ作为免费版本比Eclipse有更好的maven支持;-)

一些风格的东西……

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

弹簧批量测试应该是测试范围.

转载注明原文:java – “无法解析导入org.springframework”. - 代码日志