从非managedbeans访问DAO服务方法 [英] Accessing DAO Service methods from non managedbeans

查看:143
本文介绍了从非managedbeans访问DAO服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring 3和Hibernate 4



如何在非ManagedBean中使用以下内容

  @Inject 
EmployeeService employeeService

或者如果我想要访问DAO方法我必须将ManagedBean作为

  @Named(mymanagedbean)
@ ViewAccessScoped

我有几个 Converter 类和为了访问 DAO 服务方法,我必须使用它作为ManagedBean,即使它们不是ManagedBeans。



什么是最好的方法来调用 DAO 服务方法?



谢谢

ApplicationContextAware ,然后设置ApplicationContext。然后你需要提供静态方法来获取bean实例。

  public class SpringApplicationContext实现ApplicationContextAware {

private static ApplicationContext CONTEXT;

public void setApplicationContext(ApplicationContext context)
抛出BeansException {
CONTEXT = context;
}
public static Object getBean(String beanName){...}
public static< T> T getBean(Class< T> arg0){...}

然后在你的非托管bean你可以通过传入 EmployeeService.class 作为参数或bean名称作为参数来调用 SpringApplicationContext.getBean 方法。

I am using Spring 3 and Hibernate 4

How can I use the following in a non ManagedBean

@Inject 
EmployeeService employeeService 

Or if I would want to access DAO method I have to make that a ManagedBean as

@Named("mymanagedbean")
@ViewAccessScoped 

I have a few Converter class and in order to access DAO service methods I had to use that as ManagedBean even though they are not ManagedBeans.

What is the best approach to call DAO service methods?

Thanks

解决方案

You will need to implement the Spring interface ApplicationContextAware and then set the ApplicationContext. Then you need provide static methods to get the bean instance.

public class SpringApplicationContext implements ApplicationContextAware {

private static ApplicationContext CONTEXT;

public void setApplicationContext(ApplicationContext context)
        throws BeansException {
    CONTEXT = context;
}
    public static Object getBean(String beanName) { ...}
    public static <T> T getBean(Class<T> arg0) {...}

Then in your non-managed bean you can call SpringApplicationContext.getBean method by passing in EmployeeService.class as the argument or the bean name as the argument.

这篇关于从非managedbeans访问DAO服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