我在Spring Boot中遇到豆类问题

时间:2019-03-27 10:54:16

标签: java spring spring-boot javabeans

我对bean和自动装配有问题。 抱歉,简短说明。我不太懂英语。 Bean可能有什么问题?

我尝试在实体类中添加@Getter和@Setter之类的龙目岛注释,以支持json序列化。

错误代码:

,------.  ,------. ,--------.          ,-----. ,--.    ,--. ,--.  ,--. ,--.  ,-----.   ,--. ,--.       ,-----.
|  .--. ' |  .---' '--.  .--' ,-----. '  .--./ |  |    |  | |  ,'.|  | |  | '  .--./   |  .'   /      '  .-.  '
|  '--' | |  `--,     |  |    '-----' |  |     |  |    |  | |  |' '  | |  | |  |       |  .   '       |  | |  |
|  | --'  |  `---.    |  |            '  '--'\ |  '--. |  | |  | `   | |  | '  '--'\   |  |\   \ .--. '  '-'  '
`--'      `------'    `--'             `-----' `-----' `--' `--'  `--' `--'  `-----'   `--' '--' '--'  `-----'
2019-03-27 11:21:08.971  INFO 10236 --- [  restartedMain] c.p.pgssoftware.PetClinicApplication   : Starting PetClinicApplication on DESKTOP-7E5DVC0 with PID 10236 (C:\Users\ASUS\Desktop\software\target\classes started by ASUS in C:\Users\ASUS\Desktop\software)
2019-03-27 11:21:08.975  INFO 10236 --- [  restartedMain] c.p.pgssoftware.PetClinicApplication   : No active profile set, falling back to default profiles: default
2019-03-27 11:21:09.079  INFO 10236 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-03-27 11:21:09.079  INFO 10236 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-03-27 11:21:11.255  INFO 10236 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-03-27 11:21:11.439  INFO 10236 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 156ms. Found 1 repository interfaces.
2019-03-27 11:21:13.654  INFO 10236 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$dda65fa3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-27 11:21:13.707  INFO 10236 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$765427dd] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-27 11:21:13.719  INFO 10236 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-27 11:21:13.719  INFO 10236 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@1ec9ea1' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-27 11:21:13.723  INFO 10236 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$9b28ca8f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-27 11:21:13.727  INFO 10236 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-27 11:21:15.306  INFO 10236 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-03-27 11:21:15.406  INFO 10236 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-27 11:21:15.406  INFO 10236 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-27 11:21:15.458  INFO 10236 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files (x86)\Java\jdk1.8.0_191\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\ASUS\Desktop\apache-maven-3.6.0\bin;C:\Program Files\Git\cmd;C:\Users\ASUS\AppData\Local\Microsoft\WindowsApps;;.]
2019-03-27 11:21:15.830  INFO 10236 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-27 11:21:15.830  INFO 10236 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 6751 ms
2019-03-27 11:21:17.058  INFO 10236 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-03-27 11:21:17.350  INFO 10236 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-03-27 11:21:17.446  INFO 10236 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-03-27 11:21:17.562  INFO 10236 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.7.Final}
2019-03-27 11:21:17.562  INFO 10236 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-03-27 11:21:17.842  INFO 10236 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-03-27 11:21:18.154  INFO 10236 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2019-03-27 11:21:18.382  INFO 10236 --- [  restartedMain] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000421: Disabling contextual LOB creation as hibernate.jdbc.lob.non_contextual_creation is true
2019-03-27 11:21:18.394  INFO 10236 --- [  restartedMain] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@15fa875
2019-03-27 11:21:19.693  INFO 10236 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-03-27 11:21:19.729  INFO 10236 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-03-27 11:21:19.753  WARN 10236 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'downloadingService': Unsatisfied dependency expressed through field 'vetRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.petClinic.petClinic.data_operations.VetRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-03-27 11:21:19.753  INFO 10236 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-03-27 11:21:19.757  INFO 10236 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2019-03-27 11:21:19.765  INFO 10236 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2019-03-27 11:21:19.769  INFO 10236 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-03-27 11:21:19.809  INFO 10236 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-27 11:21:20.189 ERROR 10236 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'downloadingService': Unsatisfied dependency expressed through field 'vetRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.petClinic.petClinic.data_operations.VetRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at com.petClinic.petClinic.PetClinicApplication.main(PetClinicApplication.java:13) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.3.RELEASE.jar:2.1.3.RELEASE]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.petClinic.petClinic.data_operations.VetRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 24 common frames omitted


Process finished with exit code 0

实体类:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "vet")
public class Vet{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private Long id;

    @Column
    private String name;

    @Column
    private String secondName;

    @Column
    private String specialization;

    @Column
    @OneToMany(mappedBy="vet")
    private List<Surgery> surgery;


}

存储库

@Repository
public interface VetRepository extends JpaRepository<Vet, Long> {
}

服务

@Service
public class DownloadingService {

    @Autowired
    private VetRepository vetRepository;

    @Autowired
    private OwnerRepository ownerRepository;

    @Autowired
    private AnimalRepository animalRepository;

    @Autowired
    private SurgeryRepository surgeryRepository;


    public List<Vet> allVets(){
        return vetRepository.findAll();
    }

    public List<Owner> allOwners(){
        return ownerRepository.findAll();
    }

    public List<Animal> allAnimals(){
        return animalRepository.findAll();
    }

    public List<Surgery> allSurgery(){
        return surgeryRepository.findAll();
    }

    public List<Animal> getAllAnimalsByOwner(OwnerDTO ownerDTO) {
        return animalRepository.findAnimalsByOwner(ownerDTO.getId());
    }

    public Surgery getSurgeryByAnimal(AnimalDTO animalDTO) {
        return surgeryRepository.findSurgeryByAnimal(animalDTO.getId());
    }
}

控制器:

@RestController
@RequestMapping("/api/get")
public class DownloadingEndpoint {
    @Autowired
    private DownloadingService downloadingService;

    @PreAuthorize("hasAnyRole('ADMIN')")
    @GetMapping("/cos")
    public String cos(){
        return "cos";
    }

    @GetMapping("all/animals")
    public ResponseEntity<List<Animal>> listOfAllAnimals(){
        return ResponseEntity.ok(downloadingService.allAnimals().stream().sorted().collect(Collectors.toList()));
    }

    @GetMapping("all/surgery")
    public ResponseEntity<List<Surgery>> listOfAllSurgery()
    {
        return ResponseEntity.ok(downloadingService.allSurgery().stream().sorted().collect(Collectors.toList()));
    }

    @GetMapping("all/owners")
    public ResponseEntity<List<Owner>> listOfAllOwners()
    {
        return ResponseEntity.ok(downloadingService.allOwners().stream().sorted().collect(Collectors.toList()));
    }

    @GetMapping("all/vets")
    public ResponseEntity<List<Vet>> listOfAllVets()
    {
        return ResponseEntity.ok(downloadingService.allVets().stream().sorted().collect(Collectors.toList()));
    }

    @GetMapping("owner/animals")
    public ResponseEntity<List<Animal>> getAnimalsByOwner(@RequestBody OwnerDTO ownerDTO){
        return ResponseEntity.ok(downloadingService.getAllAnimalsByOwner(ownerDTO));
    }

    @GetMapping("owner/animalWithNameKristof")
    public ResponseEntity<List<Animal>> getAnimalWithName(@RequestBody OwnerDTO ownerDTO){
        return ResponseEntity.ok(downloadingService.getAllAnimalsByOwner(ownerDTO));
    }


    @GetMapping("animals/surgery")
    public ResponseEntity<Surgery> getAnimalSurgery(@RequestBody AnimalDTO animalDTO){
        return ResponseEntity.ok(downloadingService.getSurgeryByAnimal(animalDTO));
    }


}






@SpringBootApplication
@EnableAutoConfiguration
public class PetClinicApplication {

    public static void main(String[] args) {
        SpringApplication.run(PetClinicApplication.class, args);
    }

}

Package structure

我期望服务器启动

3 个答案:

答案 0 :(得分:0)

我通过在主类中添加@ComponentScan(basePackages = {“”})注释解决了我的问题。

答案 1 :(得分:-1)

您可以在应用程序的配置类中使用以下注释启用存储库:

@EnableJpaRepositories("com.petClinic.petClinic.data_operations")

假设您的存储库位于com.petClinic.petClinic.data_operations包中

答案 2 :(得分:-1)

我也遇到了相同的错误

  

不符合所有BeanPostProcessor(对于   例如:不具备自动代理资格)

通过添加@ComponentScan和在“ basePackages ”中配置的bean软件包来解决此问题。例如

@ComponentScan(basePackages = {"com.in.security", "com.in.rest","com.in.service"})