我遇到Spring和CrudRepository的问题,我的代码没有编译我有这个错误
<div id="headbar" style="background-color: #ffccff";>
<a href="http://www.bbc.co.uk/news" target=_blank>BBC news</a>
<a href="http://www.theguardian.com/uk" target=_blank>The Guardian</a>
</div>
<div id="footbar" style="background-color: #ffff66";>
<input type="button" onclick="location.href='http://www.facebook.com';" value="Facebook" />
<input type="button" onclick="location.href='http://www.instagram.com';" value="Instagram" />
</div>
我的代码:
Application.java
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [person.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 18 common frames omitted
PersonRepository.java
package person.application;
@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(PersonRepository repository) {
return (args) -> {
// save a couple of customers
repository.save(new Client(1,"keke","boy","kekeboy","ee","r","","","","",""));
};
}
}
Person.java
package person.repository;
@Repository
public interface PersonRepository extends CrudRepository<Client, Long> {
List<Client> findBylastname(String lastName);
}
configuration.xml文件
@Entity
public class Client {
}
你能帮我吗?
答案 0 :(得分:0)
答案 1 :(得分:0)
Spring boot建议您将主应用程序类放在其他类之上的根包中。 @EnableAutoConfiguration
注释通常放在您的主类上,并隐式为某些项定义基础search package
。例如,如果您正在编写JPA应用程序,则@EnableAutoConfiguration
带注释的类的包将用于搜索@Entity
或@Repository
项。因此,我建议您将项目结构更改为以下内容:
com
+- example
+- myproject
+- Application.java (annotated with @SpringBootApplication)
|
+- model
| +- Client.java
|
+- repository
| +- ClientRepository.java
| +- PersonRepository.java
|
+- service
| +- ClientService.java
|
使用此结构,您不需要configuration.xml
,因此您可以安全地删除它。在spring boot documentation中阅读有关此主题的更多信息。另外,请确保您的类路径中有spring-boot-starter-data-jpa
。
答案 2 :(得分:0)
你的PersonRepository实现在哪里?如果你有,你应该添加注释让接口知道它是注入的。
错误是“预计至少有一个bean”意味着他们找不到它〜
答案 3 :(得分:0)
在class Comment(EmbeddedDocument):
created = DateTimeField()
text = StringField()
class Post(Document):
comments = EmbeddedDocumentListField(Comment)
...
def add_or_replace_comment(self, comment):
existing = self.comments.filter(created=comment.created)
if existing.count() == 0:
self.comments.create(comment)
else:
existing.update(comment)
课程中添加@EnableJpaRepositories(basePackages = {"person.repository"})
注释。此批注用于启用JPA存储库。默认情况下,它将扫描Spring Data存储库的带注释配置类的包。您也可以使用Application.java
无需在@EnableJpaRepositories(basePackageClasses=PersonRepository.class)
课程中指定@Repository
。 #{1}}#1中的注释会将其标识为PersonRepository
。
只需删除@EnableJpaRepositories
文件即可。这不是必需的。