Spring UnsatisfiedDependencyException 且不是托管类型 : class java. lang.Object

标签 spring java

我想为两个域对象创建一个通用存储库类: 产品和类别,并能够列出index.html 中的所有元素。

这是我的代码:

@Controller
public class IndexController {

private final ProdCatService<Category> categoryProdCatService;

public IndexController(ProdCatService<Category> categoryProdCatService) {
    this.categoryProdCatService = categoryProdCatService;
}

@RequestMapping({"","/","index"})
public String getIndexPage(Model model)
{       
    model.addAttribute("cat", categoryProdCatService.getAll());
    return "index";
}
}

存储库:

public interface ProdCatRepository<T> extends CrudRepository<T, Long> { }

服务:

public interface ProdCatService<T> {

Set<T> getAll();
T findById(Long id);
}


@Service
public class ProdCatServiceImpl<T> implements ProdCatService {

private final ProdCatRepository<T> prodCatRepository;

public ProdCatServiceImpl(ProdCatRepository<T> prodCatRepository) {
    this.prodCatRepository = prodCatRepository;
}

@Override
public Set<T> getAll() {
    Set<T> productsSet = new HashSet<>();
    prodCatRepository.findAll().iterator().forEachRemaining(productsSet::add);
    return productsSet;
}

@Override
public T findById(Long id) {
    return prodCatRepository.findById(id).get();
}
}

所以问题是我遇到了一系列错误,例如:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexController' defined in file [C:\Users\Michał\Documents\webstore-v-1\target\classes\info\mike\webstorev1\controllers\IndexController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'prodCatServiceImpl' defined in file [C:\Users\Michał\Documents\webstore-v-1\target\classes\info\mike\webstorev1\service\ProdCatServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prodCatRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prodCatRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object

我正在寻求帮助。我查看了 S/O,但找不到答案。

编辑类别域类。

@Entity
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String categoryName;

@ManyToMany(mappedBy = "categories")
private Set<Product> products;

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Category other = (Category) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    return true;
}

最佳答案

I want to create a generic repository class for two domain objects: Product and Category and be able to list all elements in index.html.

你的要求毫无意义。
为了实现它,您应该在 Product 和 Category 之间定义一个基类,并在通用存储库中指定这个基类。
类别是与产品不同的概念。
因此,如果扭曲层次结构,您将无法定义这个基类。
即使您定义了它,它也不会反射(reflect)类别和产品中所需的字段。
那么在这些情况下,您希望如何在客户端正确列出它们?

那么,为什么要重新发明轮子呢?
实际上,您使用 Spring Data 来减少样板代码:

public interface ProdCatRepository<T> extends CrudRepository<T, Long> { }

这已经足够清楚并且易于声明。
不要试图太聪明地关联不应该关联的事物,并为每个事物定义一个不同的存储库:

public interface ProductRepository extends CrudRepository<Product, Long> { }
public interface CategoryRepository extends CrudRepository<Category, Long> { }

关于Spring UnsatisfiedDependencyException 且不是托管类型 : class java. lang.Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48064471/

相关文章:

java - spring 应用程序启动前的 spring boot setup logging

java - java 中的多线程未按预期工作

java - 为什么 Java 源文件进入目录结构?

泛型的 Java 反射

java - 有谁知道 Hibernate 和 java 是否可以有效地与 Access 一起工作?

spring - 如何在 POST 中发送 ClientId 和 ClientSecret,而不是在带有 ClientCredentialsAccessTokenProvider 的 header 中

java - Spring - Bean 创建中的 StackOverflowError

java - 阻止 Spring Boot 测试命中 SpringBootApplication 类的 @PostContruct

spring - 使用 JPA 2.0 和 @OneToMany 删除孤立实体的解决方法是什么?

java - 使用 Grails SortedSet 时无法访问空列表中的 first() 元素