@Resource没有注入CDI托管bean,但在web servlet中工作正常

时间:2016-02-29 08:37:39

标签: java-ee jdbc resources cdi managed-bean

我正在尝试使用JDBC从我的数据库中读出很多名字,它在我的名为HelloServlet的servlet中运行良好。我实际上能够用一堆名字回复GET请求。

@WebServlet(name = "helloServlet", value = "/hello")
public class HelloServlet extends HttpServlet {
    @Resource(lookup = "java:global/employeesDS")
    DataSource ds;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<String> list = new ArrayList<>();

        try(Connection connection = ds.getConnection()) {
            Statement statement = connection.createStatement();
            ResultSet result = statement.executeQuery("SELECT first_name FROM employees");

            while(result.next()) {
                list.add(result.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        PrintWriter pw = resp.getWriter();
        for(String name : list) {
            pw.write(name + " " );
        }
    }
}

当我在CDI托管bean中尝试相同的代码时,ds仍为null,导致NullPointerException麻烦:

@Named("dataFetchBean")
@RequestScoped
public class DataFetchBean {
    @Resource(lookup = "java:global/employeesDS")
    DataSource ds;
    List<String> questions;

    public List<String> getQuestions() {
        try(Connection connection = ds.getConnection()) {
            Statement statement = connection.createStatement();
            ResultSet result = statement.executeQuery("SELECT first_name FROM employees");

            while(result.next()) {
                questions.add(result.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return questions;
    }
}

如果相关,这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <data-source>
        <name>java:global/employeesDS</name>
        <class-name>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</class-name>
        <server-name>localhost</server-name>
        <port-number>3306</port-number>
        <database-name>employees</database-name>
        <user>root</user>
        <password />
    </data-source>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

这是我正在尝试使用DataFetchBean CDI托管bean的index.xhtml文档:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
    <title>Hello, JDBC!</title>
</h:head>
<h:body>
    <table>
        <tr>
            <td>First name</td>
        </tr>
        <c:forEach items="#{dataFetchBean.questions}" var="question">
            <tr>
                <td>
                    #{question}
                </td>
            </tr>
        </c:forEach>
    </table>
</h:body>
</html>

我正在使用Wildfly 10.0.10。提前谢谢!

2 个答案:

答案 0 :(得分:0)

解决方案是在Wildfly的控制台的帮助下创建一个非XA数据源。这包括向Web容器添加mysql连接器驱动程序。之后,您可以轻松地将数据源注入您的应用程序。

答案 1 :(得分:0)

问题解决了,但servlet应该很薄。通过分层架构,您可以获得更灵活的解决方案。