比如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)}
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对象也会死循环!楼上有说这样的方式,很不建议这样做
嗯嗯已经解决了感谢~版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。