Announcement Announcement Module
Collapse
No announcement yet.
How to read properties file in spring xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to read properties file in spring xml

    this is my spring xml file.

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer" scope="singleton" lazy-init="false" >
    <property name="location" value="config.properties"/>

    </bean>

    <bean id="LdapInfo" class="com.ebay.trinity.tps.LdapInfo" depends-on="propertyConfigurer">
    <property name="hostName" value="${ldap.hostname}" />
    <property name="portNo" value="${ldap.portno}" />
    <property name="userName" value="${ldap.userName}" />
    <property name="password" value="${ldap.password}" />
    <property name="rootContext" value="${ldap.rootcontext}" />
    </bean>

    <bean id="LdapConnector" class="com.ebay.trinity.tps.LdapConnector">
    <property name="ldapInfo" ref="LdapInfo" />
    </bean>

    i am having the getter/setter methods for all the properties. when i read the LdapConnector bean i am getting this exception.
    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'LdapConnector' defined in file [C:\Trinity\workspace\AOPTest\Config\tps-config.xml]: Cannot resolve reference to bean 'LdapInfo' while setting bean property 'ldapInfo'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'LdapInfo' defined in file [C:\Trinity\workspace\AOPTest\Config\tps-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'portNo'; nested exception is java.lang.NumberFormatException: For input string: "${ldap.portno}"

    it is not at all reading the properties file. please help me. am i missing anything here???.

  • #2
    1. Make sure you have a property named 'ldap.portno' at the proeprties file;
    2. Check if any property of the 'LdapInfo' bean is injected;
    3. Make sure you create an ApplicationContext (not BeanFactory);
    4. Remove unnecessary scope="singleton" lazy-init="false" attributes from PPC bean;
    5. Use more convenient '<context: property-placeholder>' config element for PPC configuring;

    Comment


    • #3
      Are you using BeanFactory or ApplicationContext ?

      See: static.springframework.org/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory

      Comment


      • #4
        Accesing property from a property file

        I have also faced the same problem accessing the property from the properties file.

        Previously i have used the bean factory but when i used the ApplicationContext then i am able to acess the property.

        In applicationContext.xml , the configurations are like

        Code:
        <bean id="projectPropertyConfigurer"
        		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        		<property name="location">
        			<value>classpath: project.properties</value>
        		</property>
        	</bean>
        
        <bean id="name"
        		class="javautils.spring.examples.program3.Name">
        
        		<property name="nameCode">
        			<value>${praveen}</value>
        		</property>
        </bean>
        In project.properties file i have mapped praveen to 1.

        Now i am able to get the value by using
        Code:
         ApplicationContext context = new ClassPathXmlApplicationContext(
                        "applicationContext.xml");
        
         Name name = (Name) context.getBean("name");
          System.out.println(product.getNameCode());
        output: 1


        Regards,
        Praveen Oruganti
        Last edited by praveenoruganti; Nov 19th, 2008, 03:34 AM. Reason: Comments incorporated

        Comment


        • #5
          1. Use [ CODE][ /CODE] tags during code submitting;
          2. Remove useless 'depends-on' attribute because PPC is guaranteed to be processed before the regular beans;

          Comment


          • #6
            Originally posted by denis.zhdanov View Post
            1. Make sure you have a property named 'ldap.portno' at the proeprties file;
            2. Check if any property of the 'LdapInfo' bean is injected;
            3. Make sure you create an ApplicationContext (not BeanFactory);
            4. Remove unnecessary scope="singleton" lazy-init="false" attributes from PPC bean;
            5. Use more convenient '<context: property-placeholder>' config element for PPC configuring;
            Thanks for the help buddy.
            I appreciate it a lot.
            It is working at last....
            _______________
            floatingtank raid recovery

            Comment

            Working...
            X