Spring Boot Annotations: ඔබේ Coding Skills දියුණු කරගන්න! | SC Guide

Spring Boot Annotations: ඔබේ Coding Skills දියුණු කරගන්න! | SC Guide

Spring Boot Annotations: වැඩේ ලේසි කරමු!

ආයුබෝවන් කොහොමද යාලුවනේ! අද අපි කතා කරන්න යන්නේ Java Spring Boot ෆ්‍රේම්වර්ක් එකේ ගොඩක් වැදගත්, හැබැයි මුලින් ඉගෙන ගන්නකොට පොඩ්ඩක් සංකීර්ණ වෙන්න පුළුවන් මාතෘකාවක් ගැන – ඒ තමයි Spring Boot Annotations. ඔබ Java වලින් web applications හදනවා නම්, Spring Boot අනිවාර්යයෙන්ම පාවිච්චි කරනවා ඇති. මේ annotations තමයි Spring Boot වල magic එක පිටිපස්සේ තියෙන්නේ. මේවා නැතුව Spring Boot project එකක් හදනවා කියන්නේ ඇස් බැඳගෙන පාරේ යනවා වගේ වැඩක්! ඉතින්, අපි බලමු මේ annotations මොනවද, ඒවා මොකටද පාවිච්චි කරන්නේ, සහ ඒවා අපේ coding ජීවිතේ කොච්චර ලේසි කරනවද කියලා.

අද අපි මේ annotations ටික ගැන විතරක් නෙවෙයි කතා කරන්නේ, ඒ ටික පාවිච්චි කරලා කොහොමද පොඩි project එකක් හදන්නේ කියලත් පොඩි hint එකක් දෙන්නම්. ඒ නිසා මේ article එක අන්තිම වෙනකල් කියවන්න. ඔයා අලුතින් Spring Boot ඉගෙන ගන්න කෙනෙක් වුණත්, නැත්නම් දැනටමත් වැඩ කරන කෙනෙක් වුණත්, මේක ඔයාට ගොඩක් වටින දෙයක් වෙයි!

1. @SpringBootApplication: හැමදේම පටන්ගන්න තැන!

Spring Boot project එකක් පටන් ගත්තාම, මුලින්ම පේන main class එකේ උඩම තියෙන annotation එක තමයි @SpringBootApplication. මේක අපේ Spring Boot application එක පටන් ගන්න ප්‍රධානම දොරටුව වගේ දෙයක්. හිතන්නකෝ මේක "all-in-one" annotation එකක් කියලා. මොකද මේක ඇතුලේ තව annotations කිහිපයක් එකට එකතු වෙලා තියෙනවා:

  • @SpringBootConfiguration: අපේ class එක Spring configuration class එකක් කියලා කියනවා. Java code වලින්ම configurations කරන්න පුළුවන්.
  • @EnableAutoConfiguration: මේක තමයි "magic" එක කරන්නේ! classpath එකේ තියෙන dependencies අනුව automatically configuration කරන්න Spring Boot එකට කියනවා. මේක නිසා අපිට ගොඩක් configuration code ලියන්න වෙනවා අඩුයි, වැඩේ ගොඩක් ලේසි වෙනවා.
  • @ComponentScan: Spring Boot එකට කියනවා ඔයාගේ application එකේ beans (Spring components) හොයාගන්න scan කරන්න ඕන packages මොනවද කියලා. සාමාන්‍යයෙන් මේක default විදියට @SpringBootApplication තියෙන package එක සහ ඒකේ sub-packages scan කරනවා.

මේ annotations තුන එකට තියෙන නිසා @SpringBootApplication කියන්නේ Spring Boot project එකක් පටන් ගන්නකොට අත්‍යවශ්‍යම දෙයක්. මේක නැතුව කිසිම Spring Boot application එකක් දුවන්න බෑ!


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // The all-in-one annotation
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

උඩ තියෙන code එකෙන් පෙන්නනවා @SpringBootApplication කොහොමද පාවිච්චි කරන්නේ කියලා. මේක තමයි අපේ application එකේ entry point එක.

2. Dependency Injection සහ @Autowired: යාලුවෙක්ට උදව් කරනවා වගේ!

