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

newsomeone 发布于 2017/04/12 20:15
阅读 1K+
收藏 0

比如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)}
   

加载中
0
学而不思则罔
学而不思则罔

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

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

n
newsomeone
可以~太感谢了,java基础不过关,见笑见笑
0
Eric_林
Eric_林

贴代码

n
newsomeone
已经贴上代码了,能帮忙看下么~
0
Eric_林
Eric_林

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

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

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

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

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

0
annkee
annkee
注解@autowired换成@resource
0
张伟康
张伟康

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

n
newsomeone
嗯嗯 已经解决了 感谢~
返回顶部
顶部