当前位置: 首页 > 知识库问答 >
问题:

如何在VO或者Entity类中调用springboot中的依赖注入?

洪子晋
2024-06-11

如何在VO或者Entity类中调用springboot中的依赖注入?
情况是这样的,我有个PostVO类,其中有个catids属性需要通过另一个属性catid递归得到。我把方法写到了service的实现类(CatServiceImpl)里,然后在PostVO里调用。结果出问题了:CatServiceImpl必须交给springboot的ioc容器管理才行,不然mybatis-plus就没法用,这就意味着不能new,只能在PostVO中注入CatServiceImpl,这导致了报错,无法把PostVO封装成json(可能是因为其注入了CatService的实现类的原因),报错如下:

2024-06-08T12:25:05.100+08:00  WARN 4712 --- [blog] [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Type kotlin.reflect.KProperty1 not present]

PostVO.java代码如下:

// 文章展示类@Data@Componentpublic class PostVO {    @Autowired    private ICatService catService;    private Integer postid;    // 标题    private String title;    // 分类id    private Integer catid;    // 级联分类ids(为了兼容element-plus的Cascader组件)    private Integer[] catids;    // 内容    private String content;    private String thumb;    private int posttime;    private int updatetime;    private short status;    public void setCatid(Integer catid){        this.catid = catid;        // 获取id集合        List<Integer> parentIds = catService.getParentIds(catid);        // 反转结果集        Collections.reverse(parentIds);        // 将结果以数组形式付给VO的catids        this.catids = parentIds.toArray(new Integer[0]);    }}

PostsController.java中相关代码如下:

/**     * 根据id返回文章(未作权限处理)     *      * @param id     * @return     */    @GetMapping("/{postid}")    public Result<PostVO> view(@PathVariable Integer postid) {        Post post = postService.getById(postid);        if (post != null) {            // PostVO postVO = new PostVO();            BeanUtils.copyProperties(post, postVO);            System.out.println(postVO);            // // 获取id集合            // List<Integer> parentIds = catService.getParentIds(post.getCatid());            // // 反转结果集            // Collections.reverse(parentIds);            // // 将结果以数组形式付给VO的catids            // postVO.setCatids(parentIds.toArray(new Integer[0]));            return Result.success(postVO);        } else {            return Result.fail(ResultCodeEnum.NOT_FOUND);        }    }

打印结果如下:

PostVO(catService=com.test.blog.service.impl.CatServiceImpl@52f90432, postid=22, title=我是标题哈哈哈, catid=11, catids=[2, 6, 11], content=我是内容啦啦啦啦啦啦啦啦啦, thumb=, posttime=0, updatetime=0, status=1)

之前我是把赋值放到controller里的,但是每次返回PostVO的时候都要写一遍,就想放到PostVO里,结果就出现了上面的问题。请大佬们解惑,我这种做法不对吗?不应该写到实体类中那应该写哪?

共有1个答案

杨良才
2024-06-11

应该放在 postService 中实现。

 类似资料:
  • 我有一个控制器 服务接口 我想在我的控制器中使用@autowired来使用该服务,但当我运行应用程序时,我得到以下错误 org.springframework.beans.factory.beanCreationException:创建名为“demo application”的bean时出错:注入autowired依赖项失败;嵌套异常为org.SpringFramework.Beans.Facto

  • 我想用di。在《颤栗》中,我加上这个https://pub.dartlang.org/packages/di打包我的项目,我开始读这篇文章https://webdev.dartlang.org/angular/guide/dependency-injection文章,但我不完全理解。 所以没关系:在服务类(例如:MyServices)上使用@Injectable()注释,但是如何注入其他类呢?例如

  • 本文向大家介绍webapi中如何使用依赖注入,包括了webapi中如何使用依赖注入的使用技巧和注意事项,需要的朋友参考一下 本篇将要和大家分享的是webapi中如何使用依赖注入,依赖注入这个东西在接口中常用,实际工作中也用的比较频繁,因此这里分享两种在api中依赖注入的方式Ninject和Unity;由于快过年这段时间打算了解下vue.js,所以后面对webapi的分享文章可能会慢点更新,希望支持

  • 我不想让泽西做任何依赖注射。我认为有从Jersey到Guice的桥梁,但是基于我不能在这里显示的程序需求,我需要自己创建REST控制器实例,并向Guice请求它们。 我的问题:我怎样才能禁用泽西岛的依赖注入? 我当前正在使用注释。也许我可以改用注释,以为Jersey不会在寻找这些注释。但我还是宁愿把泽西岛的依赖注射关了。我怎么能那样做?

  • 我需要在生成的mapper实现中注入一个spring服务类,以便通过 这是否适用于Mapstruct-1.0?

  • 我想在使用guice实例化子类时,将依赖项注入父类。在下面的示例中,我试图创建的一个实例,同时希望能够在运行时使用Guice注入。我该怎么做?

  • 问题内容: 在Angular.js中,可以使用依赖注入。我做了一些浏览,但找不到它的实现。React是否有类似的东西? 问题答案: React具有IoC,但没有像Angular这样的DI容器概念。就是说,没有使容器知道如何创建对象并传递依赖项,而是通过在实例化组件时将props传递给组件来显式传递它们。 虽然将依赖项传递为道具在React世界中并不常见。道具通常用于将数据传递到组件,而不是服务/存

  • 我一直在尝试将< code>webdriver注入到步骤中。我已经使用了这个说明,效果很好。 想法是将WebDriver作为服务注入到steps类中。在初始步骤,您需要添加以下依赖项。 依赖关系注入涉及三个主要类。在这里,我们逐一介绍它们。 BaseUtil BaseUtil是具有WebDriverof Selenium属性的类。这个类非常简单: 钩 钩子类包含之前和之后的