spring boot解决非controller引用service的问题

controller用来完成接口的任务,而controller方法内重构出来的业务放在support类中。

之前项目的做法:将在contoller里面的service传过去做具体业务。

现在忽然不想带这个service了,就将具体service放到controller的support中去了。

controller的support类

@Component
public class FeeControllerSupport {
    @Autowired
    private AccountPayConfigService accountPayConfigService;

    /**
     * 获取报价配置
     *
     * @param userId
     * @return
     */
    public AccountPayConfig getAccountPayConfig(Integer userId) {
        AccountPayConfig accountPayConfig = accountPayConfigService.queryByUserId(userId);
        return accountPayConfig;
    }
}

在support类中加入了@Component注解,我就自然的认为他将普通类实例化到spring容器中去,然而调用接口的时候的service则为null,我就明白我的用法有错误。

在controller中的用法,将support自动装配到spring容器中和在cotroller中引用service同样的写法,这样support中的service才能正常。

@Autowired
private FeeControllerSupport feeSupport;

 


版权声明:本文为qq_20552525原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。