Spring 和自动装配:NullPointerException

发布于 2024-12-17 18:45:14 字数 1937 浏览 1 评论 0原文

我试图在 Spring 中掌握自动装配,但我似乎无法正确实例化 bean(一个 DocumentBuilder)。我已经创建了一个自定义 JSP 标记,如下所示:

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 配置:

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魄砕の薆 2024-12-24 18:45:14

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

有两种解决方案:

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:

那支青花 2024-12-24 18:45:14

尝试像这样修改代码

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

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!
    }
}
萌化 2024-12-24 18:45:14

如果将标记类标记为 Spring bean,则可以使用 @Autowired。但这很愚蠢,因为简单的标签不被容器缓存。每个请求都会创建自己的标签实例,但仅在容器启动时才进行连接。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文