Spring注解@Inject不起作用 [英] Spring annotation @Inject doesn't work

查看:315
本文介绍了Spring注解@Inject不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码@Inject在一个类中可用,但在另一类中不可用. 这是我的代码:

I have the code @Inject works in one class but not in other. Here's my code:

  • 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中不起作用.有什么原因吗?

    UserDAO class is present in com.myfashions.dao package. @Inject is not working in Seller.java. Any reason why?

    推荐答案

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

    I found my mistake, I'm posting this because in case anyone has the same problem. I used new operator to create an SellerRetriver object. Inject won't work if new operator is used to call that particular class.

    这篇关于Spring注解@Inject不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