1

我正在使用 spring mvc 3.1.x 和 jets3t。我有一个 DataAccessObject 实例化为 Singleton bean..

我设法通过扩展 applicationcontextloader 类并将其添加到 web.xml 来使其工作

编辑:

我改变了我的方法,我尝试了注入和自动连接,但它不适合我的需要。我所做的是实现 ApplicationContextAware 并将其设置为 bean,在我使用它的代码中如下:

ApplicationContext ctx = BannerApplicationContext.getApplicationContext();
BannerGenericDAO bdao = (BannerGenericDAO) ctx.getBean("dao");

我是 Spring 和一般 servlet 世界的新手。问题是:

  1. 这样做的最佳方法是什么?这被认为是“最佳实践”吗?
  2. 您如何注入一个对象,同时保留自动装配未提供的其他方法字段?
  3. 如何让一个对象在整个应用程序中使用?

谢谢!!

4

1 回答 1

1

You could use annotations in your controller.

@Controller
public class MyController{

  @Autowired  // or @Inject, which is more JEEish (JSR330).
  private SomeDao daoService;

}

Given "SomeDao" is the type of your singleton DAO, of course.

于 2012-07-30T23:34:05.127 回答