【问题标题】:Spring annotation @Inject doesn't work弹簧注解@Inject 不起作用
【发布时间】: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 不是弹簧注解

标签: java spring


【解决方案1】:

要符合扫描条件,您的课程必须使用更通用的@Component@Service@Repositories 等进行注释。在您的情况下,@Service 在逻辑上更适合。 然后,您可以(如果需要)定义一些专门针对服务调用的方面 (AOP)。

此外,您可能希望使用 @Autowired 而不是 @Inject 来检索您的 bean。

有关这两个注释的差异的更多信息:

What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

您可以在下面看到我的评论,它解释了保留@Autowired 而不是@Inject 的一个很好的理由。

【讨论】:

  • 这是一个很好的答案,但我仍然收到 NullPointerException。
  • 我认为你可以使用 @Inject@Autowiredcomponent-scan... 只要你使用 Spring 3。
  • 在您的类声明上方添加@Component 或@Service 或@Repositories,否则将不会被扫描。
  • @Mik378 这是一个用例,Spring 2 和 3 之间的遗留支持。问题没有提到这样的要求,但是可以。还有其他一些事情可能会让您远离@Autowired,例如希望使用注释按名称连接bean。在这种情况下,Spring 建议您使用@Resource (static.springsource.org/spring/docs/3.1.x/…)。您可能还想转向 Java 标准(如 @Inject),以防您切换到不同的依赖注入框架。
  • 由于 @Inject 是一个 java 注释,而 @Autowired 是 spring 依赖,如果你想改变 DI 框架,@Inject 有一个优势。
【解决方案2】:

确保SellerRetrieverUserDAO 的实现都为组件扫描进行了注释。这将确保后者被注入到前者中:

@Service
public class SellerRetriever {
    @Inject
    UserDAO userDAO;
    ...
}

使用@Component 注释UserDAO 实现。

扫描多条路径时使用:

<context:component-scan base-package="com.myfashions.services, com.myfashions.dao"/>

【讨论】:

  • UserDAO 被注入到其他服务类中,但没有被注入到 SellerRetriever.java
  • 我假设SellerRetrievercom.myfashions.services 中。尝试在应用程序上下文中使用单个组件扫描标记
  • @Reimeus 这是一个有趣的建议。 Spring 是否可以进行第一次组件扫描并错过UserDAO,因为它直到第二次组件扫描才开始处理它?
  • @davidfmatheson TBH 两者是等价的,只是一个偏好。怀疑这里的问题可能是一个旧的类版本。重建应该确定是否是这种情况
【解决方案3】:

我发现我的错误,我发布这个是因为以防万一有人遇到同样的问题。我使用 new 运算符创建了一个 SellerRetriver 对象。如果使用 new 运算符调用该特定类,则注入将不起作用。

【讨论】:

    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2011-06-22
    相关资源
    最近更新 更多