Announcement Announcement Module
Collapse
No announcement yet.
Using factory beans with factory method parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    <bean id="daoFactory" class="a.b.c.DaoProxyFactory">
        <property handler="hibernateHandler">
    </bean>
    
    <bean id="userDao" 
          factory-bean="daoFactory"
          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?

    [EDITED]
    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, 12:00 PM.

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

    Code:
    public class DaoFactoryCreator {
    	public static DaoFactory createDaoFactory(List daos) {
    		DaoFactory factory = DaoFactory.getInstance();
    		factory.initialize(daos);
    		return factory;
    	}
    }
    
    <bean id="daoFactory" class="com.javalobby.tnt.spring.DaoFactoryCreator" factory-method="createDaoFactory" />
    	<constructor-arg>
    	 	<value>
    	 		<list>
    	 			<ref bean="dao1"/>
    	 			<ref bean="dao2"/>
    				<ref bean="dao3"/>
    			</list>
    		</value>
    	</constructor-arg>
    </bean>
    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?

    Code:
    <bean id="userDao" 
          factory-bean="daoFactory"
          factory-method="createProxy">
        <constructor-arg><value>a.b.c.User</value></constructor-arg>
    </bean>

    Comment


    • #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:

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
      
          <bean id="daoProxyFactoryInvocationHandler"
              class="com.googlecode.armycreator.persistence.HibernateInvocationHandler" />
      
          <bean id="daoFactory" class="com.googlecode.armycreator.persistence.DaoProxyFactory">
              <property name="invocationHandler" ref="daoProxyFactoryInvocationHandler" />
          </bean>
      
          <bean id="gameSystemDao" factory-bean="daoFactory" factory-method="getProxy">
              <constructor-arg type="java.lang.Class" value="com.googlecode.armycreator.model.GameSystem" />
          </bean>
      
          <bean id="findGameSystemByUuidCommand" class="com.googlecode.armycreator.command.FindGameSystemByUuidCommand"
              scope="prototype">
              <property name="gameSystemDao" ref="gameSystemDao" />
          </bean>
      </beans>

      Comment

      Working...
      X