Announcement Announcement Module
Collapse
No announcement yet.
How to pass param to init-method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass param to init-method

    Is there any way to pass parameters to 'init-method' of a bean?

  • #2
    Originally posted by vinod.singh View Post
    Is there any way to pass parameters to 'init-method' of a bean?
    No way. You can consider using MethodInvokingFactoryBean for that.

    Comment


    • #3
      Thanks Denis. I believe MethodInvokingFactoryBean can't be used for a 'init-method', which does not return a value.

      Comment


      • #4
        Originally posted by vinod.singh View Post
        Thanks Denis. I believe MethodInvokingFactoryBean can't be used for a 'init-method', which does not return a value.
        Have you tried?

        If you follow the reference to the MethodInvokingFactoryBean contract you find the following:

        ...
        This factory bean is still useful though when you need to call a method which doesn't return any value
        ...

        Comment


        • #5
          This does not seems to work when bean scope is set to 'prototype'. See the following example-

          A test factory class with init method-
          Code:
          public class TestBean {
          
          	private String prop;
          	private String status;
          
          	public TestBean(String prop) {
          		this.prop = prop;
          	}
          
          	public static TestBean getInstance(String value) {
          		return new TestBean(value);
          	}
          
          	public void init(String status) {
          		this.status = status;
          		System.out.println("Initialized: " + status);
          	}
          
          	public String toString() {
          		return "TestBean[prop: " + prop + ", status: " + status + "]";
          	}
          }
          Spring configuration-
          Code:
          <bean id="blah" class="TestBean" factory-method="getInstance" scope="prototype">
              <constructor-arg type="java.lang.String" value="blah-blah" />
          </bean>
          
          <bean id="blah.init" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" scope="prototype">
              <property name="targetObject" ref="blah" />
              <property name="targetMethod" value="init" />
              <property name="arguments">
                  <list>
                      <value>really initialized</value>
                  </list>
              </property>
          </bean>
          Retrieve the initialized bean-
          Code:
          ApplicationContext.getBean("blah.init")
          Though it does call the 'init' method but returned bean is 'null'. Is there any way to get the initialized bean for this scenario?

          Comment


          • #6
            Originally posted by vinod.singh View Post
            This does not seems to work when bean scope is set to 'prototype'. See the following example-
            Moreover, it's not expected to work at the provided example. You define the bean with 'prototype' scope and directly reference it from another non-prototype bean. Dedicated instance of the 'prototyped' bean is created and processed by the MethodInvokingFactoryBean and no other TestBean instance is affected.

            I strongly suggest reading the following reference chapter - 3.4.3. Singleton beans with prototype-bean dependencies.


            Originally posted by vinod.singh View Post
            Is there any way to get the initialized bean for this scenario?
            TestClass.java
            Code:
            package com.spring.ioc;
            
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.stereotype.Component;
            
            @Component
            public class TestClass {
            
                @Autowired
                public void test(Aggregated aggregated) {
                    System.out.println("xxxxxx: TestClass.test()");
                }
            }
            Aggregated.java
            Code:
            package com.spring.ioc;
            
            import org.springframework.stereotype.Component;
            
            @Component
            public class Aggregated {
            }
            spring-config.xml
            HTML 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"
                   xmlns:aop="http://www.springframework.org/schema/aop"
                   xmlns:tx="http://www.springframework.org/schema/tx"
                   xmlns:context="http://www.springframework.org/schema/context"
                   xmlns:util="http://www.springframework.org/schema/util"
                   xsi:schemaLocation="
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
            
                <context:component-scan base-package="com.spring.ioc"/>
            
            </beans>
            SpringStart.java
            Code:
            package com.spring;
            
            import org.springframework.context.support.ClassPathXmlApplicationContext;
            
            public class SpringStart {
                public static void main(String[] args) throws Exception {
                    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
                }
            }
            Output:
            xxxxxx: TestClass.test()

            Comment


            • #7
              Thanks Denis, for all your help. Unfortunately I do not see a way to a call a 'init' method for my scenario. I am trying to initialize an instance of Mac as shown below-
              Code:
                  <bean id="pwd.digester.mac" class="javax.crypto.Mac" factory-method="getInstance" scope="prototype">
                      <constructor-arg type="java.lang.String" value="HmacSHA1" />
                  </bean>
              
                  <bean id="pwd.digester.mac.init" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                      <property name="targetObject" ref="pwd.digester.mac" />
                      <property name="targetMethod" value="init" />
                      <property name="arguments">
                          <list>
                              <ref local="pwd.digester.key" />
                          </list>
                      </property>
                  </bean>
              As Mac.init(Key key) method does not return anything so I can't get a reference to 'pwd.digester.mac.init'.

              Comment


              • #8
                Originally posted by vinod.singh View Post

                As Mac.init(Key key) method does not return anything so I can't get a reference to 'pwd.digester.mac.init'.
                If you want just get 'pwd.digester.mac.init()' to be called, remove 'id' attribute from the 'pwd.digester.mac.init' element.

                Comment

                Working...
                X