Jacob
Jacob

Reputation: 14741

Accessing DAO Service methods from non managedbeans

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

Upvotes: 0

Views: 593

Answers (4)

George Suntres
George Suntres

Reputation: 11

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 {
    ...
}

Upvotes: 1

Jacob
Jacob

Reputation: 14741

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");

Upvotes: 0

zagyi
zagyi

Reputation: 17518

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.

Upvotes: 1

Dhanush Gopinath
Dhanush Gopinath

Reputation: 5739

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.

Upvotes: 1

Related Questions