简体   繁体   English

Main中的内部CrudRepository接口不起作用

[英]Inner CrudRepository interface in Main doesn't works

I was doing some tests with spring-data-jpa so to don't make a new file to do the Repository Interface I've put it in the Main as next: 我正在使用spring-data-jpa进行一些测试,以便不要制作新文件来执行存储库接口,接下来将其放在Main中:

@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(BRepository repository) {
    return (args) -> {
        log.info("Insert a");
        repository.save(new EntityA("TestA"));

        log.info("Find a " + repository.findOne(1));

    };
}


@Entity
@Table(name = "A")
public static class EntityA{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column(name = "NAME")
    private String name;

    //Getters, setters && constructors

}

public interface BRepository extends CrudRepository<EntityA, Integer> {

}

}

Then I've tested it with the @Bean CommandLineRunner to see the output but I got an UnsatisfiedDependencyException : 然后,我已经使用@Bean CommandLineRunner对其进行了测试,以查看输出,但是我得到了UnsatisfiedDependencyException

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo' defined in hello.Application: Unsatisfied dependency expressed through method 'demo' parameter 0: No qualifying bean of type [hello.Application$BRepository] found for dependency [hello.Application$BRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hello.Application$BRepository] found for dependency [hello.Application$BRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at hello.Application.main(Application.java:27) [classes/:na] 

Finally to ensure I've not done some wrong I've tested it doing the Interface repository in a new file and it works. 最后,为了确保我没有做错什么,我已经在新文件中的接口存储库中对其进行了测试,并且可以正常工作。

  • Someone knows why doesn't works the repository with a inner interface? 有人知道为什么不使用内部接口运行存储库吗?

  • Is this an issue or doesn't works for some reason? 这是一个问题还是由于某种原因而不起作用? (Or maybe I'm doing something wrong) (或者我做错了什么)

Spring doesn't see nested repositories, to make it do see, use @EnableJpaRepositories (although it's already included in @SpringBootApplication ) and set the considerNestedRepositories boolean to true. Spring没有看到嵌套库,使之确实看到,使用@EnableJpaRepositories (尽管它已经包含在@SpringBootApplication ),并设置considerNestedRepositories布尔为true。

Source post: https://stackoverflow.com/a/24485491/2816631 资料来源: https//stackoverflow.com/a/24485491/2816631

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM