问题描述
我正在和Kotlin学习Spring(启动)。 我设法通过JPA信息库建立了一个可运行的Spring应用程序。
interface APixRepo : org.springframework.data.repository.Repository<APix,EroPixKulcs>
{
@Query( value="select * from felhaszn",nativeQuery = true
)
fun sokasag(): Iterable<APix>
}
@RestController
@RequestMapping("/apix")
class AKontroller(private val dao: APixRepo)
{
@GetMapping("/sokasag")
fun sokasag() = dao.sokasag()
}
其中APix是我的实体类。还有另外两个相似的实体以及仓库和控制器。
(本机SQL对我很重要,这是我最多的专家。)
而且由于这三个实体位于三个不同的数据库中,所以我不得不使用描述为here的XxxDataSourceConfiguration魔术(Xxx = APix和另外两个)。
而且仍然有效。
现在我意识到,回购方法对我不利,因为我的SQL必须是动态的,并且您只能在@Query(answer)中编写常量SQL。 所以我根据this question and answer将我的仓库改成了DAO:
interface APixDAO
{
fun sokasag(): Iterable<APix>
}
class APixRepoImpl constructor(@PersistenceContext var em: EntityManager)
{
fun sokasag(): Iterable<APix>
{
return em.createNativeQuery("select * from felhaszn").getResultList() as Iterable<APix>
}
}
interface APixRepo : org.springframework.data.repository.Repository<APix,EroPixKulcs>,APixDAO {}
现在问题开始了。在这种状态下(没有任何其他注释),错误是这样的:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.zz.sokdb.apix.APixRepoImpl required a single bean,but 3 were found:
- org.springframework.orm.jpa.SharedEntityManagerCreator#0: defined by method 'createSharedEntityManager' in null
- org.springframework.orm.jpa.SharedEntityManagerCreator#1: defined by method 'createSharedEntityManager' in null
- org.springframework.orm.jpa.SharedEntityManagerCreator#2: defined by method 'createSharedEntityManager' in null
Action:
Consider marking one of the beans as @Primary,updating the consumer to accept multiple beans,or using @Qualifier to identify the bean that should be consumed
和
ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AKontroller' defined in file [/home/laca/spring/kotlin/zz/sokdb/build/classes/kotlin/main/com/zz/sokdb/AKontroller.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'APixRepoImpl' defined in file [/home/laca/spring/kotlin/zz/sokdb/build/classes/kotlin/main/com/zz/sokdb/apix/APixRepoImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 3: org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1,org.springframework.orm.jpa.SharedEntityManagerCreator#2
我尝试过:
class AKontroller(@org.springframework.beans.factory.annotation.Qualifier("apixEntityManager") private val dao: APixRepo)
现在出现错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AKontroller' defined in file [/home/laca/spring/kotlin/zz/sokdb/build/classes/kotlin/main/com/zz/sokdb/AKontroller.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zz.sokdb.apix.APixRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier("apixEntityManager")}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:797) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:227) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at...
at...
at...
我尝试过:
class APixRepoImpl constructor(@PersistenceContext @org.springframework.beans.factory.annotation.Qualifier("apixEntityManager") var em: EntityManager)
现在出现错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.zz.sokdb.AKontroller required a single bean,but 3 were found:
- apixEntityManager: defined by method 'apixEntityManager' in class path resource [com/zz/sokdb/apix/APixDataSourceConfiguration.class]
- eroEntityManager: defined by method 'eroEntityManager' in class path resource [com/zz/sokdb/ero/EroDataSourceConfiguration.class]
- smpixEntityManager: defined by method 'smpixEntityManager' in class path resource [com/zz/sokdb/smpix/SMPixDataSourceConfiguration.class]
Action:
Consider marking one of the beans as @Primary,or using @Qualifier to identify the bean that should be consumed
当我在两个地方都尝试注解时,错误就和我只在AKontroller中尝试时一样。
这时我放弃了,请过来寻求帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)