Announcement Announcement Module
Collapse
No announcement yet.
how to pass a parameter to a constructor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to pass a parameter to a constructor

    Hey guys
    I have to pass the variable "i" to the constructor of AuthenticationService ("as") at the following piece of code:

    int i = x;

    String[] configFiles = new String[] {"beans.xml"};
    BeanFactory beanFactory = new ClassPathXmlApplicationContext(configFiles);

    AuthenticationService as = (AuthenticationService) beanFactory.getBean ("authenticationService");


    How do I do that??

    Thanks a lot

  • #2
    Hi Moacir,

    Please refer to the Spring reference documentation here.

    Comment


    • #3
      how to pass a parameter to a constructor

      thanks phillip and sorry about my insistence
      but my problem is a little different.

      I have to pass a java variable to the constructor of the AuthenticationService but from another class. I don't wanna resolve a dependency, just pass a java variable to the costructor like it is without IoC.

      if you or anybody can help me...
      Thanks for the help

      Comment


      • #4
        Hi,

        I tend to think that there is no simple/direct way to do that.

        One (more or less silly) idea that comes to my mind is to return a factory instead of the service itself and to have the factory implement a create method that receives "i" as an argument and returns the a new instance of AuthenticationService that got "i" as parameter to its constructor.

        Hope it helps

        Erik

        Comment

        Working...
        X