problem with spring boot component scanning

problem with spring boot component scanning



I am writing spring boot application which has two entity classes Teacher and Department. I am using JPA and H2 in-memory database. When i run the application tables are not creating in h2 database. I have specified package to scan using @componentScan annotation but i thinks spring is not finding location of entity classes.



Application class-


package com.H2DatabaseDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages="com.H2DatabaseDemo")
public class H2DatabaseDemoApplication

public static void main(String args)
SpringApplication.run(H2DatabaseDemoApplication.class, args);




Aauthor Entity-


package com.H2DatabaseDemo.model;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Author

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String title;
private String body;

public Author()

public long getId()
return id;


public void setId(long id)
this.id = id;


public String getTitle()
return title;


public void setTitle(String title)
this.title = title;


public String getBody()
return body;


public void setBody(String body)
this.body = body;




Post entity-


package com.H2DatabaseDemo.model;

import java.awt.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Post

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String name;

public Post()

public Post(long id, String name)
this.id = id;
this.name = name;


public long getId()
return id;


public void setId(long id)
this.id = id;


public String getName()
return name;


public void setName(String name)
this.name = name;




Here is my project directory structure



here is my pom.xml file-


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.H2DatabaseDemo</groupId>
<artifactId>H2DatabaseDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>H2DatabaseDemo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>



Stack trace-
:: Spring Boot :: (v2.0.4.RELEASE)



2018-09-13 18:00:07.448 INFO 12584 --- [ main] c.H.H2DatabaseDemoApplication : Starting H2DatabaseDemoApplication on DESKTOP-7NILS0D with PID 12584 (D:springCourseH2DatabaseDemotargetclasses started by Mrugesh in D:springCourseH2DatabaseDemo)
2018-09-13 18:00:07.453 INFO 12584 --- [ main] c.H.H2DatabaseDemoApplication : No active profile set, falling back to default profiles: default
2018-09-13 18:00:07.539 INFO 12584 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@17c1bced: startup date [Thu Sep 13 18:00:07 IST 2018]; root of context hierarchy
2018-09-13 18:00:09.114 INFO 12584 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$934327a5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-09-13 18:00:09.860 INFO 12584 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-09-13 18:00:09.892 INFO 12584 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-09-13 18:00:09.893 INFO 12584 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.32
2018-09-13 18:00:09.903 INFO 12584 --- [ost-startStop-1] 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 FilesJavajre1.8.0_171bin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:/Program Files/Java/jre1.8.0_171/bin/server;C:/Program Files/Java/jre1.8.0_171/bin;C:/Program Files/Java/jre1.8.0_171/lib/amd64;C:Program Files (x86)InteliCLS Client;C:Program Files (x86)Common FilesOracleJavajavapath;C:Program FilesInteliCLS Client;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;c:Program Files (x86)ATI TechnologiesATI.ACECore-Static;C:Program FilesIntelWiFibin;C:Program FilesCommon FilesIntelWirelessCommon;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Filesnodejs;C:WINDOWSSystem32OpenSSH;C:Program FilesGitcmd;C:Program FilesMicrosoft VS Codebin;C:Program FilesMicrosoft VS Codebin;C:UsersMrugeshAppDataRoamingnpm;C:UsersMrugeshAppDataLocalMicrosoftWindowsApps;C:UsersMrugeshAppDataLocalGitHubDesktopbin;C:eclipse;;.]
2018-09-13 18:00:10.046 INFO 12584 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-09-13 18:00:10.047 INFO 12584 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2516 ms
2018-09-13 18:00:10.173 INFO 12584 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-09-13 18:00:10.179 INFO 12584 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet webServlet mapped to [/console/]
2018-09-13 18:00:10.188 INFO 12584 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/
]
2018-09-13 18:00:10.189 INFO 12584 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/]
2018-09-13 18:00:10.189 INFO 12584 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/
]
2018-09-13 18:00:10.189 INFO 12584 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-13 18:00:10.392 INFO 12584 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-09-13 18:00:10.667 INFO 12584 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-09-13 18:00:10.733 INFO 12584 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-09-13 18:00:10.754 INFO 12584 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2018-09-13 18:00:10.863 INFO 12584 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core 5.2.17.Final
2018-09-13 18:00:10.864 INFO 12584 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2018-09-13 18:00:10.910 INFO 12584 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.0.1.Final
2018-09-13 18:00:11.034 INFO 12584 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2018-09-13 18:00:11.581 INFO 12584 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@6bcc3f27'
2018-09-13 18:00:11.585 INFO 12584 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-09-13 18:00:11.700 INFO 12584 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [//favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-13 18:00:12.010 INFO 12584 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@17c1bced: startup date [Thu Sep 13 18:00:07 IST 2018]; root of context hierarchy
2018-09-13 18:00:12.060 WARN 12584 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-09-13 18:00:12.147 INFO 12584 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error]" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-13 18:00:12.149 INFO 12584 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],produces=[text/html]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-09-13 18:00:12.183 INFO 12584 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/
] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-13 18:00:12.183 INFO 12584 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-13 18:00:12.558 INFO 12584 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-09-13 18:00:12.560 INFO 12584 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-09-13 18:00:12.565 INFO 12584 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-09-13 18:00:12.605 INFO 12584 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-13 18:00:12.610 INFO 12584 --- [ main] c.H.H2DatabaseDemoApplication : Started H2DatabaseDemoApplication in 5.598 seconds (JVM running for 6.203)






