开发者社区> 问答> 正文

Spring service中怎么使用当前的service对象,Autowired不成功?报错

比如AuthServiceImpl下有getUser()和getPage()两个方法,在getPage中我要调用getUser()方法,使用

@Autowired(required = false)
private AuthService authService;

自动注入会报错,在getPage里面new一个AuthService对象也不可以。

像这种service中使用当前对象应该怎么注入?

 

代码如下:

第一种自动注入

public interface TestService {
   Integer getPage();
   Integer getUser();
}
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private TestService testService;
    @Override
    public Integer getPage() {
        Integer intGetUser=testService.getUser();
        return 1+intGetUser;
    }

    @Override
    public Integer getUser() {
        return 1;
    }
}

 

错误信息:

WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.seckill.service.AuthService org.seckill.service.impl.AuthServiceImpl.authService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.seckill.service.AuthService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
   

第二种:new对象

@Service
public class TestServiceImpl implements TestService {
   
    @Override
    public Integer getPage() {
        TestService testService=new TestServiceImpl();
        Integer intGetUser=testService.getUser();
        return 1+intGetUser;
    }

    @Override
    public Integer getUser() {
        return 1;
    }
}

错误信息:

org.springframework.context.support.GenericApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.seckill.service.AuthService org.seckill.service.impl.AuthServiceImpl.authService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.seckill.service.AuthService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
   

展开
收起
爱吃鱼的程序员 2020-06-08 15:36:50 807 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    this.getUser();不行?实在要用可以这样调:

    SpringContextUtil.getCurrentWebApplicationContext().getBean("authService",AuthServiceImpl.class).getUser();

    可以~太感谢了,java基础不过关,见笑见笑

    贴代码

    已经贴上代码了,能帮忙看下么~

    引用来自“学而不思则罔”的评论

    this.getUser();不行?实在要用可以这样调:

    SpringContextUtil.getCurrentWebApplicationContext().getBean("authService",AuthServiceImpl.class).getUser();

    在窗口中,还是不建议用这个方法,第1种方法不成功,可能是你配置文件不正确

    看下spring配置文件中 context:component-scan

    注解@autowired换成 @resource

    其实我觉得考虑复杂了,自己类调自己方法,this就是当前类对象,注入会死循环,new对象也会死循环!楼上有说这样的方式,很不建议这样做

    嗯嗯已经解决了感谢~
    2020-06-08 15:37:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多