J'essaie de déployer un Spring-Boot application
avec Service
accédant à un JpaRepository
qui se connecte à PostgreSQL DB
lors de l'exécution à l'aide de JPA
et Hibernate
, en se référant aux propriétés de connexion dans src/main/resources/application.properties
Lorsque je déploie le .WAR intégré sur Tomcat, l'application ne démarre pas avec l'erreur indiquée dans le journal des erreurs ci-dessous.
Quelqu'un peut-il s'il vous plaît aider quelle est la signification de cette erreur?
Note J'ai noté les points de problèmes dans les classes MyServiceImpl
et MyRequestBody
mais je n'obtiens pas la raison exacte de l'erreur, car je suis nouveau dans le framework Spring.
Les définitions des classes pertinentes dans mon projet Spring Boot Starter sont les suivantes:
Application Spring Boot
@SpringBootApplication
@ComponentScan(<root package name under which all subpackages containing relevant classes >)
public class MySpringBootApplication extends SpringBootServletInitializer {
Ma classe Spring-Boot RestController (câblage automatique de l'instance de classe MyService)
@RestController
public class MyController {
@Autowired
MyService myService;
Mon interface de service (je n'ai fourni aucune annotation @Service)
public interface MyService {
//all service method definitions
}
Classe Spring ServiceImpl avec l'instance de référentiel de câblage automatique pour fonctionner sur 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.
}
Ma classe Repository
@Repository
public interface MyRepository extends JpaRepository<MyRequestBody, Long> {
@Query("select h from MyRequestBody h where h.column1 = ?1")
List<MyRequestBody> findByColumn1(String col1);
}
Ma classe d'entité
@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
Journal des erreurs (lors du déploiement de .WAR sur 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
Après avoir ajouté @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)
Spring ne connaît pas votre entité. Vous devez indiquer MySpringBootApplication
classe avec @EntityScan("your.entities.package")