Master Spring Configuration: XML vs Java, Bean Lifecycle, and Advanced Profiles

This article explains Spring configuration files, comparing XML and Java (annotation‑based) approaches, details bean creation, scopes, initialization and destruction, and demonstrates advanced techniques such as conditional beans and profile‑specific configurations with clear code examples.

Java Captain
Java Captain
Java Captain
Master Spring Configuration: XML vs Java, Bean Lifecycle, and Advanced Profiles

What is a Spring configuration file?

Spring configuration files can be XML or Java (annotation‑based) and define how the Spring container creates and manages beans.

XML configuration

XML was the original way; beans and dependencies are declared in one or more XML files.

<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Define a Bean -->
    <bean id="myBean" class="com.example.MyBean">
        <property name="property1" value="value1"/>
    </bean>

</beans>

In this example we define a bean myBean of class com.example.MyBean with property property1 set to value1.

Java configuration

Java configuration uses @Configuration classes and @Bean methods, which is more concise.

@Configuration
public class AppConfig {

    @Bean
    public MyBean myBean() {
        MyBean myBean = new MyBean();
        myBean.setProperty1("value1");
        return myBean;
    }
}

The @Configuration annotation marks the class as a configuration class and @Bean defines a bean.

Understanding Bean lifecycle

Both XML and Java configurations share the same bean lifecycle, which includes creation, property setting, initialization, and destruction.

Bean creation

When the Spring container starts, it instantiates all beans, sets properties, and calls initialization methods.

Bean scopes

Spring provides several scopes; the most common are singleton (one instance per container) and prototype (a new instance for each request). singleton: default scope, single instance per container. prototype: a new instance each time it is requested.

<bean id="singletonBean" class="com.example.SingletonBean" scope="singleton"/>
<bean id="prototypeBean" class="com.example.PrototypeBean" scope="prototype"/>
@Bean
@Scope("singleton")
public SingletonBean singletonBean() {
    return new SingletonBean();
}

@Bean
@Scope("prototype")
public PrototypeBean prototypeBean() {
    return new PrototypeBean();
}

Bean initialization and destruction

Spring can invoke custom init and destroy methods via the init-method and destroy-method attributes.

<bean id="myBean" class="com.example.MyBean" init-method="init" destroy-method="destroy"/>
@Bean(initMethod = "init", destroyMethod = "destroy")
public MyBean myBean() {
    return new MyBean();
}

Advanced configuration: Conditional beans and profiles

Different environments can load different beans using @Conditional and @Profile annotations.

Conditional bean

@Conditional allows bean creation only when a condition evaluates to true.

@Configuration
public class AppConfig {
    @Bean
    @Conditional(MyCondition.class)
    public MyBean myBean() {
        return new MyBean();
    }
}

Profile

@Profile loads beans based on the active profile (e.g., dev or prod).

@Configuration
@Profile("dev")
public class DevConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean("dev");
    }
}

@Configuration
@Profile("prod")
public class ProdConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean("prod");
    }
}

Conclusion

Spring configuration files are core to the framework. Whether using XML or Java configuration, understanding bean lifecycle, scopes, init/destroy methods, and advanced features like conditional beans and profiles enables flexible and efficient Spring applications.

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.

JavaconfigurationspringXMLbeanProfileConditional
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.