Java Spring@autowired返回空值
这个特定类的成员私有字段(注释为@autowired)位于服务JAR中,当我试图在代码中获取该类的实例时,@autowired成员总是返回为null。我在我的项目的context.xml中有下面的组件扫描语句,它试图在JAR中查找基本包,但似乎null被注入到带注释的成员中。JAR内部还有一个context.xml,它具有与下面相同的组件扫描条目。JAR源代码现在无法更改,有什么我遗漏的吗Java Spring@autowired返回空值,java,spring,autowired,Java,Spring,Autowired,这个特定类的成员私有字段(注释为@autowired)位于服务JAR中,当我试图在代码中获取该类的实例时,@autowired成员总是返回为null。我在我的项目的context.xml中有下面的组件扫描语句,它试图在JAR中查找基本包,但似乎null被注入到带注释的成员中。JAR内部还有一个context.xml,它具有与下面相同的组件扫描条目。JAR源代码现在无法更改,有什么我遗漏的吗 <!-- local WEB-INF/spring-context/spring-applicati
<!-- local WEB-INF/spring-context/spring-application-context.xml -->
<context:annotation-config />
<context:component-scan base-package="blah.blah" />
您是否尝试将@Autowired替换为@Resource?我认为@Autowired是按类型连接的,而@Resource是按bean名称注入的。您正在自己构造WithinJARInterfaceImpl(通过调用
new
),因此它不是由spring管理的,因此不会被注入值。如何获得带有@Autowired
注释的null
字段的对象?显示代码。可能这只是问题中的一个输入错误,但注释是@Autowired
(大写a)。——如果这不是问题中的一个输入错误,那么它很可能是问题的根源。spring托管bean修复了它。谢谢!我应该早点把问题贴出来
//this code within my project
//wjc.getMethod() dereferencing comes back null
public class A{
WithinJARInterface wjc = new WithinJARInterfaceImpl()
List someObject = wjc.getMethod()
}
//this code interface within JAR
public interface WithinJARInterface{
public List getMethod() throws Exception();
}
//this code within JAR
public class WithinJARInterfaceImpl implements WithinJARInterface{
//below member always comes back NULL
@Autowired
private JARService jService;
public List getMethod(){.....}
}
public interface JARService{
public List method1();
}
@Service("JARService")
public class JARServiceImpl implments JARService {
public List method1(){ }
}