gpt4 book ai didi

java - @Resource 使用 Spring MVC 和 tomcat

转载 作者:行者123 更新时间:2023-11-28 22:55:54 25 4
gpt4 key购买 nike

我正在学习 Spring MVC。我正在尝试使用@Resource 来注入(inject)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>

上下文.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"/>

Controller 代码(使用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 来注入(inject)数据源,我打算从 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) 是否可以通过这种方式注入(inject)我们在 Tomcat 中创建的 DataSource?或者我们必须使用 JNDI 查找。在我在互联网上读到的一篇帖子中,有人说 JNDI 查找有点过时,现在依赖注入(inject)是首选方式。

2) 一般来说,最好的做法是在应用程序服务器/Web 容器中设置数据源,还是在应用程序本身中进行管理。从我阅读的帖子来看,最好让 App 服务器/容器来管理它。

非常感谢任何解决此错误的帮助。

最佳答案

  1. Apache Tomcat 仅在它自己加载的类(例如过滤器、Servlet 和监听器)上处理 @Resource 注释。

    在您的情况下,您的 Controller 类由 Spring Framework 加载,而 Spring 负责处理 @Resource 注释。阅读 Spring 文档(引用指南)。

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

    它表示,如果您配置 SimpleJndiBeanFactory,该名称可用于 JDNI 查找,但建议不要这样做,并建议显式配置引用的 bean。 -> [2]

[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

关于java - @Resource 使用 Spring MVC 和 tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27177058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com