Results 1 to 2 of 2
  1. #1
    fanjavaid is offline Member
    Join Date
    Dec 2012
    Location
    Jakarta, Indonesia
    Posts
    23
    Rep Power
    0

    Question How to set dynamically bean property value ?

    Hello,
    I am new in Spring Framework, how i can set bean property value dynamically?
    Because in my references that i read it just give example with static value like :
    Java Code:
    <bean id="mahasiswa" class="fandi.spring.Mahasiswa">
           <property name="name" value="Fandi"> <!-- It static value -->
    </bean>
    How i can set dynamically value. In my mind , i must set like this :
    Java Code:
    ....
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    Mahasiswa mahasiswa = (Mahasiswa) context.getBean("mahasiswa");
    
    mahasiswa.setName("Fandi");
    ....
    Is that best practices? Or anything?

  2. #2
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: How to set dynamically bean property value ?

    Hi,

    Spring encourages dynamically setting values as like you mentioned. But if you know property values while before you instantiate the object, spring can do it for you if you use declarative aproach.

    If you know properties before use

    <bean id="mahasiswa" class="fandi.spring.Mahasiswa">
    <property name="name" value="Fandi"> <!-- It static value -->
    </bean>

    If you want to dynamically bind then

    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    Mahasiswa mahasiswa = (Mahasiswa) context.getBean("mahasiswa");

    mahasiswa.setName("Fandi");

    Or else if you want to read values from property file at run time use the below

    <bean
    class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>WEB-INF/classes/config/properties/database.properties</value>
    </list>
    </property>
    <property name="ignoreResourceNotFound" value="true"/>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    </bean>

    Thanks,

Similar Threads

  1. how to obtain manage bean property from other jsp page
    By nikhil_me in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 06-11-2012, 03:41 AM
  2. How to set the property of Bean by constructor injection
    By Java Tip in forum Spring Framework
    Replies: 1
    Last Post: 05-05-2008, 09:01 AM
  3. Struts Form Bean with property
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-27-2007, 10:10 AM
  4. property of a java bean
    By suloverboy in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 09:31 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:18 PM

Tags for this Thread

Posting Permissions

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