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.
- Start with a focused test that loads only the beans under change
- Assert that dependencies are wired and scopes behave as expected
- 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.