我正在基于诅咒创建一个简单的持久性jpa,但是代码无法正常工作,并且我找不到问题。
这是错误:

启动ApplicationContext时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。

2019-09-06 14:21:19.692错误8280 --- [main] o.s.b.d.LoggingFailureAnalysisReporter:



申请开始失败



描述:

com.restApi.java.jpa.UserDaoServiceCommandLineRunner中的字段userDAOService需要找不到“ service.UserDAOService”类型的Bean。

注入点具有以下注释:


@ org.springframework.beans.factory.annotation.Autowired(必填= true)


行动:

考虑在配置中定义类型为“ service.UserDAOService”的bean。

流程以退出代码1完成

我使用带有springboot和Intellij想法的java openjdk 12。 Windows 10上的所有内容。

User.java

package entity;

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

@Entity
public class User {
    @Id
    @GeneratedValue
    private long id;
    private String name;
    private String role;

    protected User(){

    }
    public User(String name, String role) {
        super();
        this.name = name;
        this.role = role;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getRole() {
        return role;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", role='" + role + '\'' +
                '}';
    }
}


UserDAOService.java

package service;

import entity.User;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

@Service
public class UserDAOService {
    @PersistenceContext
    private EntityManager entityManager;

    public long insert(User user){
        entityManager.persist(user);
        return user.getId();
    }
}


UserDaoServiceCommandLineRunner.java

package com.restApi.java.jpa;

import entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import service.UserDAOService;

@Component
public class UserDaoServiceCommandLineRunner implements CommandLineRunner
{
    @Autowired
    private UserDAOService userDAOService;

    private static final Logger log =
LoggerFactory.getLogger(UserDaoServiceCommandLineRunner.class);

    @Override
    public void run(String... args) throws Exception {
        User user = new User("Tom","Admin");
        long insert = userDAOService.insert(user);
        log.info("User Created"+ user);
    }
}


JpaApplication.java

package com.restApi.java.jpa;

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

@SpringBootApplication
public class JpaApplication {

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

}


该代码应显示消息“用户创建的{用户ID}”

抱歉,如果问题和信息未正确上传,这是我的第一个问题。

最好的祝福。

更新
感谢用户czpona注释,该代码现在正在运行,但仍未显示该消息。

现在,JpaApplication.java的代码如下:
包com.restApi.java.jpa;

import entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import service.UserDAOService;

@SpringBootApplication
public class JpaApplication {

    public static void main(String[] args) {
        SpringApplication.run(JpaApplication.class, args);
    }
    public class UserDaoServiceCommandLineRunner implements
CommandLineRunner {
        @Autowired
        private UserDAOService userDAOService;

        private final Logger log =
LoggerFactory.getLogger(UserDaoServiceCommandLineRunner.class);

        @Override
        public void run(String... args) throws Exception {
            User user = new User("Toto","Admin");
            long insert = userDAOService.insert(user);
            log.info("User Created"+ user);
        }
    }

}

最佳答案

我发现您的代码有两个问题

首先是您的项目结构

您扫描的所有类(实体,组件,服务..)必须位于与主类JpaApplication相同的包(或子包)中,以使Spring Boot应用程序能够对其进行扫描。

因此,类User应该在com.restApi.java.jpa.entity包下,而不是entity

UserDAOService应该在com.restApi.java.jpa.service软件包下,而不是service

就像 :

package com.restApi.java.jpa.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
     *
     *
    }


和UserDAOService类,例如:

package com.restApi.java.jpa.service;

import com.restApi.java.jpa.entity.User;
*
import javax.transaction.Transactional;

@Service
public class UserDAOService {
       *
       *


另一件事,

为了使entityManager.persist(user)起作用,必须打开一个事务,以便可以使用@Transational注释方法,例如:

import org.springframework.transaction.annotation.Transactional;
*
@Transactional
public long insert(User user){
        entityManager.persist(user);
        return user.getId();
    }

07-24 18:56