弹簧和自动接线:NullPointerException [英] Spring and auto-wiring: NullPointerException

查看:102
本文介绍了弹簧和自动接线:NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图掌握Spring的自动装配,但似乎无法正确实例化bean(一个DocumentBuilder).我已经这样创建了一个自定义JSP标记:

I'm trying to get a grip on auto-wiring in Spring, but I can't seem to properly instantiate the bean (a DocumentBuilder). I have created a custom JSP tag as such:

public class MyTag extends SimpleTagSupport {

    @Autowired
    private DocumentBuilder documentBuilder;

    public void setBuilder(DocumentBuilder builder) {
        this.documentBuilder = builder;
    }

    @Override
    public void doTag() throws IOException {
        // documentBuilder is null in here!
    }
}

这是servlet配置:

This is the servlet configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Scan for HTTP/REST controllers -->
    <context:component-scan base-package="the.right.package" />

    <context:annotation-config/>

    <bean id="documentBuilderFactory"
          class="javax.xml.parsers.DocumentBuilderFactory"
          factory-method="newInstance">
        <property name="validating" value="false" />
        <property name="ignoringElementContentWhitespace" value="true" />
    </bean>

    <bean id="documentBuilder" class="javax.xml.parsers.DocumentBuilder"
          factory-bean="documentBuilderFactory"
          factory-method="newDocumentBuilder">
    </bean>

</beans>

有什么想法吗?

推荐答案

您只能注入春豆!但是Jsp-Tags不是Spring Bean,因此Autowird注释将被完全忽略,因此该字段为null.

You can only inject in spring beans! But Jsp-Tags are no Spring Beans, so the Autowird annotation will be completely ignored, and therefore the field is null.

有两种解决方法:

  • use the @Configurable Support. -- But that requires real AspectJ. (I have never tried it for Tags, but I guess it will work for tags like for every other normal class). @see Spring Reference: Chapter 7.8.1 Using AspectJ to dependency inject domain objects with Spring
  • Extend your tag from the abstract Spring class RequestContextAwareTag. This provides access to the WebApplicationContext via getRequestContext().getWebApplicationContext(). Then you can use the WebApplicationContext to obtain the required beans programmatic.

这篇关于弹簧和自动接线:NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