在拦截器中弹簧自动装配字符串bean失败了NPE

时间:2017-08-09 23:49:59

标签: spring autowired

spring handlerInterceptorAdapter的自动装配失败。

org.springframework.beans.factory.BeanCreationException: Error creating   bean with name 'pathInterceptor': 
Injection of autowired dependencies failed; nested exception is org.spring.framework.beans.factory.BeanCreationException: 
Could not autowire field:    
private java.lang.String com.mine.spring.interceptors.PathInterceptor.vendor; nested exception is java.lang.NullPointerException
INFO   | jvm 1    | 2017/08/09 23:29:41 |       at    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
INFO   | jvm 1    | 2017/08/09 23:29:41 |       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1218)
INFO   | jvm 1    | 2017/08/09 23:29:41 |       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
INFO   | jvm 1    | 2017/08/09 23:29:41 |       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
INFO   | jvm 1    | 2017/08/09 23:29:41 |       at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
INFO   | jvm 1    | 2017/08/09 23:29:41 |       at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
INFO   | jvm 1    | 2017/08/09 23:29:41 |       at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)

这是springxml文件中的snipet

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:c="http://www.springframework.org/schema/c" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.2.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
 http://www.springframework.org/schema/util
 http://www.springframework.org/schema/util/spring-util.xsd">

...

<bean id="vendor" class="java.lang.String" c:_0="MyVendor"/>

<mvc:interceptors>
<bean class="com.mine.spring.interceptors.PathInterceptor" />
</mvc:interceptors>

以下是java类的片段

@Component
public class PathInterceptor extends HandlerInterceptorAdapter {

@Autowired
@Qualifier("vendor")
private String vendor;

思想?

2 个答案:

答案 0 :(得分:0)

我认为你的String bean(heh)语法错误。

<bean id="vendor" class="java.lang.String">
    <constructor-arg value="MyVendor"/>
</bean>

答案 1 :(得分:0)

我的上下文扫描中存在某种自定义自动装配库,这使得标准自动装配变得混乱。我找不到自定义库并删除它以使自动装配正常工作,我没有追踪它。