אונה של שירות Spring

הערה @Service ב- Spring היא התמחות של הערה @Component. הערת השירות של Spring יכולה להיות מיושמת רק על מחלקות. היא משמשת לסימון המחלקה כספק שירות.

הערת @Service של Spring

הערת @Service של Spring משמשת עם מחלקות שמספקות פונקציות עסקיות מסוימות. ההקשר של Spring יזהה אוטומטית את המחלקות הללו כאשר משמשים בהגדרת רשתות הערות וסריקת מסלולי המחלקות.

דוגמת השירות @Service של Spring

בואו ניצור אפליקציית Spring פשוטה בה ניצור מחלקת שירות Spring. ניצור פרוייקט Maven פשוט ב- Eclipse ונוסיף תלות בסיסית של Spring המרכזית הבאה.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.0.6.RELEASE</version>
</dependency>

מבנה הפרויקט הסופי שלנו יראה כמו התמונה למטה. בואו ניצור מחלקת שירות.

package com.journaldev.spring;

import org.springframework.stereotype.Service;

@Service("ms")
public class MathService {

	public int add(int x, int y) {
		return x + y;
	}
	
	public int subtract(int x, int y) {
		return x - y;
	}
}

שים לב שזו פשוט כיתה של Java שמספקת פונקציות לחיבור ולחיסור של שני מספרים. כך שאנו יכולים לקרוא לה ספק שירות. הוספנו לה הערה @Service כדי שהקשר של Spring יכול לזהות אותה באופן אוטומטי ונוכל לקבל את מופעה מההקשר. בוא ניצור כיתת Main בה ניצור את הקשר של Spring המופעל על ידי הערות, ונקבל את מופע הכיתה שלנו.

package com.journaldev.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringMainClass {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.scan("com.journaldev.spring");
		context.refresh();

		MathService ms = context.getBean(MathService.class);

		int add = ms.add(1, 2);
		System.out.println("Addition of 1 and 2 = " + add);

		int subtract = ms.subtract(2, 1);
		System.out.println("Subtraction of 2 and 1 = " + subtract);
		
		// סגור את ההקשר של Spring 
		context.close();
	}

}

פשוט הרץ את הכיתה כיישום Java, זה יצור פלט הבא.

Jun 05, 2018 3:02:05 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Tue Jun 05 15:02:05 IST 2018]; root of context hierarchy
Addition of 1 and 2 = 3
Subtraction of 2 and 1 = 1
Jun 05, 2018 3:02:05 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Tue Jun 05 15:02:05 IST 2018]; root of context hierarchy

אם שמת לב לכיתת MathService שלנו, הגדרנו את שם השירות כ"ms". אפשר לקבל את המופע של MathService באמצעות שם זה גם. הפלט יישאר זהה במקרה זה. אך נצטרך להשתמש בהמרה מפורשת.

MathService ms = (MathService) context.getBean("ms");

זהו הכול לדוגמה מהירה של הערה @Service של Spring.

אתה יכול להוריד את קוד פרויקט הדוגמה מה-מאגר GitHub שלנו.

התייחסות: מסמך API

Source:
https://www.digitalocean.com/community/tutorials/spring-service-annotation