Tomcat 7数据源注入机制 [英] Tomcat 7 Datasource injection mechanism

查看:142
本文介绍了Tomcat 7数据源注入机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建简单的网络应用。并坚持数据源注入。似乎有几个问题。所以我将从困惑开始。据我所知,有两种(至少)方法将DataSource注入Servlet:

I am trying to create simple web-app. And stuck on datasource injection. There seems to be several problems. So I will start from my confusion. As I understand there's 2( at least) ways to inject the DataSource into Servlet:


  • web.xml

  • @Resource

web.xml 示例

<resource-ref>
    <res-ref-name>jdbc/MyDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <injection-target>
        <injection-target-class>ua.test.TestServlet</injection-target-class>
        <injection-target-name>dataSource</injection-target-name>
    </injection-target>
</resource-ref>

@Resource 示例

public class TestServlet extends HttpServlet{
    @Resource
    private DataSource dataSource;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws     ServletException, IOException {

我的困惑: web.xml 在我的简单项目中不能用于Tomcat 7。在我看来, web.xml 选项应该可以工作,因为在Java 5之前没有注释。请解释。

My confusion : web.xml doesn't work in Tomcat 7 on my simple project. In my opinion, web.xml option should work since there were no annotations before Java 5. Please explain.

更新:

Update:

数据源配置

<Resource name="jdbc/MyDB" 
          type="javax.sql.DataSource" 
          auth="Container"
          username="SA"
          password=""
          driverClassName="org.hsqldb.jdbcDriver"        
          url="jdbc:hsqldb:file:~/database/my_db" 
/> 


推荐答案

尝试取出注射-target web.xml 中输入并在<$ c上使用 name 属性$ c> @Resource 注释:

Try taking out the injection-target entry in web.xml and using the name attribute on the @Resource annotation:

public class TestServlet extends HttpServlet {
    @Resource(name = "jdbc/MyDB")
    private DataSource dataSource;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws     ServletException, IOException {
    }
}

这在我使用Tomcat 7.0.50的本地测试中有效。如果你正在寻找没有注释的方法,我就没有那个工作,即使它应该给他们的更改日志 1

That worked in my local testing with Tomcat 7.0.50. If you're looking for the annotation-less way of doing it, I haven't gotten that to work, even though it should given their changelog1.

编辑

I仍然没有找到解决方案,但我很好奇为什么这不起作用所以我看了注入目标代码。我发现它首先加载 context.xml 条目,然后从 web.xml 中获取设置,但是选择不覆盖它在 context.xml 中找到的配置,因为它已经看到 jdbc / MyDB 条目。我不确定如何将注入目标设置到 context.xml 或像<$这样的数据库设置c $ c> driverClassName 进入 web.xml

I still haven't found a solution, but I was curious why this doesn't work so I took a look at the injection-target code. I found that it loads the context.xml entry first, and does pick up the settings from web.xml, but chooses not to override the configuration it found in context.xml because it already sees a jdbc/MyDB entry. I'm not sure how to get the injection-target settings into context.xml or the DB settings like driverClassName into web.xml.

这篇关于Tomcat 7数据源注入机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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