SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException

时间:2018-04-24 23:50:22

标签: python java spring spring-boot spring-data-jpa

我是SpringBoot的新手。我编写了一个示例服务,接受项目详细信息并将其存储在postgres数据库中。但是,我无法启动我的应用程序,也不确定是什么问题。我确实检查了包层次结构,一切似乎都没问题。任何帮助表示赞赏。

我没有在数据库中创建一个表,我在发送POST请求时通过JPA这样做。

ProjectPocApiApplication.java

            package com.poc;

            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.SpringBootApplication;

            @SpringBootApplication
            public class ProjectPocApiApplication {

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

Project.java

            package com.poc.project;

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

            @Entity
            public class Project {

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

                private String status;
                private String priority;
                private String manager;
                private String comments;

                public Project() {}


                public Project(String id, String name, String status, String priority, String manager, String comments) {
                    super();
                    this.id = id;
                    this.name = name;
                    this.status = status;
                    this.priority = priority;
                    this.manager = manager;
                    this.comments = comments;
                }


                public String getId() {
                    return id;
                }
                public void setId(String id) {
                    this.id = id;
                }
                public String getName() {
                    return name;
                }
                public void setName(String name) {
                    this.name = name;
                }
                public String getStatus() {
                    return status;
                }
                public void setStatus(String status) {
                    this.status = status;
                }
                public String getPriority() {
                    return priority;
                }
                public void setPriority(String priority) {
                    this.priority = priority;
                }
                public String getManager() {
                    return manager;
                }
                public void setManager(String manager) {
                    this.manager = manager;
                }
                public String getComments() {
                    return comments;
                }
                public void setComments(String comments) {
                    this.comments = comments;
                }

            }

ProjectRepository.java

            package com.poc.project;

            import org.springframework.data.repository.CrudRepository;

            public interface ProjectRepository extends CrudRepository<Project,String> {

                public Project findByName(String name);

                public void delete(String name);

            }

项目控制器

            package com.poc.project;

            import java.util.List;

            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.web.bind.annotation.PathVariable;
            import org.springframework.web.bind.annotation.RequestBody;
            import org.springframework.web.bind.annotation.RequestMapping;
            import org.springframework.web.bind.annotation.RequestMethod;
            import org.springframework.web.bind.annotation.RestController;

            @RestController
            public class ProjectController {

                @Autowired
                private ProjectService projectService;

                @RequestMapping(method=RequestMethod.GET, value="/projects")
                public List<Project> getAllProjects(){
                    return projectService.getAllProjects();
                }

                @RequestMapping(method=RequestMethod.GET, value="/projects/{name}")
                public Project getProject(@PathVariable String name){
                    return projectService.getProject(name);
                }
                @RequestMapping(method=RequestMethod.POST, value="/projects")
                public void addProject(@RequestBody Project project){
                     projectService.addProject(project);
                }
                @RequestMapping(method=RequestMethod.POST, value="/projects/{name}")
                public void updateProject(@RequestBody Project project, @PathVariable String name){
                    projectService.updateProject(name, project);
                }
                @RequestMapping(method=RequestMethod.GET, value="/projects/{name}")
                public void deleteProject(@PathVariable String name){
                    projectService.deleteProject(name);
                }

            }

项目服务

            package com.poc.project;

            import java.util.ArrayList;
            import java.util.List;


            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.stereotype.Service;

            @Service
            public class ProjectService {

                @Autowired
                private ProjectRepository projectRepository;

                public List<Project> getAllProjects(){

                    List<Project> projects = new ArrayList<>();
                    projectRepository.findAll().forEach(projects::add);
                    return projects;
                }

                public Project getProject(String name) {

                    return projectRepository.findByName(name);
                }

                public void addProject(Project project){
                    projectRepository.save(project);
                }

                public void updateProject(String name, Project project) {
                    projectRepository.save(project);
                }
                public void deleteProject(String name) {
                    projectRepository.delete(name);
                }

            }

的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.poc.project</groupId>
                <artifactId>project-poc-api</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <packaging>jar</packaging>

                <name>project-poc-api</name>
                <description>POC for Project Management Dashboard </description>

                <parent>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.0.1.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>org.postgresql</groupId>
                        <artifactId>postgresql</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>

部分控制台输出(由于字符限制)

java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498
at org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl.useContextualLobCreation(LobCreatorBuilderImpl.java:113) [hibernate-core-5.2.16.Final.jar:5.2.16.Final]

Caused by: java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc.PgConnection.createClob()
at org.postgresql.Driver.notImplemented(Driver.java:683)
at org.postgresql.jdbc.PgConnection.createClob(PgConnection.java:1252)

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectController': Unsatisfied dependency expressed through field 'projectService'; nested exception is org.springframework.beans.factory.
UnsatisfiedDependencyException: Error creating bean with name 'projectService': Unsatisfied dependency expressed through field 'projectRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'projectRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.poc.project.Project

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectService': 
Unsatisfied dependency expressed through field 'projectRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'projectRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.poc.project.Project

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.poc.project.Project

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.poc.project.Project

1 个答案:

答案 0 :(得分:-1)

您应该使用@Repository注释对存储库进行注释。