gpt4 book ai didi

java - Spring 启动组件扫描问题

转载 作者:行者123 更新时间:2023-12-01 06:03:09 24 4
gpt4 key购买 nike

我正在编写具有两个实体类Teacher和Department的spring boot应用程序。我正在使用JPA和H2内存数据库。当我运行时,应用程序表未在h2数据库中创建。我已指定要使用@componentScan批注进行扫描的程序包,但我认为spring找不到实体类的位置。

应用类别

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);
}
}


作者实体

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;
}
}


邮政实体

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

这是我的pom.xml文件-

<?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>


堆栈跟踪-
 :: Spring Boot ::(v2.0.4.RELEASE)

2018-09-13 18:00:07.448信息12584 --- [main] cHH2DatabaseDemoApplication:使用PID 12584在DSKTOP-7NILS0D上启动H2DatabaseDemoApplication(D:\ springCourse \ H2DatabaseDemo \ target \由Mrugesh在D:\ springCourse \中启动的类H2DatabaseDemo)
2018-09-13 18:00:07.453信息12584 --- [main] c.H.H2DatabaseDemoApplication:未设置活动配置文件,回退到默认配置文件:默认
2018-09-13 18:00:07.539 INFO 12584 --- [main] ConfigServletWebServerApplicationContext:刷新org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@17c1bced:启动日期[2018年9月13日星期四18:00:07 ];上下文层次结构的根
2018-09-13 18:00:09.114信息12584 --- [main] trationDelegate $ BeanPostProcessorChecker:类型为[org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'的Bean'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'类型为$$ EnhancerBySpringCGLIB $ $ 934327a5]不符合所有BeanPostProcessor的处理要求(例如:不符合自动代理的条件)
2018-09-13 18:00:09.860信息12584 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer:使用端口初始化的Tomcat:8080(http)
2018-09-13 18:00:09.892信息12584 --- [main] o.apache.catalina.core.StandardService:启动服务[Tomcat]
2018-09-13 18:00:09.893信息12584 --- [main] org.apache.catalina.core.StandardEngine:启动Servlet引擎:Apache Tomcat / 8.5.32
2018-09-13 18:00:09.903信息12584 --- [ost-startStop-1] oacatalina.core.AprLifecycleListener:在Java上找不到基于APR的Apache Tomcat本机库,该库允许在生产环境中实现最佳性能。 library.path:[C:\ Program Files \ Java \ jre1.8.0_171 \ bin; C:\ WINDOWS \ Sun \ Java \ bin; C:\ WINDOWS \ system32; 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) \ Intel \ iCLS Client \; C:\ Program Files(x86)\ Common Files \ Oracle \ Java \ javapath; C:\ Program Files \ Intel \ iCLS Client \; C:\ WINDOWS \ system32; C:\ WINDOWS; C :\ WINDOWS \ System32 \ Wbem; C:\ WINDOWS \ System32 \ WindowsPowerShell \ v1.0 \; c:\ Program Files(x86)\ ATI Technologies \ ATI.ACE \ Core-Static; C:\ Program Files \ Intel \ WiFi \ bin \; C:\ Program Files \ Common Files \ Intel \ WirelessCommon \; C:\ Program Files(x86)\ Intel \ Intel(R)管理引擎组件\ DAL; C:\ Program Files \ Intel \ Intel( R)管理引擎组件\ DAL; C:\ Program文件(x86)\ Intel \ Intel(R)管理引擎组件\ IPT; C:\ Program Files \ Intel \ Intel(R)管理引擎组件\ IPT; C:\ Program Files \ nodejs \; C:\ WINDOWS \ System32 \ OpenSSH \; C:\ Program Files \ Git \ cmd; C:\ Program Files \ Microsoft VS Code \ bin; C:\ Program Files \ Microsoft VS Code \ bin; C:\ Users \ Mrugesh \ AppData \ Roaming \ npm ; C:\ Users \ Mrugesh \ AppData \ Local \ Microsoft \ WindowsApps; C:\ Users \ Mrugesh \ AppData \ Local \ GitHubDesktop \ bin; C:\ eclipse ;;。]
2018-09-13 18:00:10.046信息12584 --- [ost-startStop-1] o.a.c.c.C. [Tomcat]。[localhost]。[/]:初始化Spring嵌入式WebApplicationContext
2018-09-13 18:00:10.047信息12584-[ost-startStop-1] o.s.web.context.ContextLoader:根WebApplicationContext:初始化在2516毫秒内完成
2018-09-13 18:00:10.173信息12584 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean:Servlet调度程序Servlet映射到[/]
2018-09-13 18:00:10.179信息12584 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean:Servlet webServlet映射到[/ console /]
2018-09-13 18:00:10.188信息12584 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:将过滤器:'characterEncodingFilter'映射到:[/]
2018-09-13 18:00:10.189信息12584 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:将过滤器:'hiddenHttpMethodFilter'映射到:[/]
2018-09-13 18:00:10.189信息12584 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:将过滤器:'httpPutFormContentFilter'映射到:[/]
2018-09-13 18:00:10.189信息12584 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:'requestContextFilter'到:[/ *]
2018-09-13 18:00:10.392 INFO 12584 --- [main] com.zaxxer.hikari.HikariDataSource:HikariPool-1-正在启动...
2018-09-13 18:00:10.667信息12584-[[main] com.zaxxer.hikari.HikariDataSource:HikariPool-1-开始完成。
2018-09-13 18:00:10.733信息12584 --- [main] j.LocalContainerEntityManagerFactoryBean:为持久性单元'default'构建JPA容器EntityManagerFactory
2018-09-13 18:00:10.754信息12584 --- [main] o.hibernate.jpa.internal.util.LogHelper:HHH000204:处理PersistenceUnitInfo [
    名称:默认
    ...]
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信息12584 --- [main] org.hibernate.cfg.Environment:HHH000206:找不到hibernate.properties
2018-09-13 18:00:10.910信息12584 --- [main] o.hibernate.annotations.common.Version:HCANN000001:Hibernate Commons Annotations {5.0.1.Final}
2018-09-13 18:00:11.034信息12584-[[main] org.hibernate.dialect.Dialect:HHH000400:使用方言:org.hibernate.dialect.H2Dialect
2018-09-13 18:00:11.581信息12584 --- [main] o.h.t.schema.internal.SchemaCreatorImpl:HHH000476:执行导入脚本'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@6bcc3f27'
2018-09-13 18:00:11.585 INFO 12584 --- [main] j.LocalContainerEntityManagerFactoryBean:持久性单元'default'的初始化JPA EntityManagerFactory
2018-09-13 18:00:11.700 INFO 12584 --- [main] oswshandler.SimpleUrlHandlerMapping:将URL路径[//favicon.ico]映射到类型为[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler ]
2018-09-13 18:00:12.010 INFO 12584 --- [main] swsmmaRequestMappingHandlerAdapter:寻找@ControllerAdvice:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@17c1bced:启动日期[9月13日,星期四: 00:07 IST 2018];上下文层次结构的根
2018-09-13 18:00:12.060 WARN 12584 --- [main] aWebConfiguration $ JpaWebMvcConfiguration:默认情况下启用spring.jpa.open-in-view。因此,可以在视图渲染期间执行数据库查询。明确配置spring.jpa.open-in-view以禁用此警告
2018-09-13 18:00:12.147信息12584-[[main] swsmmaRequestMappingHandlerMapping:将“ {[/ error]}”映射到公共org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web。 servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-13 18:00:12.149信息12584 --- [main] swsmmaRequestMappingHandlerMapping:将“ {[/ error],produces = [text / html]}”映射到公共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信息12584 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping:将URL路径[/ webjars /]映射到类型为[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]的处理程序上
2018-09-13 18:00:12.183信息12584 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping:将URL路径[/ **]映射到[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]类型的处理程序上
2018-09-13 18:00:12.558 INFO 12584 --- [main] o.s.j.e.a.AnnotationMBeanExporter:在启​​动时注册JMX暴露的bean
2018-09-13 18:00:12.560 INFO 12584 --- [main] o.s.j.e.a.AnnotationMBeanExporter:JMX暴露已自动检测到名称为'dataSource'的Bean
2018-09-13 18:00:12.565 INFO 12584 --- [main] o.s.j.e.a.AnnotationMBeanExporter:位于MBean'数据源':作为MBean向JMX服务器注册[com.zaxxer.hikari:name = dataSource,type = HikariDataSource]
2018-09-13 18:00:12.605信息12584 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat在端口8080(http)上启动,具有上下文路径''
2018-09-13 18:00:12.610信息12584 --- [main] c.H.H2DatabaseDemoApplication:在5.598秒内启动H2DatabaseDemoApplication(JVM运行6.203)

最佳答案

您的实体类很好,但是您的H2控制台配置了默认数据库(如果我没记错的话,请配置为testdb)。您至少应在H2控制台中指定URL以匹配您的数据库名称或其他方式。

您可以尝试以下方法:

1在application.properties文件中配置H2控制台访问控制:

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


2从 http://localhost:8080/h2-console/访问数据库

3在H2控制台内部,将 jdbc:h2:mem:hddemo-app复制并粘贴到JDBC URL字段中。将密码字段保留为空,然后单击连接按钮。

4您应该在表中看到在项目中指定的实体。

关于java - Spring 启动组件扫描问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52292876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com