java 为什么 Spring 没有将我的 @Autowired 成员连接到依赖 jar 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11134201/
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
Why is Spring not wiring my @Autowired members in a dependent jar?
提问by MStodd
I'm building a Google App Engine app using Spring 3.1 and am having a problem getting members in one of my jars wired.
我正在使用 Spring 3.1 构建一个 Google App Engine 应用程序,并且在连接我的一个 jars 中的成员时遇到了问题。
I have three projects:
我有三个项目:
server
server.model
server.persistence
server
server.model
server.persistence
I have an ant build script so that when my workspace builds, it creates jars for server.model
and server.persistence
, and puts them in the correct lib directory for the server
project.
我有一个 ant 构建脚本,因此当我的工作区构建时,它会为server.model
和创建 jar server.persistence
,并将它们放在项目的正确 lib 目录中server
。
In server
, I can autowire things from both server.model
and server.persistence
, but in server.model
my server.persistence
beans aren't getting wired even though they're the exact same as in server
.
在 中server
,我可以从server.model
和 中自动连接东西server.persistence
,但是在server.model
我的server.persistence
bean 中,即使它们与server
.
snippet from my servlet application config:
来自我的 servlet 应用程序配置的片段:
<context:component-scan base-package="com.impersonal.server"/>
<bean autowire="byType" id="appEngineDataStore" class="com.impersonal.server.persistance.AppEngineDataStore"/>
<bean autowire="byType" id="userList" class="com.impersonal.server.model.UserList"/>
I have the following code in both the server
project and the server.model
project, and only the server one gets fulfilled. Here's the one failing:
我在server
项目和server.model
项目中都有以下代码,只有服务器一个得到满足。这是失败的一个:
package com.impersonal.server.model;
import java.util.ArrayList;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import com.impersonal.server.persistance.AppEngineDataStore;
import com.impersonal.server.persistance.IDataStore;
public class UserList extends ArrayList<User>
{
private UserList(){}
//this is always null, but the same line in a class in the other project works
private @Autowired AppEngineDataStore _dataStore;
public UserList(UUID userId, String tempId)
{
String poo = "poo";
poo.concat("foo ");
int i = 3;
}
}
Edit:Just did a test in the server.model
project trying to @Autowired something that I don't have defined as a bean in my application config, and didn't get any errors. I should have got a 'no such bean found' error like I do if I do the same thing for the server
project.
编辑:刚刚在server.model
项目中做了一个测试,试图@Autowired 一些我没有在我的应用程序配置中定义为 bean 的东西,并且没有得到任何错误。如果我为server
项目做同样的事情,我应该得到一个“没有找到这样的 bean”的错误。
Any ideas why?
任何想法为什么?
采纳答案by MStodd
I was instantiating my objects incorrectly. For framework objects and such like MVC controllers, you don't need to do anything to get your @Autowired members wired.
我错误地实例化了我的对象。对于框架对象和诸如 MVC 控制器之类的东西,你不需要做任何事情来连接你的 @Autowired 成员。
For objects I was creating on the fly, I wasn't going through the IOC container, that's why their dependencies weren't being fulfilled.
对于我动态创建的对象,我没有通过 IOC 容器,这就是它们的依赖项没有得到满足的原因。
回答by Nandkumar Tekale
<context:component-scan/>
tag searches for annotated classes.
<context:component-scan/>
标签搜索带注释的类。
If you are going to autowire class using @Autowire
annotation, Autowiring class should be annotated with one of stereotype annotation (@Component,@Controller,@Service,@Repository). Spring resolves first annotation configuration and then xml configuration. This is written in spring doc as
如果您打算使用@Autowire
注释自动装配类,则应使用构造型注释(@Component、@Controller、@Service、@Repository)之一对自动装配类进行注释。Spring先解析注解配置,再解析xml配置。这是在spring doc中写的
Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches.
注解注入在 XML 注入之前执行,因此后一种配置将覆盖通过两种方法连接的属性的前一种。
Check proof on spring doc.
检查spring 文档上的证明。
So what you need to do is add annotations for classes from server
project as well as server.model
. Same in case of your third project server.persistence
. Add annotations according to layers or functionality.
因此,您需要做的是为server
项目中的类以及server.model
. 在您的第三个项目的情况下也是如此server.persistence
。根据层或功能添加注释。
回答by ThreaT
Try:
尝试:
@Autowired(required = true)
private AppEngineDataStore _dataStore;
Instead of:
代替:
private @Autowired AppEngineDataStore _dataStore;
EDIT 1:
编辑 1:
While using the autowire above, in your spring xml, try:
在使用上面的自动装配时,在你的 spring xml 中,尝试:
<bean id="appEngineDataStore" class="com.impersonal.server.persistance.AppEngineDataStore" scope="prototype"></bean>
Instead of:
代替:
<bean autowire="byType" id="appEngineDataStore" class="com.impersonal.server.persistance.AppEngineDataStore"/>
回答by Bozho
autowire
in xml means slightly different thing. Instead of defining your bean in xml, you can annotate it as @Service
, it will be discovered by the component-scan and the @Autowired
will work.
autowire
在 xml 中的意思略有不同。您可以将 bean 注释为@Service
,而不是在 xml 中定义 bean ,它会被组件扫描发现并且@Autowired
会起作用。
回答by jddsantaella
In your xml configuration, use autowire-candidate
property
在您的 xml 配置中,使用autowire-candidate
属性
<bean autowire="byType" id="appEngineDataStore" class="com.impersonal.server.persistance.AppEngineDataStore" autowire-candidate="true" />