If you want to set your Spring bean’s property value in a bean configuration file, you can use the below methods.
- 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>
- 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" />
- Below will show you how to do this with Eclipse Spring IDE by examples.
1. Use bean sub-tag property.
- Open Eclipse and create a Spring project. You can read Spring Hello World Example Use Maven And Eclipse to learn more.
- Create a java class com.dev2qa.example.spring.BeanPropertiesInjection with two properties
loginUserName
andloginPassword
.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; } }
- Add BeanPropertiesInjection in src/main/resources/BeanSettings.xml file.
- 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.
- In the popup Bean Definition dialog, input bean Id ( beanPropsInjection ), Name ( beanPropsInjection ), and browse the Class ( com.dev2qa.example.spring.BeanPropertiesInjection ) we just created.
- Click Next, in the next wizard, add bean properties, all properties are auto prompt when you input part character of the property.
- Click the Finish button, you can see the Spring Beans —> Beans Overview wizard dialog.
- You can see the beanPropsInjection definition in the beans item.
beans | |-beanPropsInjection | |-loginUserName |-loginPassword
- 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”.
- Below is the step to use namespace “p” to specify a bean property’s value.
- Click the BeanSettings.xml file bottom Namespaces tab and select the checkbox before p & beans to add the namespace.
- 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>
- Now you can edit the bean configuration XML source manually to use namespace p to define the property and it’s value.