自动连接的依赖关系返回null(每一个)

时间:2010-09-09 04:23:35

标签: java spring autowired

Spring似乎解决了并且在启动时创建了自动装配的对象。但是当我尝试访问它们时,它们会返回null。任何人都对可能发生的事情有任何猜测?

此外,XML信息为空,因为我只允许一个超链接......

<beans  xmlns="" 
 xmlns:xsi=""
 xmlns:p="" 
 xmlns:mvc=""    
 xmlns:context=""
 xsi:schemaLocation="...only allowed one hyperlink" default-autowire="byName">

 <mvc:annotation-driven />

<context:component-scan base-package="com.blah.controller"/>
<context:component-scan base-package="com.blah.*.service"/>
<context:component-scan base-package="com.blah.*.dao"/>

<context:annotation-config />

public class AuthFilter extends UsernamePasswordAuthenticationFilter {

@Autowired
private ProfileService profileService;
    //.... Do more stuff below that shows profileService coming back as null.

}

我能够使用调试器来显示该对象正在初始化。如果你们想要的话,我可以在这里粘贴我的日志,但是这需要很多东西来编辑:)。

在定义authfilter的地方添加一点:

 <b:bean id="authenticationFilter" class="com.blah.auth.AuthFilter">
    <b:property name="authenticationManager" ref="authenticationManager" />
    <b:property name="filterProcessesUrl" value="/login/validate" />
    <b:property name="usernameParameter" value="username" />
    <b:property name="passwordParameter" value="password" />
    <b:property name="authenticationSuccessHandler" ref="authSuccessHandler" />
    <b:property name="authenticationFailureHandler" ref="authFailureHandler" />
</b:bean>

组件扫描位于springapp-servlet.xml

我已经创建了启动日志的pastebin。移动组件扫描没有骰子。 http://pastebin.com/ttC5MPnQ

1 个答案:

答案 0 :(得分:3)

  • 查看日志中是否存在可疑内容
  • 在我看来AuthFilter不是一个弹簧bean - 要么将其映射到xml配置中,要么用@Component注释它。
  • 如果您没有令人信服的理由将其设置为default-autowire="byName",请将其删除。
  • 确保您在上下文中使用该过滤器,而不是自己实例化。
  • 确保您没有引用父上下文中定义的bean(例如,applicationContext.xml中定义的bean无法访问dispatcher-servlet.xml中定义的bean)
  • component-scan(而不是servlet)中放置applicationContext.xml。 (您可以将其保留在servlet xml中,但仅限于Web包)