The configuration approach in the Spring ecosystem has shifted significant over the years.
Early Spring (1.x Era)
In the initial release, dependency injection and bean definitions were managed exclusively through verbose XML files. As applications grew, this led to massive configuration files and tangled dependency graphs, making maintenance difficult.
Annotation-Driven Development (2.x Era)
With the introduction of JDK 1.5, Spring 2.x embraced annotations. Developers could declare and wire beans using @Component, @Service, and @Autowired, significantly reducing XML boilerplate. The standard practice during this period was:
- Use XML for infrastructure setup (e.g., data sources, external properties).
- Use annotations for business logic components.
Modern Spring (3.x, 4.x, and Boot)
The newer versions of the framework strongly favor Java-based configuration and auto-configuration. The framework detects dependencies on the classpath and configures beans automatically, minimizing manual intervention.
The Rise of Spring Boot
Dynamic languages like Ruby, Node.js, and Groovy highlighted the friction in Java development: heavy configuration, complex deployment cycles, and integration hurdles. Spring Boot emerged to address these issues.
It operates on the principle of "Convention over Configuration." Instead of forcing you to define every bean, it provides sensible defaults. You can launch a standalone application with a embedded Servlet container (like Tomcat) simply by running a JAR file.
Key Advantages:
- Rapid project scaffolding.
- Zero-configuration integration with common libraries.
- Standalone execution without requiring an external application server.
- Production-ready features like health checks and metrics.
- Streamlined deployment pipelines and cloud-native compatibility.
Creating a Basic Web Service
1. Project Inheritance
Define the parent in your pom.xml to inherit default configurations and dependancy management.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
2. Web Dependency
Include the starter for web functionalities to enable REST endpoints and embedded web servers.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
3. Build Plugin
Add the Maven plugin to package the application as an executable JAR.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4. Application Entry Point
Write the main class to bootstrap the application.
@SpringBootApplication
@Configuration
@Controller
public class DemoApp {
@RequestMapping("/greeting")
@ResponseBody
public String getGreeting() {
return "hello world!";
}
public static void main(String[] args) {
SpringApplication.run(DemoApp.class, args);
}
}
Annotation Breakdown:
@SpringBootApplication: Enables auto-configuration and component scanning.@Configuration: Marks the class as a source of bean definitions.@Controller: Identifies this class as a web controller in the MVC pattern.main: The entry point that launches the Spring context.
Handling Default Redirects
To forward requests to a specific view or page without a controller logic, you can configure view controllers:
@Controller
public class HomeRedirect {
@RequestMapping("/")
public String redirectRoot() {
return "forward:/home.jsp";
}
}
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
}
}