从非managedbeans访问DAO服务方法 [英] Accessing DAO Service methods from non managedbeans
问题描述
我使用Spring 3和Hibernate 4
如何在非ManagedBean中使用以下内容
@Inject
EmployeeService employeeService
或者如果我想要访问DAO方法我必须将ManagedBean作为
@Named(mymanagedbean)
@ ViewAccessScoped
我有几个 Converter
类和为了访问 DAO
服务方法,我必须使用它作为ManagedBean,即使它们不是ManagedBeans。
什么是最好的方法来调用 DAO
服务方法?
谢谢
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屋!