0

This is a question about the use of Spring for business objects and how they are stored after being injected into classes that need them.

In a typical Spring setup, you might have a business object as follows:

package app.service.impl;

public class MaintainStudent implements app.service.intf.MaintainStudent
{
    protected app.dao.intf.CourseDAO courseDAO;
    protected app.dao.intf.StudentDAO studentDAO;
    protected app.dao.intf.CourseAssignDAO courseAssignDAO;

    //injection methods here
    public void setCourseDAO(app.dao.intf.CourseDAO courseDAO)
    {
        this.courseDAO = courseDAO;
    }

    public void setStudentDAO(app.dao.intf.StudentDAO studentDAO)
    {
        this.studentDAO = studentDAO;
    }

    public void setCourseAssignDAO(app.dao.intf.CourseAssignDAO courseAssignDAO)
    {
        this.courseAssignDAO = courseAssignDAO;
    }
    //end injection methods

    //example business method
    @Override
    @Transactional(rollbackFor={Exception.class})
    public void assignCourse(StudentKey studenKey, CourseKey courseKey)
    {
        courseAssignDAO.assignCourse(studentKey, courseKey);
    }

    //rest of the class here
}

The bean XML entries for the above might look like this:

<bean id="baseDAO" class="app.dao.base.BaseDAO" abstract="true">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="courseDAO" class="app.dao.impl.CourseDAO"
    parent="baseDAO">
</bean>

<bean id="studentDAO" class="app.dao.impl.StudentDAO"
    parent="baseDAO">
</bean>    

<bean id="courseAssignDAO" class="app.dao.impl.CourseAssignDAO"
    parent="baseDAO">
</bean>

<bean id="maintainStudent" class="app.service.impl.MaintainStudent">
    <property name="courseDAO" ref="courseDAO" />
    <property name="studentDAO" ref="courseAssignDAO" />
    <property name="courseAssignDAO" ref="studentDAO" />
</bean>

My question is, is it ok to store the injected beans in a HashMap instead of in individual interface variables? I ask this question because I am working on a porting project for an applicaion I support and I am trying to replace the application's EJB / JDBC transaction architecture with Spring. I am using Struts2 and Spring as the new architecture for the application. I am using Spring in this project primarily for its Database Transaction capabilities. The existing application uses factory classes to instantiate everything, and I have come up with a way of converting the architecture to Dependency Injection using the factory classes by storing the beans in a HashMap.

Below is an example of the same business object using my HashMap method of storing the beans:

package app.service.impl;

public class MaintainStudent extends app.base.BaseBusinessObj
    implements app.service.intf.MaintainStudent
{ 
    //injection methods here
    public void setCourseDAO(app.dao.intf.CourseDAO courseDAO)
    {
        this.springBeans.add(courseDAO.getClass().getName(), courseDAO);
    }

    public void setStudentDAO(app.dao.intf.StudentDAO studentDAO)
    {
        this.springBeans.add(studentDAO.getClass().getName(), studentDAO);
    }

    public void setCourseAssignDAO(app.dao.intf.CourseAssignDAO courseAssignDAO)
    {
        this.springBeans.add(courseAssignDAO.getClass().getName(), courseAssignDAO);
    }
    //end injection methods

    //example business method
    @Override
    @Transactional(rollbackFor={Exception.class})
    public void assignCourse(StudentKey studenKey, CourseKey courseKey)
    {
        app.dao.intf.CourseAssignDAO courseAssignDAO =
            app.dao.fact.CourseAssignDAOFactory.requestBean(this);

        courseAssignDAO.assignCourse(studentKey, courseKey);
    }

    //rest of the class here
}

BaseBusinessObj class:

package app.base;    

public class BaseBusinessObj implements org.springframework.beans.factory.DisposableBean
{
    private Map<String, Object> springBeans = new HashMap<String, Object>();

    public void addBean(String className, Object bean)
    {
        this.springBeans.put(className, bean);
    }

    public Object getBean(String className)
    {
        return this.springBeans.get(className);
    }

    @Override
    public void destroy() throws Exception
    {
        this.springBeans.clear();
    }
}

Modified bean XML entries:

<bean id="baseBusinessObj" class="app.base.BaseBusinessObj" abstract="true">
</bean>

<bean id="baseDAO" class="app.dao.base.BaseDAO" abstract="true"
    parent="baseBusinessObj">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="courseDAO" class="app.dao.fact.CourseDAOFactory"
    parent="baseDAO">
</bean>

<bean id="studentDAO" class="app.dao.fact.StudentDAOFactory"
    parent="baseDAO">
</bean>

<bean id="courseAssignDAO" class="app.dao.fact.CourseAssignDAOFactory"
    parent="baseDAO">
</bean>

<bean id="maintainStudent" class="app.service.fact.MaintainStudentFactory"
    parent="baseBusinessObj">
    <property name="courseDAO" ref="courseDAO" />
    <property name="studentDAO" ref="courseAssignDAO" />
    <property name="courseAssignDAO" ref="studentDAO" />
</bean>

Sample converted factory class that now serves up an injected bean instead of creating a new instance:

package app.dao.fact;

public class CourseAssignDAOFactory extends app.dao.impl.CourseAssignDAO
{
    protected CourseAssignDAOFactory()
    {
        super();
    }

    //method formerly called "newInstance()"
    public static app.dao.intf.CourseAssignDAO requestBean(app.base.BaseBusinessObj requester)
    {
        app.dao.intf.CourseAssignDAO result;
        Object requestedBean = requester.getBean(CourseAssignDAOFactory.class.getName());
        result = (app.dao.intf.CourseAssignDAO)requestedBean;

        return result;
    }
}

What my architecture above does is provide the capability of writing code in a traditional non-DI style that allows the developer to explicitly "request" needed objects. This requesting is a similar activity to creating (instantiating) objects yourself. In my architecture, 'Requesting a Bean' has replaced 'Creating a New Instance' so that the Factory Class statements can be left in the code while making the code work with Dependency Injection architecture. I'm going to create a code generator that will automatically generate the required injector methods into every class that needs them.

I have tested the above architecture and it works. Transactions commit and rollback when expected.

I'm just wondering if this architecture I have created will still allow Spring to behave as designed.

With this architecture, will all of my beans still be able to be destroyed by Spring after a transaction is complete? Or will there be hanging objects in memory because of the HashMap storage idea?

Let me know what you think. Thanks for taking the time to read this.

2
  • What's the point in storing beans in map? If you don't like injected variables then you can get required bean from spring context. May 29, 2015 at 8:23
  • @AleksandrM Will calling the getBean method from ApplicationContext to get all of your beans break the transaction? Or will transactions still propagate, commit, and rollback as expected? May 29, 2015 at 13:10

1 Answer 1

0

You can replace the factories with Provider<Type> and have those injected instead:

public class CourseAssignDaoUser {

    @Autowired
    private Provider<CourseAssignDao> factory;

    void useDao() {
        CourseAssignDao dao = factory.get();
        // ...
    }
}

Or even inject the values directly:

public class CourseAssignDaoUser {

    @Autowired
    private CourseAssignDao dao;

    void useDao() {
        // ...
    }
}

If this is not possible for some reason you can use the Spring context instead of using the map:

public class CourseAssignDAOFactory {

    public static CourseAssignDAO requestBean(ApplicationContext context) {
        return context.getBean(CourseAssignDao.class);
    }
}

But using @Autowired with or without a Provider is definitely the best solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.