Spring Boot Starters සිංහලෙන් | Spring Boot Dependency Management Explained | SC Guide

කොහොමද යාලුවනේ! Spring Boot Starters වල රහස අපි හොයමු!
අද කාලේ software development කරන කෙනෙක්ට Spring Boot කියන්නේ නැතුවම බැරි tool එකක් වෙලා නේද? විශේෂයෙන්ම Java වලින් backend services හදන අයට. පොඩි REST API එකක් හදන්න ගියත්, complex microservice එකක් build කරන්න ගියත්, Spring Boot අපේ වැඩ ගොඩක් ලේසි කරනවා. හැබැයි මේකේ magic එක මොකක්ද කියලා ඔයාලා කවදා හරි හිතලා තියෙනවද? අපි පොඩි dependency එකක් pom.xml එකට දැම්මම, ඒකට ඕන කරන හැමදේම auto-configure වෙන්නේ කොහොමද?
අන්න ඒ magic එක පිටිපස්සේ ඉන්න ප්රධානම රහස තමයි Spring Boot Starters. මේවා කියන්නේ Spring Boot වල තියෙන සුපිරිම feature එකක්. අද අපි මේ Starters මොනවද, ඒවා අපේ ජීවිතේ කොච්චර ලේසි කරනවද, සහ practical විදියට අලුත් Starter එකක් use කරන්නේ කොහොමද කියලා කතා කරමු.
අද ලිපිය ඉවර වෙනකොට, Spring Boot Starters ගැන ඔයාලට හොඳ අවබෝධයක් ලැබෙයි කියලා මට විශ්වාසයි. එහෙනම් වැඩේට බහිමු!
Spring Boot Starters කියන්නේ මොනවාද? (What are Spring Boot Starters?)
සරලවම කිව්වොත්, Spring Boot Starter කියන්නේ specific dependency එකක් එක්ක වැඩ කරන්න අවශ්ය කරන සියලුම dependencies එකට එකතු කරලා හදපු ready-to-use dependency set එකක්. හරියට අපි කෑමක් හදන්න යනකොට, ඒකට ඕන කරන හැම අමුද්රව්යයක්ම එක package එකක දාලා දෙනවා වගේ වැඩක්.
අපි හිතමු Spring Boot නැතිව සාමාන්ය Spring web application එකක් හදනවා කියලා. එතකොට අපිට Spring MVC, Tomcat server, JSON parsing library (Jackson වගේ), validation libraries, logging libraries වගේ ගොඩක් dependencies එකින් එක pom.xml (Maven නම්) එකට add කරන්න වෙනවා. ඒ විතරක් නෙවෙයි, ඒ හැම එකකම version compatibility බලන්න ඕනේ, configuration කරන්න ඕනේ. මේක හරිම headache එකක් නේද?
Spring Boot Starters මේ ප්රශ්නෙට හොඳම විසඳුම. උදාහරණයක් විදියට, අපිට web application එකක් හදන්න ඕනේ නම්, අපිට අවශ්ය වෙන්නේ spring-boot-starter-web
කියන එක විතරයි. මේ එක dependency එක add කරාම, ඒකට අදාළ සියලුම transitive dependencies (ඒ කියන්නේ ඒ dependency එකට ඕන කරන අනිත් dependencies) auto-මැටිකලි අපේ project එකට add වෙනවා. ඒ විතරක් නෙවෙයි, Spring Boot ඒවාට අවශ්ය default configurations ටිකත් කරලා දෙනවා. එතකොට අපිට පුළුවන් කෙලින්ම අපේ business logic එක ලියන්න පටන් ගන්න. ලොකු කාලයක් ඉතුරු වෙනවා.
මේවා නිසා ලැබෙන ලොකුම වාසි ටික තමයි:
- සරල බව (Simplicity): එක dependency එකකින් ගොඩක් දේවල් manage වෙනවා.
- අනුකූලතාව (Consistency): Dependencies වල version compatibility ගැන හිතන්න ඕනේ නැහැ, මොකද Spring Boot ඒ ටික manage කරනවා.
- වේගවත් සංවර්ධනය (Rapid Development): Boilerplate code සහ configuration වලින් මිදිලා අපේ වැඩේට focus කරන්න පුළුවන්.
`spring-boot-starter-web` එකේ බලය (The Power of `spring-boot-starter-web`)
Spring Boot වල තියෙන ජනප්රියම Starter එක තමයි spring-boot-starter-web
. මේක අපි Spring Boot වලින් RESTful APIs හෝ web applications හදනකොට අනිවාර්යයෙන්ම භාවිතා කරන එකක්. මේක add කරාම අපිට මොනවද ලැබෙන්නේ කියලා බලමු.
spring-boot-starter-web
එකට ඇතුළත් වන ප්රධානම Dependencies ටිකක් මෙන්න:
- Spring MVC: Web applications හදන්න අවශ්ය core framework එක.
- Tomcat: Embedded web server එකක්. මේක නිසා අපිට වෙනම server එකක් install කරන්න හෝ configure කරන්න අවශ්ය නැහැ. අපේ application එක run කරපු ගමන් server එකත් run වෙනවා.
- Jackson: JSON data convert කරන්න (serialize/deserialize) භාවිතා කරන library එක. APIs වලදී මේක අනිවාර්යයි.
- Validation: Data validation කරන්න අවශ්ය libraries.
- Spring-Core, Spring-Beans, Spring-Context, Spring-AOP: Spring Framework එකේ core modules.
දැක්කනේ? මේ හැමදේම එකට එකතු වෙලා තියෙන්නේ spring-boot-starter-web
කියන එක ඇතුළේ. අපිට කරන්න තියෙන්නේ මේක pom.xml (Maven) එකට හෝ build.gradle (Gradle) එකට add කරන එක විතරයි.
Maven Project එකක `pom.xml` එකට එකතු කරන්නේ මෙහෙමයි:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Gradle Project එකක `build.gradle` එකට එකතු කරන්නේ මෙහෙමයි:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
මේක add කරාම, Spring Boot auto-configure කරලා දෙනවා dispatcher servlet, embedded Tomcat server එක සහ තවත් ගොඩක් දේවල්. අපිට කෙලින්ම Controller එකක් ලියලා API එකක් හදන්න පුළුවන්.
අලුත් Starter එකක් එකතු කරමු: `spring-boot-starter-thymeleaf` (Let's Add a New Starter: `spring-boot-starter-thymeleaf`)
දැන් අපි theory ඇති, practical වෙමු! අපි දැන් අපේ Spring Boot web application එකකට Thymeleaf කියන template engine එක add කරමු. Thymeleaf කියන්නේ server-side HTML render කරන්න භාවිතා කරන ජනප්රියම template engine එකක්. මේකෙන් පුළුවන් අපේ backend එකෙන් එන data, HTML පිටුවක dynamic විදියට පෙන්නන්න.
පියවර 1: Spring Boot Project එකක් හදාගනිමු (Create a Spring Boot Project)
ඔයාලා ළඟ දැනටමත් Spring Boot project එකක් නැත්නම්, Spring Initializr එකට ගිහින් අලුත් project එකක් හදාගන්න. Dependencies විදියට Spring Web
විතරක් තෝරගන්න. Generate කරලා download කරගෙන IDE එකෙන් (IntelliJ IDEA, VS Code වගේ) open කරගන්න.
පියවර 2: `spring-boot-starter-thymeleaf` Dependency එක එකතු කරමු (Add `spring-boot-starter-thymeleaf` Dependency)
දැන් ඔයාලගේ project එකේ pom.xml
(Maven නම්) හෝ build.gradle
(Gradle නම්) file එක open කරලා, පහත dependency එක එකතු කරන්න.
Maven (`pom.xml`):
<dependencies>
<!-- Existing spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- New Thymeleaf Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Optional: For development tools like auto-restart -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- Existing test starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Gradle (`build.gradle`):
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependency එක add කරාට පස්සේ, Maven හෝ Gradle project එක refresh කරන්න අමතක කරන්න එපා. IDE එක ඒක auto-matically කරන්නත් පුළුවන්.
පියවර 3: Controller එකක් හදමු (Create a Controller)
දැන් අපි සරල Spring MVC Controller එකක් හදමු. මේ Controller එකෙන් request එකක් ආවම, අපේ Thymeleaf template එක render කරන්න කියනවා.
src/main/java/com/example/demo/HelloController.java
වගේ file එකක් හදලා මේ code එක paste කරන්න:
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "Spring Boot Lankan Developer");
return "hello"; // This refers to src/main/resources/templates/hello.html
}
@GetMapping("/")
public String index() {
return "redirect:/hello"; // Redirects root to /hello
}
}
මෙහිදී hello
කියලා return කරන්නේ, Spring Boot එකෙන් auto-matically src/main/resources/templates/hello.html
කියන template එක හොයාගන්න කියන එකයි. model.addAttribute("name", "Spring Boot Lankan Developer");
කියන එකෙන් අපි template එකට data එකක් pass කරනවා.
පියවර 4: Thymeleaf Template එකක් හදමු (Create a Thymeleaf Template)
දැන් අපි Thymeleaf template file එක හදමු. Spring Boot වල convention එක අනුව, template files තියෙන්න ඕනේ src/main/resources/templates/
කියන folder එක ඇතුලේ. ඒ නිසා, src/main/resources/templates/hello.html
කියලා file එකක් හදලා මේ code එක paste කරන්න:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to My Spring Boot App</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
text-align: center;
padding-top: 50px;
}
.container {
background-color: #fff;
margin: 0 auto;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
max-width: 600px;
}
h1 {
color: #007bff;
}
p {
font-size: 1.2em;
}
</style>
</head>
<body>
<div class="container">
<h1>Spring Boot Starters ගැන අපි ඉගෙන ගමු!</h1>
<p>ආයුබෝවන් <strong th:text="${name}">Guest</strong>!</p>
<p>මේක Spring Boot Starter එකක බලය! Thymeleaf Starter එක add කරපු නිසා, අපිට ලේසියෙන්ම මේ HTML page එකට data pass කරන්න පුළුවන් වුණා. </p>
<p>ඔයාලට මේ article එක ප්රයෝජනවත් වෙන්න ඇති කියලා හිතනවා.</p>
</div>
</body>
</html>
මේ HTML file එකේ <strong th:text="${name}">Guest</strong>
කියන කොටස බලන්න. th:text="${name}"
කියන්නේ Thymeleaf expression එකක්. මේකෙන් වෙන්නේ, අපේ Controller එකෙන් model එකට add කරපු name
attribute එකේ value එක මෙතනට replace කරන එකයි.
පියවර 5: Project එක Run කරලා Test කරමු (Run and Test the Project)
දැන් project එක Run කරන්න. ඔයාලට පුළුවන් IDE එකෙන් main application class එක Run කරන්න, නැත්නම් command line එකෙන්:
mvn spring-boot:run
# OR
gradle bootRun
Application එක start වුණාට පස්සේ, web browser එක open කරලා http://localhost:8080/hello
කියන URL එකට යන්න.
දැන් ඔයාලට මෙහෙම output එකක් පෙනෙයි:
<h1>Spring Boot Starters ගැන අපි ඉගෙන ගමු!</h1>
<p>ආයුබෝවන් <strong>Spring Boot Lankan Developer</strong>!</p>
<p>මේක Spring Boot Starter එකක බලය! Thymeleaf Starter එක add කරපු නිසා, අපිට ලේසියෙන්ම මේ HTML page එකට data pass කරන්න පුළුවන් වුණා. </p>
<p>ඔයාලට මේ article එක ප්රයෝජනවත් වෙන්න ඇති කියලා හිතනවා.</p>
දැක්කනේ? spring-boot-starter-thymeleaf
කියන එක add කරපු නිසා, Spring Boot auto-matically Thymeleaf configuration කරලා දීලා, අපිට ඉතාම ලේසියෙන් server-side rendering කරන්න අවස්ථාව ලැබුණා. වෙන මොකුත්ම configure කරන්න වුණේ නැහැ. මේක තමයි Spring Boot Starters වල නියම වටිනාකම!
වෙනත් Starter වර්ග සහ හොඳ පුරුදු (Other Starter Types and Best Practices)
spring-boot-starter-web
සහ spring-boot-starter-thymeleaf
වගේම, Spring Boot වල තව ගොඩක් Starters තියෙනවා. ඒවා අපිට විවිධ කාර්යයන් සඳහා උදව් වෙනවා.
පොදු Starters කීපයක්:
spring-boot-starter-data-jpa
: Databases (JPA/Hibernate) එක්ක වැඩ කරන්න.spring-boot-starter-security
: Spring Security use කරලා authentication and authorization කරන්න.spring-boot-starter-test
: Unit සහ integration tests ලියන්න. JUnit, Mockito, Spring Test වගේ දේවල් මේකේ තියෙනවා.spring-boot-starter-actuator
: Production ready features (monitoring, metrics, health checks) එකතු කරන්න.spring-boot-starter-validation
: Bean validation (e.g., @NotNull, @Size) කරන්න.
හොඳම පුරුදු (Best Practices):
- අවශ්ය දේ පමණක් භාවිතා කරන්න (Only include what you need): හැම Starter එකක්ම project එකට add කරන්න එපා. අපිට ඇත්තටම අවශ්ය කරන දේවල් විතරක් use කරන්න. අනවශ්ය dependencies නිසා project size එක වැඩි වෙන්න පුළුවන්.
- Dependency versions ගැන සැලකිලිමත් වෙන්න (Be mindful of dependency versions): සාමාන්යයෙන් Spring Boot Starters වලදී version handle කරන්නේ Parent POM (
spring-boot-starter-parent
) එකෙන් හෝ Gradle plugin එකෙන්. ඒ නිසා අපි සාමාන්යයෙන් versions mention කරන්නේ නැහැ. ඒත් සමහර rare cases වලදී, specific third-party library එකක version එකක් override කරන්න අවශ්ය වෙන්න පුළුවන්. - documentation එක බලන්න (Check the documentation): අලුත් Starter එකක් use කරනකොට, Spring Boot documentation එක බලලා ඒකෙන් මොනවද එන්නේ, මොනවද configurations කියලා තේරුම් ගන්න.
නිගමනය (Conclusion)
ඉතින් යාලුවනේ, ඔයාලට දැන් Spring Boot Starters ගැන හොඳ අවබෝධයක් ලැබුණා නේද? මේවා තමයි Spring Boot වල “convention over configuration” කියන concept එකේ පිටිපස්සේ ඉන්න ප්රධානම කෙනා. Starters නිසා අපිට dependency hell එකෙන් මිදිලා, ඉක්මනින්ම applications develop කරන්න පුළුවන් වෙනවා.
අපි අද spring-boot-starter-web
එක ගැන කතා කරලා, spring-boot-starter-thymeleaf
එකක් add කරලා, සරල web page එකක් render කරලා බැලුවා. මේ වගේම Spring Boot වල තියෙන අනිත් Starters ගැනත් හොයලා බලන්න. තව තව දේවල් ඉගෙන ගන්න උත්සාහ කරන්න. මේවා practice කරන තරමට තමයි ඔයාලට වැඩේ ලේසි වෙන්නේ.
මේ ලිපිය ගැන ඔයාලගේ අදහස්, ප්රශ්න පහළින් comment section එකේ දාන්න. ඔයාලට මේ වගේ තවත් මොනවා ගැනද දැනගන්න අවශ්ය කියලත් කියන්න. ඒ වගේම මේ ලිපිය ඔයාලගේ යාලුවන්ටත් share කරන්න අමතක කරන්න එපා!
ආයෙත් හම්බවෙමු!