【发布时间】:2013-05-15 00:06:54
【问题描述】:
我的代码 @Inject 在一个班级中有效,但在其他班级中无效。
这是我的代码:
- context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
<context:component-scan base-package="com.myfashions.services"/>
<context:component-scan base-package="com.myfashions.dao"/>
</beans>
- SellerRetriever.java
public class SellerRetriever {
@Inject
UserDAO userDAO;
...
...
}
UserDAO 类存在于com.myfashions.dao 包中。
@Inject 在 Seller.java 中不起作用。有什么原因吗?
【问题讨论】:
-
Seller类 Spring 是否托管?还是您使用new运算符创建它? -
谢谢@nicholas.hauschild 你让我知道我的错误。我使用 new 运算符创建了该对象。所以这就是我得到所有注入 null 的原因
-
@Inject不是弹簧注解