代码之家  ›  专栏  ›  技术社区  ›  newbie

可以在@service内部使用spring 3@autowired注释吗?

  •  1
  • newbie  · 技术社区  · 16 年前

    我使用的类不是我的 @Controller 直接使用,当我尝试使用 @Autowired 在那一类中的属性定义为 @Service 财产是 null . 但是 @自动售货机 具有相同标记的将在内部工作 @控制器 .

    示例代码:

    @Service
    public class UsernameValidator implements Validator {
    
        @Autowired private UserDao userDao;
    
        // code here when used, userDao is always null
    
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Bozho    16 年前

    如果您的 applicationContext.xml :

    <context:component-scan  base-package="com.yourproject" />