Announcement Announcement Module
Collapse
No announcement yet.
Confused in instantiating bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Confused in instantiating bean

    Hi all,

    I am using spring 2.5 and using a client jar to call some services. Now from my code in xml configuration I need to instantiate this clint jar class because it has a reference in my class. right?
    But to instantiate it I need to also set it's properties i.e refernce to other beans.
    So my problem is if I continue with this I do not know when I will be stopped referncing these beans and instantiating.
    Am I missing something? Is there other way to achieve this?
    Please suggest.

  • #2
    @kousik.majumder,

    Originally posted by kousik.majumder View Post
    I need to instantiate this client jar class because it has a reference in my class. right?
    If you need to use components ( client services ), in most cases ( e.g. if they are not static utilities ) you need to define these components as beans, and inject them to whoever needs them via XML / Annotation / Autowire / etc..

    For example if you have a MoneyTransferService that lives in your "client jar", and that is used by your application, you would need to define this service as a bean, and possibly (depending on how you use it) inject this bean into another bean that depends on this service, so it is available at run time.

    If components are designed to be as independent as possible ( strong decoupling ), you will not have "chasing properties" problems.

    /Anatoly

    Comment


    • #3
      @litius,

      So what you are suggesting is that only instantiating the implementing class of 'MoneyTransferService' will suffice.
      No need to instantiate its properties in xml. right?

      In that case is this the jar who is responsible to instantiate its class's properties once someone tries to instantiate the class?

      Comment


      • #4
        @kousik.majumder,

        No, "MoneyTransferService" needs do be defined as a bean. All its properties / dependencies must be also defined.

        And if these external services are designed correctly, you would not have to chase dependencies.

        /Anatoly

        Comment


        • #5
          @litius

          Suppose "MoneyTransferService" has a property "Destination" and "Destination" itself has a peoperty "Address".
          So according to you I need to only define two beans . One for "MoneyTransferService" and one for its property "Destination". Right?

          But defining the bean "Destination" without its property(Address) will work? I am confused here.

          Please suggest.

          Comment


          • #6
            @kousik.majumder,

            If the "MoneyTransferService" is designed properly, if would probably not have a "destination" property, but instead, a "destination" would be one of the "MoneyTransferService.transferMoney()" method parameters. Which of course you don't need to specify configuratively since it is a method parameter provided at runtime by the caller.

            A concrete example of your services, and their properties will make it clearer on what it is you are trying to achieve.

            /Anatoly

            Comment


            • #7
              @ litius,

              My case is same as we discused before.
              I have a LocalAccountManager client jar which have methods createAccount(),deactivateAccount(), reactivateAccount() , searchAccount, viewAccount().
              In every methods original methods are called as below.
              Code:
              private AccountManagement accountManagement;
              public final void setAccountManagement (final AccountManagement  mngmt)
              {
                   this.accountManagement= mngmt;
              }
              public final CreateAccountRSType createAccount(CreateAccountRQType req)
              {
              return this.accountManagement.createAccount(req);
              }
              Here AccountManagement is an interface whose impl class is AccountManagementImpl which is in another jar file.

              Now this AccountManagementImpl class have 'BusinessRuleCheck' and 'AccountBasicServices' properties which itself have many properties as I told you in chain mails.

              So now how do i configure my beans in config xml file? How many bean references do I need to refer in this .xml?

              One more thing I need to know is that , what is the scope of the referenced bean in in a config file?
              Suppose I have below code:
              Code:
              <bean id="mybean"
              		class="com.test.Impl">
              		<property name="dao"><ref bean="acc.dao" />
                       </bean>
              What is the scope of this ref bean 'acc.dao' ? Will application context look for this bean in whole classpath or in those xml file referenced in application context?

              Because I was thinking to put the ref bean name same as they have put in their jar code. If I do that and as because their jars are in classpath will that bean can find those xml entry in jars to be referred properly?
              Last edited by kousik.majumder; Oct 11th, 2010, 05:03 AM.

              Comment


              • #8
                @kousik.majumder,
                Code:
                <bean id="accountManagementSvc" class="org.package.service.AccountManagementImpl">
                   <property name="businessRuleCheckSvc" ref="businessRuleCheckSvc">
                   <property name="accountBasicSvc" ref="accountBasicSvc">
                </bean>
                
                <bean id="accountBasicSvc" class="org.package.service.AccountBasicServices">
                   <property name="propertyOne" value="1">
                   <property name="propertyTwo" value="2">
                </bean>
                
                <bean id="businessRuleCheckSvc" class="org.package.service.BusinessRuleCheck"/>
                1. default bean scope in Spring is singleton
                2. application context will have visibility to all beans defined in application context
                3. if you import spring configuration files from your jars, all beans defined there will be available to you

                To answer your additional application context questions, take a look at Introduction to the Spring IoC container and beans.

                /Anatoly

                Comment

                Working...
                X