Spring 注入bean以在整个应用程序的控制器中使用
我使用的是SpringMVC3.1.x和jets3t。 我有一个DataAccessObject,我将其实例化为单例bean 通过扩展applicationcontextloader类并将其添加到web.xml中,我成功地使其工作 编辑: 我改变了我的方法,我尝试了注射和自动连线,但它不适合我的需要。 我所做的是实现ApplicationContextAware并将其设置为bean,在代码中我使用它如下:Spring 注入bean以在整个应用程序的控制器中使用,spring,servlets,dependency-injection,controller,Spring,Servlets,Dependency Injection,Controller,我使用的是SpringMVC3.1.x和jets3t。 我有一个DataAccessObject,我将其实例化为单例bean 通过扩展applicationcontextloader类并将其添加到web.xml中,我成功地使其工作 编辑: 我改变了我的方法,我尝试了注射和自动连线,但它不适合我的需要。 我所做的是实现ApplicationContextAware并将其设置为bean,在代码中我使用它如下: ApplicationContext ctx=BannerApplicationConte
ApplicationContext ctx=BannerApplicationContext.getApplicationContext();
BannerGenericDAO bdao=(BannerGenericDAO)ctx.getBean(“dao”);
我对Spring和servlet世界都是新手。。
问题是:
谢谢 您可以在控制器中使用注释
@Controller
public class MyController{
@Autowired // or @Inject, which is more JEEish (JSR330).
private SomeDao daoService;
}
当然,“SomeDao”是单例DAO的类型。您可以在控制器中使用注释
@Controller
public class MyController{
@Autowired // or @Inject, which is more JEEish (JSR330).
private SomeDao daoService;
}
当然,“SomeDao”是您的单例DAO的类型