gpt4 book ai didi

java - Autowiring 的依赖项返回 null(每个依赖项)

转载 作者:行者123 更新时间:2023-12-04 06:46:38 24 4
gpt4 key购买 nike

Spring 似乎可以在启动时很好地解析和创建 Autowiring 的对象。但是当我尝试访问它们时,它们返回为 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

最佳答案

  • 查看日志中是否有可疑内容
  • 在我看来 AuthFilter不是 spring bean - 要么在 xml 配置中映射它,要么用 @Component 注释它.
  • 删除 default-autowire="byName"如果您没有令人信服的理由来设置它。
  • 确保您在上下文中使用该过滤器,而不是自己实例化它。
  • 确保您没有引用在父上下文中定义的 bean(例如 - 在 applicationContext.xml 中定义的 bean 不能访问在 dispatcher-servlet.xml 中定义的 bean)
  • 放置一个 component-scanapplicationContext.xml (不是 servlet 之一)。 (您可以在 servlet xml 中保留一个,但仅限于 web 包)
  • 关于java - Autowiring 的依赖项返回 null(每个依赖项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3673765/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com