@Autowired කියන්නේ Spring Framework එකේ තියෙන පට්ටම විශේෂ ලක්ෂණයක් වෙන Dependency Injection (DI) කියන concept එකේ කොටසක්. හිතන්නකෝ ඔයාට ඕන දේවල්, ඔයාට ඕන වෙලාවට Spring Framework එකෙන් සපයනවා. මේ ක්‍රියාවලියට තමයි Inversion of Control (IoC) කියලත් කියන්නේ.

@Autowired annotation එකෙන් කරන්නේ, Spring IoC container එකට කියනවා මේ variable එකට (dependency එකට) ඕන object එක auto-wire කරන්න, එහෙමත් නැත්නම් automatically inject කරන්න කියලා. ඒ කියන්නේ, ඔයාට ඒ dependency එකේ object එක අතින් create කරන්න ඕන නෑ. Spring Framework එක ඒක ඔයාට ලෑස්ති කරලා දෙනවා. මේකෙන් code එක ගොඩක් clean වෙනවා, test කරන්න ලේසි වෙනවා, coupled වීම අඩු වෙනවා. Object Oriented Programming (OOP) වල Dependency Inversion Principle (DIP) එකටත් මේක ගොඩක් උදව් වෙනවා.

@Autowired පාවිච්චි කරන්න පුළුවන් ප්‍රධාන ක්‍රමයක් තමයි Constructor Injection. මේක තමයි Spring Boot recommend කරන best practice එක. මේකෙන් dependencies immutable වෙනවා (වෙනස් කරන්න බෑ), test කරන්න ගොඩක් ලේසි වෙනවා. Constructor Injection වලදී, dependency එකක් නැතුව class එකක object එකක් හදන්න බෑ. ඒකෙන් අපිව force කරනවා dependencies නිවැරදිව manage කරන්න.


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyService {

    private final MyRepository myRepository; // Always good practice to use 'final' with constructor injection

    // Constructor Injection (Recommended best practice)
    @Autowired // @Autowired is optional here since Spring 4.3 for single constructor
    public MyService(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    public String getData() {
        return myRepository.findData();
    }
}

@Component
class MyRepository {
    public String findData() {
        return "Data from database!";
    }
}

මේ උදාහරණයෙන් පේනවා MyService class එකට MyRepository එක අවශ්‍ය වෙනකොට, අපි ඒක අතින්ම new MyRepository() කියලා හදන්නේ නැතුව, @Autowired එකෙන් Spring ට කියනවා "මචං, මේකට MyRepository එකක් ඕන, උඹම දීපන්" කියලා. පට්ට ලේසියිනේ?

3. @Component: පොදු Class එකක් හඳුන්වාදීම.

@Component කියන්නේ Spring Framework එකේ පොදුම component annotation එක. අපි Spring Boot application එකක් හදනකොට, Spring IoC container එකට කියන්න ඕන මේ class එක Spring bean එකක් විදියට manage කරන්න කියලා. @Component annotation එකෙන් කරන්නේ අන්න ඒ දේ. මේක දැම්මම Spring Boot වලට පුළුවන් @ComponentScan මගින් මේ class එක scan කරලා, ඒකේ object එකක් හදලා (instantiate කරලා), IoC container එකේ තියාගන්න. එතකොට අපිට ඕන වෙලාවක @Autowired පාවිච්චි කරලා ඒ object එක අරගන්න පුළුවන්.

සරලවම කියනවා නම්, @Component කියන්නේ Spring Boot එකට "මේක මම පාවිච්චි කරන class එකක්, මේක ගැන බලාගන්න!" කියලා කියන ක්‍රමය. මේක තමයි අනෙක් @Service, @Repository, @Controller වගේ annotations වල මූලිකම දෙය. ඒ හැම annotation එකක්ම @Component එකේ විශේෂිත අවස්ථා (specialized versions) විදියට ක්‍රියා කරනවා. ඔබට ඔබේ application එකේ layers වලට නිශ්චිතවම ගැලපෙන්නේ නැති, නමුත් Spring විසින් manage කළ යුතු ඕනෑම class එකක් සඳහා @Component භාවිතා කළ හැක.


import org.springframework.stereotype.Component;

@Component
public class MyUtility {

    public void doSomethingUseful() {
        System.out.println("Doing something useful...");
    }
}

මේකෙන් පේනවා MyUtility කියන class එක Spring bean එකක් විදියට register කරන්නේ කොහොමද කියලා. මේකෙන් පුළුවන් IoC container එක හරහා මේ object එක ඕන තැනකට inject කරන්න.

4. @Service සහ @Repository: Layer කරන එකේ වාසි!

අපි software development වලදී application එකක් හදනකොට, code එක එක එක කොටස් වලට (layers වලට) වෙන් කරන එක ගොඩක් වැදගත්. මේකෙන් code එක maintain කරන්න, test කරන්න, සහ team එකක් විදියට වැඩ කරන්න ගොඩක් ලේසි වෙනවා. Spring Framework එකේදී මේ layering එකට උදව් වෙන්න තමයි @Service සහ @Repository annotations තියෙන්නේ.

@Service: Business Logic එකේ හදවත.

@Service annotation එක පාවිච්චි කරන්නේ business logic එක තියෙන classes mark කරන්න. ඒ කියන්නේ, ඔබේ application එකේ core functionalities, calculations, validations වගේ දේවල් කරන තැන් තමයි මේ Services. උදාහරණයක් විදියට, customer කෙනෙක්ගේ order එකක් process කරනවා නම්, ඒකට අදාළ හැම logic එකක්ම OrderService එකක් ඇතුලේ තියෙන්න පුළුවන්. @Service කියන්නේ @Component එකේ විශේෂිත අවස්ථාවක් වුණත්, මේකෙන් අපිට තේරෙනවා මේ class එක business layer එකේ කොටසක් කියලා. මේකෙන් Spring වලටත් පුළුවන් ඒකට අදාළ විශේෂ ක්‍රියාකාරීත්වයන් (උදා: transaction management) automatically apply කරන්න.


import org.springframework.stereotype.Service;

@Service
public class ProductService {

    private final ProductRepository productRepository;

    public ProductService(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    public String getProductNameById(Long id) {
        // Business logic goes here
        return productRepository.findById(id);
    }
}

@Repository: Database එකට යන පාර!

@Repository annotation එක පාවිච්චි කරන්නේ data access layer එකේ classes mark කරන්න. ඒ කියන්නේ, database එකත් එක්ක කෙලින්ම කතා කරන (data retrieve කරන, save කරන, update කරන, delete කරන) classes තමයි මේ Repositories. උදාහරණයක් විදියට, ProductRepository එකක product details database එකෙන් හොයන්න, save කරන්න වගේ methods තියෙන්න පුළුවන්. @Repository කියන්නෙත් @Component එකේ විශේෂිත අවස්ථාවක්. මේකෙන් Spring වලට පුළුවන් database related exceptions translate කරන්න, ඒ කියන්නේ JDBC-specific exceptions වගේ දේවල් Spring's DataAccessException hierarchy එකට convert කරන්න. මේකෙන් code එක clean වෙනවා, error handling ලේසි වෙනවා.


import org.springframework.stereotype.Repository;

@Repository
public class ProductRepository {

    public String findById(Long id) {
        // Simulating database call
        if (id == 1L) {
            return "Laptop";
        }
        return "Unknown Product";
    }

    public void save(String productName) {
        System.out.println("Saving product: " + productName + " to database.");
    }
}

මේ annotations දෙක පාවිච්චි කරන එකෙන් අපේ application එකේ code එක layers වලට වෙන් කරලා, එක් එක් layer එකේ වගකීම මොකක්ද කියලා පැහැදිලිව පෙන්නනවා. ඒකෙන් code එකේ readability එක වැඩි වෙනවා වගේම, maintainability එකත් වැඩි වෙනවා.

5. මේවා ප්‍රයෝගිකව පාවිච්චි කරන්නේ කොහොමද? (ප්‍රායෝගික උදාහරණයක්)

අපි දැන් කතා කරපු හැම annotation එකක්ම එකට එකතු කරලා පොඩි application එකක් කොහොමද පටන් ගන්නේ කියලා බලමු. මේකෙන් අපිට Spring Boot වල flow එක තේරුම් ගන්න පුළුවන්.

මුලින්ම, අපේ application එකේ entry point එක:


// src/main/java/com/example/demo/DemoApplication.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);

        MyProductService productService = context.getBean(MyProductService.class);
        System.out.println(productService.getProductDetails(1L));
        productService.addNewProduct("New Smartwatch");
    }
}

