Direct Value Injection
Bean properties and constructor arguments can be configured with literal values or references to other managed beans. The value attribute of the <property/> element accepts human-readable string representations, which Spring's conversion service transforms into the appropriate types.
Basic Value Assignment
<bean id="databaseConnection" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/appdb"/>
<property name="username" value="admin"/>
<property name="password" value="securepass123"/>
</bean>
P-Namespace Example
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="databaseConnection" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/appdb"
p:username="admin"
p:password="securepass123"/>
</beans>
Properties Configuraton
<bean id="configProperties"
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="properties">
<value>
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/appdb
</value>
</property>
</bean>
Bean Reference Validation
The <idref/> element provides valdiation that the referenced bean exists during deploymant:
<bean id="serviceBean" class="..."/>
<bean id="clientBean" class="...">
<property name="serviceIdentifier">
<idref bean="serviceBean"/>
</property>
</bean>
Cross-Bean References
The <ref/> element establishes dependencies between beans:
<ref bean="targetBean"/>
Parent Container References
<!-- Parent context -->
<bean id="paymentService" class="com.example.PaymentService"/>
<!-- Child context -->
<bean id="paymentService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="paymentService"/>
</property>
</bean>
Inner Bean Definitions
<bean id="containerBean" class="...">
<property name="nestedComponent">
<bean class="com.example.Component">
<property name="identifier" value="internal123"/>
<property name="version" value="2.0"/>
</bean>
</property>
</bean>
Collection Configuration
<bean id="collectionHolder" class="example.CollectionContainer">
<property name="emailMap">
<map>
<entry key="admin" value="admin@example.com"/>
<entry key="support" value="help@example.com"/>
</map>
</property>
<property name="itemList">
<list>
<value>first item</value>
<ref bean="serviceBean"/>
</list>
</property>
</bean>
Collection Merging
<bean id="baseConfig" abstract="true" class="example.Configuration">
<property name="settings">
<map>
<entry key="timeout" value="30"/>
<entry key="retries" value="3"/>
</map>
</property>
</bean>
<bean id="extendedConfig" parent="baseConfig">
<property name="settings">
<map merge="true">
<entry key="retries" value="5"/>
<entry key="cacheSize" value="100"/>
</map>
</property>
</bean>
Strongly Typed Collections
public class DataProcessor {
private Map<String, Double> thresholds;
public void setThresholds(Map<String, Double> thresholds) {
this.thresholds = thresholds;
}
}
<bean id="processor" class="DataProcessor">
<property name="thresholds">
<map>
<entry key="low" value="0.5"/>
<entry key="high" value="95.0"/>
</map>
</property>
</bean>
Null Value Handling
<bean class="ExampleBean">
<property name="optionalField">
<null/>
</property>
</bean>
C-Namespace for Constructor Injection
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mainService" class="com.example.MainService"
c:repository-ref="dataRepository"
c:timeout="5000"/>
</beans>
Nested Property Paths
<bean id="configBean" class="ConfigurationHolder">
<property name="nested.config.value" value="finalSetting"/>
</bean>