Java 正在尝试检索Springbean,但不断获取NullPointerException

Java 正在尝试检索Springbean,但不断获取NullPointerException,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在尝试从bean中检索BasicDataSource。每当我尝试访问bean时,我都会得到一个NullPointerException 我的代码: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %> <%@ page import="java.sql.*" %> <%@ page import="org.apache.c

我正在尝试从bean中检索BasicDataSource。每当我尝试访问bean时,我都会得到一个NullPointerException

我的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.dbcp2.BasicDataSource" %>
<%@ page import="org.postgresql.*" %>
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org/">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Thank you for suscribing</title>
</head>
<body>
    <%  
        try {
            BasicDataSource source = (BasicDataSource)getServletContext().getAttribute("dataSource");
            out.write(source.toString());
        } catch(Exception e) {
            out.write(e.toString());
        }
    %>
    <p>${suscriber.name}<p/>
    <p>${suscriber.email}<p/>
</body>
</html>

谢谢你的支持
${suscriber.name}

${suscriber.email}

我的servlet上下文:

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.edutainer4u.site" />



    <beans:bean id="dbURL" class="java.net.URI">
        <beans:constructor-arg
            value="'#{systemEnvironment['DATABASE_URL']}'">
        </beans:constructor-arg>
    </beans:bean>

    <beans:bean id="dataSource" 
        class="org.apache.commons.dbcp2.BasicDataSource">
        <beans:property name="url"
            value="jdbc:postgresql://localhost:10000/postgres">
        </beans:property>
        <beans:property value="blabla"
            name="username">
        </beans:property>
        <beans:property value="blabla"
            name="password">
        </beans:property>
    </beans:bean>
</beans:beans>


不管怎样,我总是得到一个NullPointerException。我做错了什么?

您的问题中没有任何内容表明您在这里使用的
ServletContext
中放置了一个名为
dataSource
的属性

BasicDataSource source = (BasicDataSource)getServletContext().getAttribute("dataSource");
您似乎被Servlet API类型的命名和
DispatcherServlet
加载的Spring(通常也称为(dispatcher)Servlet上下文)弄糊涂了。这是两个完全不同的组成部分

以下是一些相关内容:


您最好使用模型属性(请求属性)。要从JSP访问Springbean,您需要使用适当的属性名从
ServletContext
访问根
WebApplicationContext
或servlet
WebApplicationContext
,这些属性名可能会从Spring的一个版本更改到下一个版本,在视图解析器配置中,您可以添加需要在JSP中可用的bean列表:

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="exposedContextBeanNames">
      <list>
          <value>dataSource</value>
      </list>
  </property>
</beans:bean>

数据源

为什么您认为在
ServletContext
Bean中应该有一个名为
dataSource
的属性?否则我如何导入bean。