在Spring Boot应用程序中创建类的bean?

问题描述

我试图直接从Spring启动应用程序中过量使用Entity Manager。但是我无法创建包含我需要的方法的类的bean。我从下一篇文章中引用。

https://dzone.com/articles/accessing-the-entitymanager-from-spring-data-jpa

ProductCategoryRepository自定义界面:-

public interface ProductCategoryRepositoryCustom {

   public void merge(ProductCategory productcategory);
   public ProductCategory find(int id);
   public void persist(ProductCategory productcategory);

}

ProductCategoryRepositoryImpl 实现 ProductCategoryRepositoryCustom :-

@Component
public class ProductCategoryRepositoryImpl implements ProductCategoryRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void merge(ProductCategory productcategory) {
        entityManager.merge(productcategory);
    }

    @Override
    public ProductCategory find(int id) {
        ProductCategory productCategory=entityManager.find(ProductCategory.class,id);
        return productCategory;
    }

    @Override
    public void persist(ProductCategory productcategory) {
          entityManager.persist(productcategory);
    }
}

配置类:-

@Configuration
public class ProductConfig {
    
    @Bean
    public ProductCategoryRepositoryImpl productCategoryRepositoryImpl(){
        return new ProductCategoryRepositoryImpl();
    } 

}

在所有这些事情之后,当我尝试使用@Autowire注释自动连接它时,我无法访问已实现的方法。 请帮助我,让我知道我做错了什么。 谢谢我前进。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)