Mastering Spring XML Configuration: 9 Essential Settings Explained

This article provides a comprehensive guide to Spring XML configuration, covering namespace declaration, bean definition, property injection, scope settings, dependency wiring, AOP setup, transaction management, file imports, and component scanning with clear code examples and explanations.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mastering Spring XML Configuration: 9 Essential Settings Explained

1. Declare XML namespaces and schema

In Spring XML files you must declare the XML namespace and XML Schema to use Spring tags and attributes.

<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">
    <!-- configuration content -->
</beans>
xmlns

declares the XML namespace. xsi declares the XML Schema instance namespace. xsi:schemaLocation points to the schema file.

2. Declare a bean

Use the <bean> element to define a bean’s id, class and other attributes.

<bean id="helloWorld" class="com.mikechen.HelloWorld">
    <property name="message" value="Hello World!" />
</bean>

Attributes: id: unique identifier in the container. class: fully‑qualified class name.

3. Set bean properties

Use <property> to inject values or references.

<bean id="userDao" class="com.example.UserDao">
    <property name="dataSource" ref="dataSource" />
</bean>

4. Define bean scope

The scope attribute controls the lifecycle, e.g., singleton (default) or prototype.

<bean id="userService" class="com.example.UserService" scope="prototype">
    <!-- configuration content -->
</bean>

5. Declare dependencies

Constructor injection uses <constructor-arg>, while property injection uses <property>.

<bean id="userRepository" class="com.example.UserRepository">
    <constructor-arg value="jdbc:mysql://localhost:3306/mydb" />
    <constructor-arg value="root" />
    <constructor-arg value="password" />
</bean>

6. Configure AOP

Use <aop:config> with <aop:aspect>, <aop:pointcut> and advice elements.

<aop:config>
    <aop:aspect ref="loggingAspect">
        <aop:pointcut id="userServicePointcut"
                      expression="execution(* com.example.UserService.*(..))" />
        <aop:before pointcut-ref="userServicePointcut" method="beforeAdvice" />
    </aop:aspect>
</aop:config>

7. Configure transactions

Use <tx:advice> to apply transaction attributes to methods.

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

8. Import other XML files

The <import> element allows you to include additional configuration files.

9. Component scanning

<context:component-scan>

automatically detects annotated classes and registers them as beans.

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.

transactionaopspringXMLBeans
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

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.