我在windows7中设置环境时遇到两个问题。
问题#1) 运行Spring-Boot-version 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) Spring Boot版本: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;
}
** PersonRepository:**
package com.example;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface PersonRepository extends MongoRepository<Person, String>{
}
** AddressRepository:**
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.
的pom.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"})
,这无法解决我的问题。
答案 0 :(得分:0)
问题1和问题2的解决方案
由Deinum先生评论评论我的jar已损坏,因此我删除了.m2文件夹并再次构建项目。这次工作正常。
它与我的sts configration没有关系,它与jar相关,它已经被破坏了。