How To Inject Value To Spring Bean Properties

If you want to set your Spring bean’s property value in a bean configuration file, you can use the below methods.

  1. Use property tag under bean tag.
    <bean id="beanPropsInjection" name="beanPropsInjection" class="com.dev2qa.example.spring.BeanPropertiesInjection">
          <property name="loginUserName" value="dev2qa.com"></property> 
          <property name="loginPassword" value="dev2qa.com"></property> 
    </bean>
  2. Use bean tag attribute prefixed with the p schema. But before do the below configuration, you should add namespace xmlns:p="http://www.springframework.org/schema/p" in the configuration file at first.
    <bean id="beanPropsInjection" name="beanPropsInjection" 
    class="com.dev2qa.example.spring.BeanPropertiesInjection" p:loginUserName="dev2qa.com" p:loginPassword="dev2qa.com" />
  3. Below will show you how to do this with Eclipse Spring IDE by examples.

Table of Contents

1. Use bean sub-tag property.

  1. Open Eclipse and create a Spring project. You can read Spring Hello World Example Use Maven And Eclipse to learn more.
  2. Create a java class com.dev2qa.example.spring.BeanPropertiesInjection with two properties loginUserName and loginPassword.
    package com.dev2qa.example.spring;
    
    public class BeanPropertiesInjection {
    	
    	private String loginUserName = "";
    	
    	private String loginPassword = "";
    
    	public String getLoginUserName() {
    		return loginUserName;
    	}
    
    	public void setLoginUserName(String loginUserName) {
    		this.loginUserName = loginUserName;
    	}
    
    	public String getLoginPassword() {
    		return loginPassword;
    	}
    
    	public void setLoginPassword(String loginPassword) {
    		this.loginPassword = loginPassword;
    	}
    }
    
  3. Add BeanPropertiesInjection in src/main/resources/BeanSettings.xml file.
  4. Double click the BeanSettings.xml file in the eclipse left panel, click the beans tab in the right bottom panel, click the “New Bean …” button.
  5. In the popup Bean Definition dialog, input bean Id ( beanPropsInjection ), Name ( beanPropsInjection ), and browse the Class ( com.dev2qa.example.spring.BeanPropertiesInjection ) we just created.
  6. Click Next, in the next wizard, add bean properties, all properties are auto prompt when you input part character of the property.
  7. Click the Finish button, you can see the Spring Beans —> Beans Overview wizard dialog.
  8. You can see the beanPropsInjection definition in the beans item.
    beans
      |
      |-beanPropsInjection
              |
              |-loginUserName
              |-loginPassword
  9. Click the bottom Source tab, you can see the configuration XML source code as below.
    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        default-init-method="globalInit" default-destroy-method="globalDestroy"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
        <bean id="beanPropsInjection" name="beanPropsInjection"
            class="com.dev2qa.example.spring.BeanPropertiesInjection">
            <property name="loginUserName" value="dev2qa"></property>
            <property name="loginPassword" value="dev2qa"></property>
        </bean>
    
    </beans>

2. Use bean attribute with namespace “p”.

  1. Below is the step to use namespace “p” to specify a bean property’s value.
  2. Click the BeanSettings.xml file bottom Namespaces tab and select the checkbox before p & beans to add the namespace.
  3. Click the bottom Source tab, you can see the namespace has been added to it.
    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        default-init-method="globalInit" default-destroy-method="globalDestroy"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
        <bean id="beanPropsInjection" name="beanPropsInjection"
            class="com.dev2qa.example.spring.BeanPropertiesInjection" 
            p:loginUserName="dev2qa.com" p:loginPassword="dev2qa.com"/>
        </bean>
    
    </beans>
  4. Now you can edit the bean configuration XML source manually to use namespace p to define the property and it’s value.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.