@Resource使用Spring MVC和tomcat

时间:2014-11-27 18:34:45

标签: java spring spring-mvc tomcat datasource

我正在学习Spring MVC。我试图使用@Resource注入DataSource。就像这样:

Tomcat的web.xml:

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/TestDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

context.xml中:

Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="sa" password="" driverClassName="org.h2.Driver"
           url="jdbc:h2:tcp://localhost/~/test"/>

控制器代码(使用Spring MVC框架):

@Controller
public class SimpleControllerAnnotation {

//@Resource(name="dataSource")
@Resource(name="jdbc/TestDB")
private DataSource dataSource;

public DataSource getDataSource() {
    return dataSource;
}

//@Resource(name="dataSource")
@Resource(name="jdbc/TestDB")
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}

@RequestMapping("/testDataSource")
public ModelAndView testDataSource() {

    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    String name = null;
    String ID = null;

    try {
        con = dataSource.getConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery("select ID, name from STUDENT");
        while(rs.next()){
            name = rs.getString("name");
            ID = rs.getString("ID");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }finally{
            try {
                if(rs != null) rs.close();
                if(stmt != null) stmt.close();
                if(con != null) con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }

    ModelAndView mw = new ModelAndView("TestDataSourceForm");
    mw.addObject("DataSourceValue",dataSource);
    mw.addObject("Name",name);
    mw.addObject("ID",ID);

    return mw;
}

在这段代码中,我使用@Resource注入DataSource,我打算从Tomcat“获取”,我在Tomcat中设置(上面分享的web.xml和context.xml)。

当我运行此程序时,我得到以下异常:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name    'simpleControllerAnnotation': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jdbc/TestDB' is defined

jdbc / TestDB是我在Tomcat中设置的DataSource。

我有以下问题:

1)我们在Tomcat中创建的DataSource是否可以这样注入?或者我们必须使用JNDI查找。在我在互联网上阅读的一篇文章中,有人说JNDI查找有点过时,而且这些天依赖注入是首选方式。

2)通常,最佳做法是在App server / Web Container中设置DataSource或在应用程序本身中进行管理。根据我在帖子中阅读的内容,首选App server / Container来管理它。

对过去此错误的任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:1)

  1. Apache Tomcat仅在其自身加载的类(例如Filters,Servlet和Listeners)上处理@Resource注释。

    在您的情况下,您的控制器类由Spring Framework加载,Spring负责处理@Resource注释。阅读Spring文档(参考指南)。

  2. 根据Spring Reference Guide [1],@ Resource注释中的值是Spring bean的名称。

    如果您配置SimpleJndiBeanFactory,它表示该名称可用于JDNI查找,但建议不要使用它,并建议明确配置引用的bean。 - &GT; [2]

  3. [1] http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-resource-annotation

    [2] http://docs.spring.io/spring/docs/current/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-jee