web-dev-qa-db-de.com

geschachtelte Ausnahme ist Java.lang.IllegalArgumentException: Kein verwalteter Typ: Klasse

Ich versuche, einen Spring-Boot application mit Service bereitzustellen, der auf einen JpaRepository zugreift, der zur Laufzeit mit JPA und Hibernate eine Verbindung zu PostgreSQL DB herstellt. ], unter Bezugnahme auf Verbindungseigenschaften in src/main/resources/application.properties

Wenn ich die erstellte .WAR-Datei unter Tomcat bereitstelle, kann die Anwendung nicht mit dem Fehler gestartet werden, der im folgenden Fehlerprotokoll angegeben ist.

Kann jemand bitte heraushelfen, was die Bedeutung dieses Fehlers ist?

Hinweis Ich habe die Problempunkte in den Klassen MyServiceImpl und MyRequestBody notiert, erhalte jedoch nicht den genauen Grund für den Fehler, da ich mit Spring Framework noch nicht vertraut bin.

Die Definitionen der relevanten Klassen in meinem Spring Boot Starter-Projekt lauten wie folgt:

Spring Boot-Anwendung

@SpringBootApplication
@ComponentScan(<root package name under which all subpackages containing relevant classes >)
public class MySpringBootApplication extends SpringBootServletInitializer {

Meine Spring-Boot RestController-Klasse (automatisches Verdrahten der MyService-Klasseninstanz)

@RestController
public class MyController {

    @Autowired
    MyService myService;

Mein Service-Interface (Ich habe keine @Service-Annotation bereitgestellt.)

public interface MyService {
    //all service method definitions
}

Spring ServiceImpl-Klasse mit der Autowiring-Repository-Instanz für PostgreSQL DB

@Service("myService")
public class MyServiceImpl implements MyService {

       @Autowired
       private MyRepository myRepository; <-- **ERROR -- Bean not found** 

       //other default services e.g. find, add, delete, update, etc.

}

Meine Repository-Klasse

@Repository
public interface MyRepository extends JpaRepository<MyRequestBody, Long> {
    @Query("select h from MyRequestBody h where h.column1 = ?1")
    List<MyRequestBody> findByColumn1(String col1);
}

Meine Entitätsklasse

@Entity
@Table(name = "myTable", schema = "my_db")
public class MyRequestBody { <-- **ERROR -- Not a managed class type**

     @Id
     @Column(name = "id")
     @GeneratedValue(strategy = GenerationType.SEQUENCE)
     private Long id;
     @Column(name = "column1")
     private String column1;

//getter and setter AND toString methods

Fehlerprotokoll (bei der Bereitstellung von .WAR unter Tomcat)

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-12-28 15:50:23.282 ERROR 5320 --- [ost-startStop-1] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MyController': Unsatisfied dependency expressed through field 'myService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myService': Unsatisfied dependency expressed through field 'myRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRepository': Invocation of init method failed; nested exception is Java.lang.IllegalArgumentException: Not a managed type: class com.rest.MyRequestBody

Nach dem Hinzufügen von @EntityScan ("entity_package_name")

***************************
APPLICATION FAILED TO START
***************************

Description:

Field myRepository in com.rest.MyServiceImpl required a bean of type 'com.rest.MyRepository' that could not be found.


Action:

Consider defining a bean of type 'com.rest.MyRepository' in your configuration.

29-Dec-2017 08:22:25.023 SEVERE [localhost-startStop-1] org.Apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
 org.Apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/spring-boot-service]]
        at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:167)
        at org.Apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.Java:752)
        at org.Apache.catalina.core.ContainerBase.addChild(ContainerBase.Java:728)
        at org.Apache.catalina.core.StandardHost.addChild(StandardHost.Java:734)
        at org.Apache.catalina.startup.HostConfig.deployWAR(HostConfig.Java:986)
        at org.Apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.Java:1857)
        at Java.util.concurrent.Executors$RunnableAdapter.call(Executors.Java:511)
        at Java.util.concurrent.FutureTask.run(FutureTask.Java:266)
        at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1149)
        at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:624)
        at Java.lang.Thread.run(Thread.Java:748)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'myService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myService': Unsatisfied dependency expressed through field 'myRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rest.MyRepository' 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:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:867)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:543)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.Java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.Java:693)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.Java:360)
        at org.springframework.boot.SpringApplication.run(SpringApplication.Java:303)
        at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.Java:154)
        at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.Java:134)
        at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.Java:87)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.Java:169)
        at org.Apache.catalina.core.StandardContext.startInternal(StandardContext.Java:5196)
        at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
        ... 10 more
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myService': Unsatisfied dependency expressed through field 'myRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rest.MyRepository' 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:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:202)
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.Java:208)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.Java:1138)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.Java:1066)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:585)
        ... 32 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rest.MyRepository' 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:1493)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.Java:1104)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.Java:1066)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:585)
        ... 45 more

29-Dec-2017 08:22:25.026 SEVERE [localhost-startStop-1] org.Apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive [/home/sunilg/Apache-Tomcat-8.5.24/webapps/spring-boot-service.war]
 Java.lang.IllegalStateException: ContainerBase.addChild: start: org.Apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/spring-boot-service]]
        at org.Apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.Java:756)
        at org.Apache.catalina.core.ContainerBase.addChild(ContainerBase.Java:728)
        at org.Apache.catalina.core.StandardHost.addChild(StandardHost.Java:734)
        at org.Apache.catalina.startup.HostConfig.deployWAR(HostConfig.Java:986)
        at org.Apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.Java:1857)
        at Java.util.concurrent.Executors$RunnableAdapter.call(Executors.Java:511)
        at Java.util.concurrent.FutureTask.run(FutureTask.Java:266)
        at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1149)
        at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:624)
        at Java.lang.Thread.run(Thread.Java:748)
6

Spring ist sich Ihrer Entität nicht bewusst. Sie müssen MySpringBootApplication class mit @EntityScan("your.entities.package") bezeichnen

18