我在Windows7中设置环境时遇到了两个问题。
问题#1( 运行 Spring 引导版本 1.5.3 时,我收到错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at com.example.MongodbdemoApplication.main(MongodbdemoApplication.java:13)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
我的解决方案:
当我将版本更改为 1.4.6 时,它工作正常。即使在谷歌搜索后,我也无法找到问题所在。
问题 2(春季启动版本:1.4.6
我的完整代码com.example
包中。但在RestController
类中无法连接PersonRepository
类。代码如下。
package com.example;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
控制器类:
@RestController
public class PersonController {
@Autowired
PersonRepository repo;
}
** 人员存储库:**
package com.example;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface PersonRepository extends MongoRepository<Person, String>{
}
** 地址存储库:**
package com.example;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface AddressRepository extends MongoRepository<Address, Integer> {
}
主要应用类:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.*"})
public class MongodbdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MongodbdemoApplication.class, args);
}
}
错误:
Error Logs:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field repo in com.example.PersonController required a bean of type 'com.example.PersonRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.PersonRepository' in your configuration.
绒球.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>mongodbdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mongodbdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
步骤 尝试添加@ComponentScan(basePackages = {"com.example"})
,这并不能解决我的问题。
问题 1 和问题 2 的解决方案
正如Deinum先生评论的那样,我的jar已损坏,因此我删除了.m2文件夹并再次构建项目。这次它工作正常。
它与我的 sts 配置无关,它与已损坏的 jar 有关。