Results 1 to 4 of 4

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

  1. #1
    Join Date
    Oct 2008
    Posts
    4

    Default 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. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

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

  3. #3
    Join Date
    Oct 2008
    Posts
    4

    Default

    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.

  4. #4
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •