Announcement Announcement Module
No announcement yet.
Using factory beans with factory method parameters Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using factory beans with factory method parameters

    Hello guys.

    I have a factory that is responsible to create dynamic proxies for my DAOs. You send in the domain object Class you want to create the DAO proxy for and using reflection and naming conventions it returns the proxy for you.

    The factory would be injected with the correct InvocationHandler by Spring, and I would create the dao beans using this factory. Problem is, the factory method needs a parameter (the domain object class). Here's what I got so far:

    <bean id="daoFactory" class="a.b.c.DaoProxyFactory">
        <property handler="hibernateHandler">
    <bean id="userDao" 
          factory-method="createProxy"/> <!-- ==== Here I actually want to call createProxy("a.b.c.User") or createProxy(User.class)
    The problem is my factory can create more than 1 type of object (but all of them extends from the same interface). I don't want to set-up a factory for each kind of DAO I need or to create a method for each kind of DAO in the factory. That's why I'm using reflection.

    Could you point me to the right direction?

    I also want to avoid having to wire my factory into my commands and having then asking for the DAOs through the factory as I will be coupling both solutions together. My commands should have the correct DAO injected. If it's a proxy created by a factory or if it's a concrete implementation defined in spring doesn't matter.
    Last edited by daniel.mfreitas; Jun 26th, 2008, 11:00 AM.

  • #2
    I saw a guy doing this in a blog (haven't tested it so don't know if it's correct)

    public class DaoFactoryCreator {
    	public static DaoFactory createDaoFactory(List daos) {
    		DaoFactory factory = DaoFactory.getInstance();
    		return factory;
    <bean id="daoFactory" class="com.javalobby.tnt.spring.DaoFactoryCreator" factory-method="createDaoFactory" />
    	 			<ref bean="dao1"/>
    	 			<ref bean="dao2"/>
    				<ref bean="dao3"/>
    Basically he's using the <constructor-arg> to send parameters to the factory method. If this is valid, I should be able to do the same right?

    <bean id="userDao" 


    • #3
      Yes I can do this. Actually the <constructor-arg> tag means "arguments needed to create an object" and not "java constructor arguments". If the beans are create by constructor or by factory methods it doesn't matter. My final solution:

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="" xmlns:xsi=""
          <bean id="daoProxyFactoryInvocationHandler"
              class="com.googlecode.armycreator.persistence.HibernateInvocationHandler" />
          <bean id="daoFactory" class="com.googlecode.armycreator.persistence.DaoProxyFactory">
              <property name="invocationHandler" ref="daoProxyFactoryInvocationHandler" />
          <bean id="gameSystemDao" factory-bean="daoFactory" factory-method="getProxy">
              <constructor-arg type="java.lang.Class" value="com.googlecode.armycreator.model.GameSystem" />
          <bean id="findGameSystemByUuidCommand" class="com.googlecode.armycreator.command.FindGameSystemByUuidCommand"
              <property name="gameSystemDao" ref="gameSystemDao" />