Announcement Announcement Module
Collapse
No announcement yet.
Reflection of ProxyBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reflection of ProxyBean

    Hi,

    I have a TransactionProxyFactoryBean which wraps my service.

    <bean id="txProxyTemplate"
    abstract="true"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="duplicate*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="myserviceManager" parent="txProxyTemplate">
    <property name="target">
    <bean class="some.service.class">

    </bean>
    </property>
    </bean>



    now I got a message which contains bean name, service method name and method argument. i need use reflection to invoke the method on the service.

    ------------------------------------------------------------------------
    Class clazz = getServiceClass();

    Object bean = applicationContext.getBean(beanName);

    Method method = clazz.getDeclaredMethod(methodName...)//[successfully found]

    method.invoke(bean, argument) //[Problem here]
    ------------------------------------------------------------------------
    the error says that bean is not an instance of declaring class

    any suggestions?

    thanx
    Last edited by kaisor; Jul 21st, 2006, 09:20 AM.

  • #2
    no one knows why or didn't i put it clearly enough?

    Comment


    • #3
      The bean is not of the class required because it is a proxy. A much cleaner mechanism is to call the method directly.

      Is it really necessary to use reflection to call the method you require?

      Is it really necessary to use the bean factory directly? Could you inject your service into a helper class that knows what its interface is?

      Comment


      • #4
        thanx for the answer, i have the following scenario.

        i have serveral servers deployed on different machines. Messages should be sent among them to keep application consistent. These messages are most db relative.

        I use spring AOP to send messages, that is, after some methods are called, messages should be sent to other servers.

        so the message is defined as

        String beanName,
        String methodName,
        Object data
        ....

        my initial idea is to receive this kind of message on other side. so the server can call the method just like usual java reflection.

        or do u have other suggestions?
        thanx

        Comment


        • #5
          What does getServiceClass() provide?
          If you separate your service class into interface and implementation you could retrieve the Method from the interface (instead of the class). Then you have to specify the interface also for your proxy factory bean.
          This should allow to invoke the method on the implementing class and the proxy as well.

          Regards,
          Andreas

          Comment


          • #6
            good idea, i do seperate my services into interfaces and impls.
            i will give a try on this.

            how can i specify interface in this factory bean?
            http://www.springframework.org/docs/...ctoryBean.html

            this transactionproxyfactory bean only has a target attribute where i can set my service.

            <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
            abstract="true">
            <property name="transactionManager" ref="transactionManager"/>
            <property name="transactionAttributes">
            <props>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
            </property>
            </bean>

            <bean id="myProxy" parent="baseTransactionProxy">
            <property name="target" ref="myTarget"/>
            </bean>

            <bean id="yourProxy" parent="baseTransactionProxy">
            <property name="target" ref="yourTarget"/>
            </bean>
            Last edited by kaisor; Jul 25th, 2006, 03:53 AM.

            Comment


            • #7
              Just use the "proxyInterfaces" property.

              Code:
                <property name="proxyInterfaces" value="my.pkg.MyInterface"/>
              Note that the property could actually handle multiple interfaces.

              Regards,
              Andreas

              Comment


              • #8
                thanx a lot! Andreas!

                Comment

                Working...
                X