开发者

Autowired dependencies coming back null (Every single one of them)

开发者 https://www.devze.com 2023-01-15 23:48 出处:网络
Spring seems to resolve and create the autowired objects just fine on boot up. But when I try to access them they come back as null. Anyone have any guesses on what might be going on?

Spring seems to resolve and create the autowired objects just fine on boot up. But when I try to access them they come back as null. Anyone have any guesses on what might be going on?

Also XML info is blank as I'm only allowed one hyperlink...

<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.

}

I was able to use a debugger to show that the object was being initialized. I can paste my logs here if you guys want but that's a lot to back edit :).

Adding a bit to this where authfilter is defined:

 <b:bean id="authenticationFilter" class="com.blah.auth.AuthFilter">
    <b:property name="authenticationManager" ref="authenticationManager" />
    <b:property name="filt开发者_Go百科erProcessesUrl" 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>

The component scan is in springapp-servlet.xml

I've created a pastebin of the boot logs. No dice with moving the component scan. http://pastebin.com/ttC5MPnQ


  • see if the there is something suspicious in the logs
  • it appears to me that AuthFilter is not a spring bean - either map it in the xml config, or annotate it with @Component.
  • remove default-autowire="byName" if you don't have a compelling reason to have it set so.
  • make sure you use that filter from within the context, and not instantiating it yourself.
  • make sure you are not referring to a bean defined in a parent context (for example - a bean defined in applicationContext.xml can't access a bean defined in dispatcher-servlet.xml)
  • place a component-scan in the applicationContext.xml (not the servlet one). (You can retain the one in the servlet xml but limit it to web packages only)
0

精彩评论

暂无评论...
验证码 换一张
取 消