关于 Spring 3、servlet、@autowired 的另一个问题
我想我已经在这里和 springsource.org 上阅读了有关 Spring 和自动装配 servlet 的所有问题和答案,但我仍然无法让它工作。
我想要做的就是在我的 servlet 中自动设置数据源。我知道容器创建 servlet 而不是 Spring。
这是我的测试 servlet 中的代码:
package mypackage.servlets;
imports go here...
@Service
public class TestServlet extends HttpServlet
{
private JdbcTemplate _jt;
@Autowired
public void setDataSource(DataSource dataSource)
{
_jt = new JdbcTemplate(dataSource);
}
etc etc
在我的 applicationContext.xml 中,我有:
<context:annotation-config />
<context:component-scan base-package="mypackage.servlets />
<import resource="datasource.xml" />
在我的 datasource.xml 中:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/db" />
如果我无法正常工作,我将只在 servlet 的 init 方法中使用 WebApplicationContextUtils,但我真的很想这样做在我读完所有的书之后,我完成了这项工作。
我正在使用 Spring 3,Java 1.6。
谢谢,
保罗
I think I've read every question and answer on Spring and autowiring a servlet, both here and at springsource.org, and I still can't get it working.
All I want to do is have the datasource automatically set in my servlets. I understand that the container creates the servlet and not Spring.
Here is code from my test servlet:
package mypackage.servlets;
imports go here...
@Service
public class TestServlet extends HttpServlet
{
private JdbcTemplate _jt;
@Autowired
public void setDataSource(DataSource dataSource)
{
_jt = new JdbcTemplate(dataSource);
}
etc etc
In my applicationContext.xml I have:
<context:annotation-config />
<context:component-scan base-package="mypackage.servlets />
<import resource="datasource.xml" />
and in my datasource.xml:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/db" />
If I can't get this working I'll just use WebApplicationContextUtils in the servlet's init method but I'd really like to make this work after all the reading I've been doing.
I'm using Spring 3, Java 1.6.
Thanks,
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要用 Spring MVC 控制器替换您的 Servlet。因为 Spring 不会注入任何由其他人创建的类(servlet),然后是 Spring 本身(@Configurable 除外)。
(要获得一个非常简单的示例,请查看 STS Spring 模板项目:MVC)。
You need to replace your Servlets by Spring MVC contollers. Because Spring will not inject anything the classes (servlets) created by someone else then Spring itselfe (except @Configurable).
(To get an very simple example, take a look at the STS Spring Template Project: MVC).
我想要做的是在我的 Servlet 中免费获取 DataSource 引用,即不在某个类上调用静态 getDatasource 方法。
以下是我学到的知识以及如何让它发挥作用:
Servlet 无法由 Spring 配置或自动装配。 Servlet 是在 Spring 的应用程序上下文加载之前创建的。请参阅问题 SPR-7801:https://jira.springsource.org/browse/SPR-7801
我所做的是在 applicationContext.xml 中创建一个 DataSource 并将其导出为属性:
在 servlet 的 init 方法中,我读取了该属性:
如果我使用 DAO 而不是从 servlet 访问数据库,那么它会是通过将它们标记为@Configurable,可以轻松地将它们连接到@Autowired,并且还能够使用@Transactional和其他Spring好东西。
What I wanted to do was get a DataSource reference in my Servlet for free, i.e. not calling a static getDatasource method on some class.
Here's what I learned and how I got it working:
Servlets cannot be configured or autowired by Spring. Servlets are created before Spring's app context is loaded. See issue SPR-7801: https://jira.springsource.org/browse/SPR-7801
What I did was create a DataSource in my applicationContext.xml and export that as a property:
In my servlet's init method I read the property:
If I'd been using DAOs instead of hitting the database from the servlets it would have been easy to wire them up for @Autowired by marking them @Configurable, and also be able to use @Transactional and other Spring goodies.