Spring boot 配置在字符串STS中不工作
在windows7中设置环境时,我面临两个问题 问题#1) 当运行SpringBootVersion1.5.3时,我得到的错误如下Spring boot 配置在字符串STS中不工作,spring-boot,Spring Boot,在windows7中设置环境时,我面临两个问题 问题#1) 当运行SpringBootVersion1.5.3时,我得到的错误如下 Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication at com.example.MongodbdemoApplication.main(MongodbdemoApplication.java:13) Cau
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)
我的解决方案:
@RestController
public class PersonController {
@Autowired
PersonRepository repo;
}
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);
}
}
当我将版本更改为1.4.6时,其工作正常。即使在谷歌搜索之后,我也找不到问题
问题2)
Spring启动版本: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.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);
}
}
**PersonRepository:**
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> {
}
错误:
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>
4.0.0
com.example
Mongodbemo
0.0.1-快照
罐子
Mongodbemo
SpringBoot的演示项目
org.springframework.boot
spring启动程序父级
1.5.3.1发布
UTF-8
UTF-8
1.8
org.springframework.boot
spring启动程序数据mongodb
org.springframework.boot
SpringBootStarterWeb
org.projectlombok
龙目
真的
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
步骤尝试添加
@ComponentScan(basePackages={“com.example”})
,但这并不能解决我的问题。问题1和问题2的解决方案
正如Deinum先生评论的那样,我的jar已经损坏,所以我删除了.m2文件夹并重新构建了项目。这一次很好
它与我的sts配置无关,它与已损坏的jar相关。当您在1.5.3降级中出错时,很难解决问题。我怀疑您的存储库中有一个损坏的jar。运行
mvn dependency:purge local repository
以清理存储库。在这种情况下添加@ComponentScan
也不会改变任何东西,因为@SpringBootApplication
@M.Deinum中已经定义了这一点,您在清除jar后就可以正常工作了