Spring and auto-wiring: NullPointerException

有些话、适合烂在心里 提交于 2019-12-10 10:57:48

问题


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!
    }
}

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>

Any ideas?


回答1:


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.

There are two solution:

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



回答2:


Try to modify the code like this

public class MyTag extends SimpleTagSupport {

    private DocumentBuilder documentBuilder;

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

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



回答3:


You can use @Autowired if you mark your tag class as Spring bean. But it's stupid, because simple tags not caching by container. Each request creates own tag instance, but wiring happend only conteiner starts.



来源:https://stackoverflow.com/questions/8243847/spring-and-auto-wiring-nullpointerexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!