Announcement Announcement Module
Collapse
No announcement yet.
How to invoke method dynamically on an instance managed by Spring Container. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to invoke method dynamically on an instance managed by Spring Container.

    If we know the bean id or the classname and the method name at runtime,Is there anyway we can invoke methods on the instances maintained by spring container with no thread issues.
    Please provide a sample example with the solution .
    Can it be done using ReflectiveMethodInvocation ?How ?

  • #2
    You can use standard Reflection API to call necessary method on the particular object.

    Comment


    • #3
      Thanks for the reply ....
      Is there any mechanism in spring where in which we can just specify the bean id/class name and get the bean manged by the container(apart from appContext.getBean("beanid"));.So that on fly i can get the bean, managed by spring container and invoke a method specific to that bean.

      Comment


      • #4
        I'm afraid I don't understand the problem. Spring doesn't bring any magic to the application. You want to get a reference to particular object (via its id) and call specific method. Spring doesn't offer any special support for that because it may be done via couple of lines using Reflection API code. Something like:
        Code:
        Object bean = context.getBean("xxx");
        Method method = MyClass.class.getMethod("methodName");
        method.invoke(bean);
        It's already very simple and I don't see the necessity of any additional support from the Spring for that.

        Comment

        Working...
        X