請看以下代碼:
package com.stepforward.mirrorworld.p3_application_bean.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface Person_Repository extends JpaRepository
{ }
當運行以下代碼:
Person_Repository Person_Repository=( Person_Repository) Application_Context.getBean( Person_Repository.class);
報錯:No qualifying bean of type 'com.stepforward.mirrorworld.p3_application_bean.Person.Person_Repository' available
請問錯在哪?@Repository不能由Spring容器管理嗎?
你这个用的有点不对吧 ,你加入整合吧就行了这个不需要加@Repository 注解```
看一下你这个Person_Repository类的位置在哪里,是不是没有被spring管理到,看报错信息应该是你这个bean没有注入到spring中,所以在通过
spring获取bean的时候,获取不到可用的bean
主要看这个类所在的包是否被spring扫描到:
spring项目一般是application.xml配置文件中:
springboot项目中,如果没有配置扫描包,那么所以的bean都需要在启动类所在目录的包下面,如果有配置,一般是启动累的注解上面@SpringBootApplication(scanBasePackages = {"com.XX.XX"})
在你获取bean的那个类上加:@ComponentScan(basePackages = "com.stepforward.*")
去掉“@EnableAutoConfiguration(exclude =......”,加@EntityScan,@EnableJpaRepositories問題解決,但不知其所以然。謝謝大家