Java Spring注入的资源始终为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21127225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring injected resource is always null
提问by travega
ISSUE:
问题:
I am trying to inject a service into a bean but the service instance is always null.
我正在尝试将服务注入 bean,但服务实例始终为空。
BACKGROUND:
背景:
I have two beans one called from the other. This is how they are defined in XML config:
我有两个豆子,一个从另一个豆子中调用。这是它们在 XML 配置中的定义方式:
<context:annotation-config />
<bean class="com.test.MyBeanImpl" name="myBean"/>
<bean id="myService" class="com.test.MyServiceImpl" />
and the beans are implemented like so:
和 bean 是这样实现的:
MyServiceImpl.java
MyServiceImpl.java
class MyServiceImpl implements MyService {
public void getString() {
return "Hello World";
}
}
MyBeanImpl.java
MyBeanImpl.java
@Component
class MyBeanImpl implements MyBean, SomeOtherBean1, SomeOtherBean2 {
@Resource(name="myBean")
private MyService myService;
public MyBeanImpl() {}
}
QUESTIONS:
问题:
Is there some reason related to the fact that my bean implements 3 interfaces that is preventing the Service being injected? If not what other factors could be effecting it?
是否有一些原因与我的 bean 实现了 3 个阻止注入服务的接口有关?如果不是,还有哪些其他因素会影响它?
采纳答案by Ashish Jagtap
as you are using annotations Just mark your service class with @Service
annotation and use @Autowired
annotation to get the instance of your service class
当您使用注解时只需用注解标记您的服务类@Service
并使用@Autowired
注解来获取您的服务类的实例
MyServiceImpl.java
MyServiceImpl.java
package com.mypackage.service;
@Service
class MyServiceImpl implements MyService {
public void getString() {
return "Hello World";
}
}
MyBeanImpl.java
MyBeanImpl.java
@Component
class MyBeanImpl implements MyBean, SomeOtherBean1, SomeOtherBean2 {
@Autowired
private MyService myService;
public MyBeanImpl() {}
}
also make sure you mention your package name in <context:component-scan />
element in your dispatcher file as
还要确保您<context:component-scan />
在调度程序文件的元素中提及您的包名称
<context:annotation-config />
<context:component-scan base-package="com.mypackage" />
hope this will solve your problem
希望这能解决你的问题
回答by Kevin Bowersox
Make sure the bean you are injecting MyService
into is a bean.
确保您注入MyService
的 bean 是 bean。
/* This must be a bean, either use @Component or place in configuration file */
@Component
public class SomeClass{
@Resource
private MyService myService;
}
Also make sure that within your configuration you have specified that the application uses annotation-based configuration using:
还要确保在您的配置中,您已指定应用程序使用基于注释的配置:
<context:annotation-config/>
Since your using multiple interfaces it may be best to qualify the bean with a name:
由于您使用多个接口,因此最好使用名称来限定 bean:
<bean class="com.test.MyBeanImpl" name="myBean" />
Then specify the name element on the @Resource
annotation
然后在@Resource
注解上指定name元素
@Resource(name="myBean")
private MyService myService;
Here is a Github Gistthat explains these concepts.
这是解释这些概念的Github Gist。