ඊළඟට, අපේ data access layer එක (@Repository):


// src/main/java/com/example/demo/MyProductRepository.java
package com.example.demo;

import org.springframework.stereotype.Repository;

@Repository
public class MyProductRepository {

    public String findProductById(Long id) {
        if (id == 1L) {
            return "Product Name: Laptop, Price: $1200";
        } else if (id == 2L) {
            return "Product Name: Smartphone, Price: $800";
        }
        return "Product Not Found";
    }

    public void saveProduct(String name) {
        System.out.println("Saving product '" + name + "' to the database.");
    }
}

ඊළඟට, අපේ business logic layer එක (@Service):


// src/main/java/com/example/demo/MyProductService.java
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyProductService {

    private final MyProductRepository productRepository;

    @Autowired // Optional from Spring 4.3 if only one constructor
    public MyProductService(MyProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    public String getProductDetails(Long productId) {
        System.out.println("Fetching product details for ID: " + productId);
        return productRepository.findProductById(productId);
    }

    public void addNewProduct(String productName) {
        if (productName == null || productName.trim().isEmpty()) {
            System.out.println("Product name cannot be empty!");
            return;
        }
        System.out.println("Adding new product: " + productName);
        productRepository.saveProduct(productName);
    }
}

මේ උදාහරණයෙන්, DemoApplication එක පටන් ගන්නකොට @SpringBootApplication එකෙන් Spring IoC container එක configure වෙනවා. @ComponentScan එකෙන් අපේ MyProductRepository සහ MyProductService classes detect කරනවා. ඊට පස්සේ MyProductService එකේ constructor එකේ MyProductRepository එක @Autowired වෙනවා. ඒ කියන්නේ Spring විසින්ම MyProductRepository එකේ object එකක් හදලා MyProductService එකට inject කරනවා. අපිට ඒ object එක අතින් හදන්න ඕන නෑ.

මේක run කරලා බලන්න! ඔයාට පුළුවන් මේ files ටික හදලා Maven හෝ Gradle project එකක් විදියට Spring Initializr එකෙන් download කරලා බලන්න.

අවසාන වශයෙන් (Wrapping Up)

ඉතින් යාලුවනේ, ඔන්න ඔහොම තමයි Spring Boot annotations වැඩ කරන්නේ. @SpringBootApplication, @Autowired, @Component, @Service, @Repository කියන මේ annotations ගැන පැහැදිලි අවබෝධයක් ගන්න අද අපි කතා කළා. මේවා අපේ code එක organize කරන්න, maintenance ලේසි කරන්න, සහ Spring Boot වල auto-configuration powers උපරිමයෙන් පාවිච්චි කරන්න උදව් වෙනවා.

මතක තියාගන්න, annotations කියන්නේ Spring Boot වල backbone එක වගේ. ඒවා හරියට තේරුම් ගත්තොත්, ඔයාට පුළුවන් ගොඩක් efficient විදියට Spring Boot applications develop කරන්න. මේක ඔයාගේ Spring Boot coding skills වලට පට්ටම boost එකක් දෙයි.

දැන් ඔයාට හොඳ අවබෝධයක් තියෙන නිසා, පුළුවන් නම් මේ concepts ඔයාගේ project වලට apply කරලා බලන්න. පොඩි demo project එකක් හදලා මේ annotations පාවිච්චි කරලා බලන්න. මොකද code කරද්දී තමයි ඇත්තටම ඉගෙන ගන්න පුළුවන්!

ඔයාට මේ ලිපිය ගැන මොනවා හරි අදහස්, ප්‍රශ්න තියෙනවා නම්, නැත්නම් වෙනත් Spring Boot annotations ගැන දැනගන්න ඕන නම් පහලින් comment කරන්න. හැමෝටම සතුටින් code කරන්න ලැබේවා!