我正在基于诅咒创建一个简单的持久性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。
注入点具有以下注释:
操作:
考虑在您的配置中定义类型为“ service.UserDAOService”的bean。
以退出代码1完成的过程
我正在将java openjdk 12与springboot和Intellij想法一起使用。 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);
}
}
}
答案 0 :(得分:0)
我发现您的代码有两个问题
首先是您的项目结构
您扫描的所有类(实体,组件,服务..)必须位于与主类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();
}
答案 1 :(得分:0)
在您的JpaApplication中,您应该提及要扫描的程序包中的bean
@SpringBootApplication(scanBasePackages = {"service"})
但是您没有执行CommandLineRunner。是你想做什么?