Accessing DAO Service methods from non managedbeans

一世执手 提交于 2019-12-24 20:56:03

问题


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


回答1:


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.




回答2:


If you want to keep your Converter class clean and use dependency injection (which is highly recommended in order to be able the test the class easily) instead of the class pulling in its dependencies manually, you can use Spring's ability to configure a pre-existing object created outside of the application context. See the related section in Spring's reference documentation here.




回答3:


Here is a working example (pertinent to zagyi's answer). Application uses Spring Roo and therefore aspectj.

@FacesConverter("example.entity.converter")
@Configurable
public class EntityConverter implements Converter {

    @Resource
    MyDAO dao;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
        String value) {

        Entity obj;

        try {
            obj = dao.getEntity(Long.valueOf(value));
        } catch( NumberFormatException e ) {
            throw new ConverterException( message );
        }
        return obj;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
         Object value) {

        Entity obj = (Entity) value;
        return (obj != null) ? obj.getId().toString() : "";
    }
}

The dao class

@Repository("myDAO")
public class MyDAOImpl implements MyDAO {
    ...
}



回答4:


I have managed to get the DAO method in Converter without @Inject using the following and in EmployeeService class which implements Interface I have defined as @Service(value="employeeService")

EmployeeService employeeService = 
(EmployeeService)facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null,
"employeeService");


来源:https://stackoverflow.com/questions/15128228/accessing-dao-service-methods-from-non-managedbeans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!