Java “无法自动装配。没有类型的豆类......找到”与简单项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39939916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Could not autowire. No beans of type... found" with Simple project
提问by IcedDante
I downloaded the simple JPA Spring Boot tutorialand it worked just fine. However, when I attempt to replicate this simple behavior in my own test project, I get a "could not autowire" error on the bean injection in my Application.demo() method that returns a CommandLineRunner. The project is so barebones I don't even know what to submit but here's the POM:
我下载了简单的JPA Spring Boot 教程,效果很好。但是,当我尝试在我自己的测试项目中复制这个简单的行为时,在返回 CommandLineRunner 的 Application.demo() 方法中的 bean 注入中出现“无法自动装配”错误。该项目是如此准系统,我什至不知道该提交什么,但这是 POM:
<?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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.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-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
And the application.
和应用程序。
package com.example;
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Bean
//errors with: "Could not autowire. No beans of 'SimpRepository' type found"
public CommandLineRunner demo(SimpRepository repository) {
return (args) -> {
};
}
}
And the Repository service:
和存储库服务:
package com.example;
public interface SimpRepository extends CrudRepository<Simp, Long> {
}
for the following entity:
对于以下实体:
package com.example;
@Entity
public class Simp {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String value;
public Simp(String value) {
this.value = value;
}
}
采纳答案by metacubed
A couple of possibilities here.
这里有几种可能性。
You need to add the @EnableJpaRepositories(basePackages = {"your.pkg.here"})
to the TestApplication
. This tells Spring Data to look for your repository classes under the specified package. If the repository package name is the same as the TestApplication
, you can skip the basePackages
part.
您需要将@EnableJpaRepositories(basePackages = {"your.pkg.here"})
加到TestApplication
. 这告诉 Spring Data 在指定的包下查找您的存储库类。如果存储库包名称与 相同,则TestApplication
可以跳过该basePackages
部分。
Similarly, if your TestApplication
and SimpRepository
are not in the same package, you need to add a @ComponentScan
with the list of all relevant packages.
同样,如果您的TestApplication
和SimpRepository
不在同一个包中,则需要添加@ComponentScan
所有相关包的列表。