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

时间:2019-08-23 14:03:18

标签: postgresql spring-boot

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

1。 “ LinkRepository”界面:

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:不是托管类型:com.example.entity.Link

2 个答案:

答案 0 :(得分:1)

如果要使用CommandLineRunner,则应该是这样的:

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

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

    @Override
    public void run(String... args) throws Exception {
       // TODO Auto-generated method stub      
       return (args) -> {
       repository.save(new Link("test", "link"));
       for (Link linkrepo : repository.findAll()) {
           System.out.println(linkrepo.getName());
       }
    };
}

我只是喜欢这样:

1)创建一个新类,例如DemoBootstrap 2)应该是这样的东西

@Component
public class DemoBootstrap implements ApplicationListener<ContextRefreshedEvent> {

private final LinkRepository categoryRepository;

public DemoBootstrap(LinkRepository linkRepository) {
    this.linkRepository = linkRepository;
}

@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent event) {
    // Here add all links that should be saved
    // for example
    linkRepository.save(new Link("foo", "bar"));
    linkRepository.save(new Link("foo2", "bar2"));

    // etc

}

答案 1 :(得分:0)

在DemoApplication类上添加@EntityScan("com.example.entity")或将DemoApplication移至“ com.example”包,然后spring将扫描所有子包。