为了账号安全,请及时绑定邮箱和手机立即绑定

'com.example.demo.GirlRepository' that could not be found.GirlRepository(已解决)

Description:

Field girlRepository in com.example.demo.GirlController required a bean of type 'com.example.demo.GirlRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.demo.GirlRepository' in your configuration.


GirlRepository和GirlApplication都在同一个包下面的

http://img1.sycdn.imooc.com//592d260b00016b0204400258.jpg

已解决

正在回答

10 回答

https://img1.sycdn.imooc.com//5b3a31d800010b4e06260172.jpg大兄弟,帮我看下,我用的是这个jpa但是为什么还是注入失败呢?

0 回复 有任何疑惑可以回复我~
#1

深蓝d

我和你一样,你知道怎么解决了吗
2019-05-17 回复 有任何疑惑可以回复我~

还有一种可能,少写了

public class GirlService {

上面的注解@Service

0 回复 有任何疑惑可以回复我~
  1. 你把运行的application类放到最基本的包里,比如:com.imooc 这个包下!

0 回复 有任何疑惑可以回复我~

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@13c10b87: startup date [Mon Aug 21 22:25:23 CST 2017]; root of context hierarchy

at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:230) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at com.example.demo.DemoApplication.main(DemoApplication.java:10) [classes/:na]


2017-08-21 22:25:24.528 ERROR 46202 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception


java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@13c10b87: startup date [Mon Aug 21 22:25:23 CST 2017]; root of context hierarchy

at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:230) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]

at com.example.demo.DemoApplication.main(DemoApplication.java:10) [classes/:na]

我包这个错不知道是啥问题

0 回复 有任何疑惑可以回复我~

这个问题我也存在 但是把所有类放到一个包下面就好了


0 回复 有任何疑惑可以回复我~
#1

qq_Weakmindedyo_0

@SpringBootApplication 这个注解的类要放在最外围的包里,一遍能管理到其他 controller
2017-09-22 回复 有任何疑惑可以回复我~

复制的时候 多了这个<scope>test</scope>在 pom中

0 回复 有任何疑惑可以回复我~

我跟你的错误一样,但是用了import org.springframework.data.jpa.repository.JpaRepository;还是没解决/(ㄒoㄒ)/~~还可能是什么原因吗

0 回复 有任何疑惑可以回复我~
#1

未卜先知 提问者

jap在maven中注意要用spring-boot start那个,不要用spring data下的 我是在写代码的时候直接用工具导入的,结果工具导入的依赖是spring data的jpa不是spring boot的jpa
2017-06-14 回复 有任何疑惑可以回复我~
#2

glwindy 回复 未卜先知 提问者

灰常感谢
2017-06-15 回复 有任何疑惑可以回复我~
#3

qq_别人家的小明_0

那个,我还是没搞懂,我导入的是 spring-boot-starter-data-jpa,结果还是报的这个错误。。。
2018-01-04 回复 有任何疑惑可以回复我~

是的,是要注入GirlRepository,和GirlProperties没关系,要不你对比下我的,我能执行。或者你的代码idea有提示错误吗?

package com.girl.repository;

import com.girl.domain.Girl;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
* Created by zouzhi on 2017/5/30.
*/
public interface GirlRepository extends JpaRepository<Girl,Integer> {
   public List<Girl> findByAge(Integer age);
}



package com.girl.controller;

import com.girl.domain.Girl;
import com.girl.repository.GirlRepository;
import com.girl.service.GirlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* Created by zouzhi on 2017/5/30.
*/
@RestController
public class GirlController {


   private final static Logger logger = LoggerFactory.getLogger(GirlController.class);

   @Autowired
   private GirlRepository girlRepository;

   @Autowired
   private GirlService girlService;

   @GetMapping(value = "/girls")
   public List<Girl> girlList(){
       //System.out.println("girlList()");
       logger.info("girlList()");
       return  girlRepository.findAll();
   }

   @PostMapping(value = "/girls")
   public Girl add(@Valid Girl girl,
                   BindingResult bindingResult){
       if(bindingResult.hasErrors()){
           //System.out.println(bindingResult.getFieldError().getDefaultMessage());
           logger.info(bindingResult.getFieldError().getDefaultMessage());
           return null;
       }
       return girlRepository.save(girl);

   }
  /* public Girl add(@RequestParam("cupSize")String cupSize,
                     @RequestParam("age")Integer age){
       Girl girl = new Girl();
       girl.setAge(age);
       girl.setCupSize(cupSize);

       return girlRepository.save(girl);

   }*/

   @GetMapping(value = "/girls/{id}")
   public Girl get(@PathVariable("id")Integer id){
       return girlRepository.findOne(id);
   }

   @PutMapping(value = "/girls/{id}")
   public Girl update(@PathVariable("id")Integer id,
                      @RequestParam("cupSize")String cupSize,
                      @RequestParam("age")Integer age){
       Girl girl = new Girl();
       girl.setId(id);
       girl.setCupSize(cupSize);
       girl.setAge(age);

       return girlRepository.save(girl);

   }

   @DeleteMapping(value = "/girls/{id}")
   public void delete(@PathVariable("id")Integer id){
       girlRepository.delete(id);
   }

   @GetMapping(value = "girls/age/{age}")
   public List<Girl> girlListByAge(@PathVariable("age")Integer age){
       return  girlRepository.findByAge(age);
   }

   @PostMapping(value = "/girls/two")
   public void girlTwo(){
       girlService.insertTwo();
   }

}

0 回复 有任何疑惑可以回复我~
#1

未卜先知 提问者

解决了,写的时候按照idea提示加了Spring下的jpa,不是Spring boot下的jpa,所以找不到 import org.springframework.data.jpa.repository.JpaRepository; 这个导入是一模一样的
2017-05-30 回复 有任何疑惑可以回复我~
#2

为伊尘心 回复 未卜先知 提问者

我也没想到这个
2017-05-30 回复 有任何疑惑可以回复我~
#3

我爱lyf 回复 未卜先知 提问者

我可以问一下,这是什么意思吗?我也出现了同样的问题,百度了一下午,不知道该怎么解决~
2017-10-20 回复 有任何疑惑可以回复我~
#4

深蓝d 回复 未卜先知 提问者

所以是怎么改的呢
2019-05-16 回复 有任何疑惑可以回复我~
查看1条回复

@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {


黑体文字加了吗?

回复你不能加黑体,你看这个回复  黑体部分


0 回复 有任何疑惑可以回复我~
#1

未卜先知 提问者

GirlProperties这个类前面肯定是加了@Component的 但是要注入的是GirlRepository,不是GirlProperties,这两者没关系啊
2017-05-30 回复 有任何疑惑可以回复我~

@RestController
public class GirlController {

   @Autowired
   private  GirlRepository girlRepository;


注意上面加粗的文字

0 回复 有任何疑惑可以回复我~
#1

未卜先知 提问者

@RestController public class GirlController { @Autowired private GirlRepository girlRepository; @GetMapping(value = "/girls") public List<Girl> girlList(){ return girlRepository.findAll(); } } 这个 按道理 我都写了啊
2017-05-30 回复 有任何疑惑可以回复我~
#2

为伊尘心 回复 未卜先知 提问者

@Component @ConfigurationProperties(prefix = "girl") public class GirlProperties { 黑体文字加了吗?
2017-05-30 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
2小时学会Spring Boot
  • 参与学习       151599    人
  • 解答问题       1079    个

Spring Boot入门视频教程,你将学会使用Spring Boot快速构建应用程序

进入课程

'com.example.demo.GirlRepository' that could not be found.GirlRepository(已解决)

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信