|
| 1 | +{ |
| 2 | + "id": 110, |
| 3 | + "slug": "spring-xml-config-vs-annotations", |
| 4 | + "title": "Spring XML Bean Config vs Annotation-Driven", |
| 5 | + "category": "enterprise", |
| 6 | + "difficulty": "intermediate", |
| 7 | + "jdkVersion": "17", |
| 8 | + "oldLabel": "Spring (XML)", |
| 9 | + "modernLabel": "Spring Boot 3+", |
| 10 | + "oldApproach": "XML Bean Definitions", |
| 11 | + "modernApproach": "Annotation-Driven Beans", |
| 12 | + "oldCode": "<!-- applicationContext.xml -->\n<beans xmlns=\"http://www.springframework.org/schema/beans\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://www.springframework.org/schema/beans\n http://www.springframework.org/schema/beans/spring-beans.xsd\">\n\n <bean id=\"userRepository\"\n class=\"com.example.UserRepository\">\n <property name=\"dataSource\" ref=\"dataSource\"/>\n </bean>\n\n <bean id=\"userService\"\n class=\"com.example.UserService\">\n <property name=\"repository\" ref=\"userRepository\"/>\n </bean>\n\n</beans>", |
| 13 | + "modernCode": "@SpringBootApplication\npublic class Application {\n public static void main(String[] args) {\n SpringApplication.run(Application.class, args);\n }\n}\n\n@Repository\npublic class UserRepository {\n private final JdbcTemplate jdbc;\n\n public UserRepository(JdbcTemplate jdbc) {\n this.jdbc = jdbc;\n }\n}\n\n@Service\npublic class UserService {\n private final UserRepository repository;\n\n public UserService(UserRepository repository) {\n this.repository = repository;\n }\n}", |
| 14 | + "summary": "Replace verbose Spring XML bean definitions with concise annotation-driven configuration in Spring Boot.", |
| 15 | + "explanation": "Traditional Spring applications wired beans through XML configuration files, declaring each class and its dependencies as verbose <bean> elements. While annotation support existed since Spring 2.5, XML remained the dominant approach until Spring Boot introduced auto-configuration. Spring Boot detects beans annotated with @Component, @Service, @Repository, and @Controller via classpath scanning, satisfies dependencies through constructor injection automatically, and configures infrastructure like DataSource from the classpath — eliminating all XML wiring files.", |
| 16 | + "whyModernWins": [ |
| 17 | + { |
| 18 | + "icon": "🚫", |
| 19 | + "title": "No XML", |
| 20 | + "desc": "@SpringBootApplication triggers component scanning and auto-configuration, eliminating all XML wiring files." |
| 21 | + }, |
| 22 | + { |
| 23 | + "icon": "💉", |
| 24 | + "title": "Constructor injection", |
| 25 | + "desc": "Spring injects dependencies through constructors automatically, making beans easier to test and reason about." |
| 26 | + }, |
| 27 | + { |
| 28 | + "icon": "⚡", |
| 29 | + "title": "Auto-configuration", |
| 30 | + "desc": "Spring Boot configures DataSource, JPA, and other infrastructure from the classpath with zero boilerplate." |
| 31 | + } |
| 32 | + ], |
| 33 | + "support": { |
| 34 | + "state": "available", |
| 35 | + "description": "Widely available since Spring Boot 1.0 (April 2014); Spring Boot 3 requires Java 17+" |
| 36 | + }, |
| 37 | + "prev": "enterprise/jdbc-resultset-vs-jpa-criteria", |
| 38 | + "next": null, |
| 39 | + "related": [ |
| 40 | + "enterprise/ejb-vs-cdi", |
| 41 | + "enterprise/jndi-lookup-vs-cdi-injection", |
| 42 | + "enterprise/manual-transaction-vs-declarative" |
| 43 | + ], |
| 44 | + "docs": [ |
| 45 | + { |
| 46 | + "title": "Spring Framework — Annotation-based Container Configuration", |
| 47 | + "href": "https://docs.spring.io/spring-framework/reference/core/beans/annotation-config.html" |
| 48 | + }, |
| 49 | + { |
| 50 | + "title": "Spring Boot — Auto-configuration", |
| 51 | + "href": "https://docs.spring.io/spring-boot/reference/using/auto-configuration.html" |
| 52 | + } |
| 53 | + ] |
| 54 | +} |
0 commit comments