How Spring XML Configuration Works by Example |Video upload date:  · Duration: PT10M23S  · Language: EN

Compact guide to Spring XML configuration schemas beans XML files and how Spring Boot loads XML based config for Java apps

Quick intro to Spring XML and why it still hangs around

If you thought XML was dead you are in good company and also wrong. Many legacy Java apps use Spring XML to wire beans and validate configuration with schemas. Spring Boot prefers Java configuration these days, but XML is still supported when you inherit code written by someone who liked indentation a lot.

Read XML schemas and namespaces the boring but useful way

Namespaces tell the Spring parser which rules to apply. Common namespaces include the core beans namespace, the context namespace and aop when you get fancy. The schema locations in the XML file let Spring validate configuration against the right XSD so typos blow up at startup instead of in production.

<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">
  
    <!-- bean declarations go here -->
  </beans>

Declare beans and properties without reinventing new

Define a bean with an id and a class. You can inject dependencies via constructor args or property tags. Yes it is verbose. Yes it works. Use descriptive ids so future you does not cry during maintenance.

<bean id="userService" class="com.example.UserService">
    <property name="userRepository" ref="userRepository" />
  </bean>

  <bean id="userRepository" class="com.example.UserRepository" />

Constructor injection or setter injection

Constructor injection is great for immutability and for making your code testable. Setter injection is fine when you need optional wiring. Both are supported with <constructor-arg> and <property> tags.

Choose the right scope for your beans

Default scope is singleton. If you need fresh instances every time use prototype. For web apps you can also pick request or session scope. Pick the smallest scope that still keeps your sanity.

  • singleton for shared services
  • prototype for stateful helpers
  • request or session for web scoped objects

Load XML into an ApplicationContext

In classic Spring you can use ClassPathXmlApplicationContext to bootstrap from XML files. It eagerly loads and wires beans at startup which is great for catching config errors early.

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
MyService svc = ctx.getBean(MyService.class);

Keep XML working with Spring Boot

Spring Boot leans toward Java config, but you do not have to abandon XML. The usual pragmatic approach is to import XML into your Java configuration using @ImportResource. That lets modern code live next to legacy wiring without a dramatic rewrite.

@Configuration
@ImportResource("classpath:legacy-beans.xml")
public class AppConfig { }

Alternative bootstrapping tips

You can also register an XML based ApplicationContext initializer if you are into advanced bootstrapping. For most apps @ImportResource is the easier and cleaner route.

Testing and migration strategies that do not involve ritual sacrifice

Write unit tests that load a minimal XML context and assert bean wiring. Enable schema validation during tests to catch typos before they reach your CI and your users.

  1. Start with a focused test that loads only the beans under change
  2. Assert that dependencies are wired and scopes behave as expected
  3. Replace small pieces with @Configuration classes and component scanning

Practical tips and gotchas

  • Prefer small focused XML files per module to limit merge conflicts
  • Enable schema validation during CI runs to catch silly typos fast
  • Use clear bean ids or use annotation based names when you migrate for readability

Wrap up

Spring XML is old school but reliable. It teaches you explicit wiring and schema driven validation which can be a lifesaver in legacy systems. When you are ready migrate to Java configuration for better compile time safety and less verbosity. Until then keep your XML tidy and your tests merciless.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.