You don't need @ComponentScan next to that entities aren't components so they won't be detected by component-scanning. Why do you use a java.awt.List instead of a java.util.List in your entity?

– M. Deinum
Sep 12 '18 at 10:23



@ComponentScan


java.awt.List


java.util.List






Your class is not declared as a bean, so its normal that your component scan wouldn't find it, declare your class as @Component

– Elhoussine Zennati
Sep 12 '18 at 10:24






Entities aren't components and they shouldn't be.

– M. Deinum
Sep 12 '18 at 10:26







can you add @Table(name = "post") and @Table(name = "author") and try, also you may not need @ComponentScan(basePackages="com.H2DatabaseDemo"), when you add @SpringBootApplication over a class then Spring will search all components at and inside that package?

– dkb
Sep 12 '18 at 10:37



@ComponentScan(basePackages="com.H2DatabaseDemo")






Please post your pom.xml, your stack trace and eventually your application.properties file. And replace java.awt.List with List as mentioned before

– georges van
Sep 12 '18 at 10:42





3 Answers
3



Your entities classes are fine but your H2 console is configured with the default db (testdb if i'm not wrong). You should at least specify the URL in your H2 Console to match your db name or the other way around.



You may try the following approach:



1 In the application.properties file configure H2 console access control:


application.properties


spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:hddemo-app



2 Access the db from http://localhost:8080/h2-console/



3 Inside the H2 Console, Copy and paste jdbc:h2:mem:hddemo-app in the JDBC URL field. Leave the password field empty and click the connect button.


jdbc:h2:mem:hddemo-app



4 You should see the entity you specified in your project as a table.



Try configurate



hibernate.hbm2ddl.auto



value to be create by adding following line in the application.properties.


spring.jpa.hibernate.ddl-auto=create



I have the same problem so I tried this and it works.



Hope this helps.



In Spring you have two annotations for scanning:
- ComponentScan for scanning Component annotated classes and generating beans out of them (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html), and
- EntityScan for generating information for JPA and others for the entities (https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/domain/EntityScan.html)


ComponentScan


Component


EntityScan



In your case, since you are trying to generate entitites and not beans/components, you should use @EntityScan.


@EntityScan



N.B.: Spring also supports auto configuration: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html






He already uses Spring Boot and he uses the correct structure so using @EntityScan shouldn't be needed.

– M. Deinum
Sep 12 '18 at 10:43


@EntityScan



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)