导致原因:org.springframework.beans.factory.BeanCreationException:使用 Spring Boot 2 创建名称为错误的 bean 时出错

用户3526665

我正在使用 Spring Boot 2 创建 Web 应用程序并使用 CommandLineRunner 运行该应用程序以连接 PostgreSql 数据库

1 . “链接库”接口:

package com.example.demo;

import org.springframework.data.repository.CrudRepository;

import com.example.entity.Link;

public interface LinkRepository extends CrudRepository<Link, Long> {

}

2.“链接”实体:

    package com.example.entity;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name = "link")
    public class Link {

        @Id
        @GeneratedValue
        @Column(name = "id")
        private Long id;

        @Column(name = "NAME")
        private String name;

        @Column(name = "url", unique = true)
        private String url;

        public Link(String name, String url) {
            this.name = name;
            this.url = url;
        }

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

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

    }

3. 演示应用配置:

    package com.example.demo;

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;

    import com.example.entity.Link;

    @SpringBootApplication(scanBasePackages = { "com.example" })
    public class DemoApplication {

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

        @Bean
        public CommandLineRunner demo(LinkRepository repository) {
            // TODO Auto-generated method stub      
            return (args) -> {
                repository.save(new Link("test", "link"));
                for (Link linkrepo : repository.findAll()) {
                    System.out.println(linkrepo.getName());

                }
            };
        }
    }

4. 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>

        <properties>
            <java.version>11</java.version>
        </properties>

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

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>


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

            <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
            </dependency>
        </dependencies>

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

    </project>

5. Application.properties :


  spring.datasource.url=jdbc:postgresql://localhost:5432/TestDb
  spring.datasource.username=postgres
  spring.datasource.password=root
  spring.datasource.driver-class-name=org.postgresql.Driver
  spring.jpa.hibernate.ddl-auto = create
  spring.h2.console.enabled=true
  spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
  spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
  spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect

我收到以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:在 com.example.demo.DemoApplication 中定义名称为“demo”的 bean 创建时出错:通过方法“demo”参数 0 表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“linkRepository”的 bean 时出错:init 方法调用失败;嵌套异常是 java.lang.IllegalArgumentException: Not a managed type: class com.example.entity.Link

埃尔加耶德

添加@EntityScan("com.example.entity")DemoApplication 类或将 DemoApplication 移动到 'com.example' 包,然后 spring 将扫描所有子包。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档