我将使用spring data jpa
进行简单的测试。
我有一个简单的pojo
,一个界面和一个runner
应用程序。
这是我的代码:
package aa.bb.cc.repository;
@Repository
public interface ContentRepository extends CrudRepository<Content, Long>{
}
而且,我有一个简单的POJO
:
@Entity
@Table(name = "content")
public class Content {
public Content(String name, String title, String description) {
this.name = name;
this.title = title;
this.description = description;
}
@NotNull
private String name;
@NotNull
private String title;
@NotNull
private String description;
...
}
而且,Application
类:
package aa.bb.cc.repository;
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public CommandLineRunner demo(ContentRepository repository) {
return (args) -> {
// save two contents
repository.save(new Content("name1", "title1", "description1"));
// fetch all Contents
log.info("Contents found with findAll():");
for (Content eachContent : repository.findAll()) {
log.info(eachContent.toString());
}
log.info("");
};
}
}
我的pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.185</version>
</dependency>
我得到了这个例外:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [aa.bb.cc.repository.ContentRepository] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this
dependency. Dependency annotations: {}
我看到了一些相关的问题,但无法解决这个问题。 解决方案是什么?
更新
spring-config.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<jpa:repositories base-package="aa.bb.cc.repository"/>
<context:annotation-config/>
</beans>
答案 0 :(得分:4)
尝试将ContentRepository
,Content
和Application
放在同一个包中。如果tou需要不同的包,请使用:
Application
@EnableJpaRepositories("repository.package")
@EntityScan("entities.package")
@ComponentScan("other.components.package")
答案 1 :(得分:1)
根据specializt的回答,您可以添加@Repository注释:
package aa.bb.cc.repository;
@Repository
public interface ContentRepository extends CrudRepository<Content, Long>{
}
答案 2 :(得分:1)
那么你的例子在没有xml配置的env中工作。我必须解决的两件事是:
@Id
注释(@Id private Long id = 5l;
)如果您仍然遇到此问题,我可以将其上传到我的github存储库并在此处发布链接。
答案 3 :(得分:0)
我建议您阅读official documentation,您需要在弹簧配置中激活您的存储库包,如下所示:
<repositories base-package="aa.bb.cc.repository" />