【发布时间】:2015-07-25 11:03:20
【问题描述】:
由于某种原因,在我的配置包中,我无法自动装配字段,而在我的控制器包中,似乎没有任何问题。
例如:
servlet-context.xml
<context:component-scan base-package="service, controller, config" />
root-context.xml(也尝试在这里添加服务)
<context:component-scan base-package="security" />
这不起作用:配置包内的设置类。我收到一个空指针错误。
@Component
public class Setup { //inside the config package
@Autowired
private UserService userService; //null pointer error
/*..other stuff */
}
这确实有效:控制器包内的控制器:
@Controller
@RequestMapping(value="/contact")
public class ContactController { //inside the controller package
@Autowired
private UserService userService; //this works
/*..other stuff..*/
}
为什么它适用于控制器包而不适用于配置包?
【问题讨论】:
标签: spring spring-mvc dependency-injection spring-annotations