Mastering Spring Property Injection: From XML to SpEL

This article provides a comprehensive guide to Spring's property injection techniques, covering setter and constructor injection using both XML and Java annotations, external configuration with @PropertySource, and advanced SpEL expressions, complete with runnable code examples and output screenshots.

Programmer DD
Programmer DD
Programmer DD
Mastering Spring Property Injection: From XML to SpEL

In this tutorial we explore Spring Framework's property injection methods, including setter injection, constructor injection, annotation‑based injection, and SpEL expressions, showing both XML and Java‑based configurations with complete code samples.

1. Setter Property Injection

1.1 Using XML for setter injection

<bean id="userSetter" class="com.example.demo.bean.User">
    <property name="username" value="example-username-setter"/>
    <property name="age" value="25"/>
</bean>

1.2 Using @Bean annotation for setter injection

@Bean
public User user() {
    User user = new User();
    user.setUsername("example-username-anno-setter");
    user.setAge(25);
    return user;
}

1.3 Full code example

public class User {
    private String username;
    private Integer age;
    // getters and setters omitted for brevity
    @Override
    public String toString() {
        return "User{username='" + username + "', age=" + age + "}";
    }
}

2. Constructor Injection

2.1 Using XML for constructor injection

<bean id="userConstructor" class="com.example.demo.bean.User">
    <constructor-arg index="0" value="example-username-constructor"/>
    <constructor-arg index="1" value="25"/>
</bean>

2.2 Using @Bean annotation for constructor injection

@Bean
public User userConstructor() {
    return new User("example-username-anno-constructor", 25);
}

2.3 Full code example

public class User {
    private String username;
    private Integer age;
    public User() {}
    public User(String username, Integer age) {
        this.username = username;
        this.age = age;
    }
    // getters and setters omitted for brevity
    @Override
    public String toString() {
        return "User{username='" + username + "', age=" + age + "}";
    }
}

3. Annotation‑Based Property Injection

3.1 @Value annotation

@Component
public class White {
    @Value("white-value-annotation")
    private String title;
    @Value("1")
    private Integer rank;
    @Override
    public String toString() {
        return "White{title='" + title + "', rank=" + rank + "}";
    }
}

3.2 External configuration with @PropertySource

@Configuration
@ComponentScan("com.example")
@PropertySource("classpath:blue.properties")
public class InjectValueConfiguration {}

// blue.properties
blue.title=blue-value-properties
blue.rank=2
@Component
public class Blue {
    @Value("${blue.title}")
    private String title;
    @Value("${blue.rank}")
    private Integer rank;
    @Override
    public String toString() {
        return "Blue{title='" + title + "', rank=" + rank + "}";
    }
}

3.3 XML external properties

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd">
    <context:property-placeholder location="classpath:blue.properties"/>
    <bean class="com.example.demo.bean.Blue">
        <property name="title" value="${blue.title}"/>
        <property name="rank" value="${blue.rank}"/>
    </bean>
</beans>

4. SpEL Expressions

4.1 Using @Value with SpEL

@Component
public class Azure {
    @Value("#{'spel-for-azure'}")
    private String name;
    @Value("#{10}")
    private Integer priority;
    // getters, setters and toString omitted for brevity
}
@Component
public class Emerald {
    @Value("#{'copy of ' + azure.name}")
    private String name;
    @Value("#{azure.priority + 1}")
    private Integer priority;
    // getters, setters and toString omitted for brevity
}
@Component
public class Ivory {
    @Value("#{azure.name.substring(0, 3)}")
    private String name;
    @Value("#{T(java.lang.Integer).MAX_VALUE}")
    private Integer priority;
    // getters, setters and toString omitted for brevity
}

4.2 SpEL in XML

<bean id="azure" class="com.example.demo.bean.Azure">
    <property name="name" value="#{'spel-for-azure'}"/>
    <property name="priority" value="#{10}"/>
</bean>
<bean id="emerald" class="com.example.demo.bean.Emerald">
    <property name="name" value="#{'copy of ' + azure.name}"/>
    <property name="priority" value="#{azure.priority + 1}"/>
</bean>
<bean id="ivory" class="com.example.demo.bean.Ivory">
    <property name="name" value="#{azure.name.substring(0, 3)}"/>
    <property name="priority" value="#{T(java.lang.Integer).MAX_VALUE}"/>
</bean>

All examples can be run with a simple ApplicationContext (or Spring Boot) to obtain the beans and print their toString() output, demonstrating successful property injection in each scenario.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaspringSpELannotationsXMLdependency-injectionProperty Injection
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.