تعليق تكوين الربيع

التعليق: تعتبر تعليق @Configuration جزءًا من إطار عمل Spring الأساسي. يشير تعليق Spring Configuration إلى أن الفئة تحتوي على طرق تعريف @Bean. بذلك يمكن لحاوية Spring معالجة الفئة وإنشاء Spring Beans التي يمكن استخدامها في التطبيق.

Spring @Configuration

تسمح تعليق @Configuration في Spring لنا باستخدام التعليقات لحقن التبعيات dependency injection. دعونا نفهم كيفية إنشاء فئات تكوين Spring. لنقم بإنشاء فئة بسيطة لجافا.

package com.journaldev.spring;

public class MyBean {

	public MyBean() {
		System.out.println("MyBean instance created");
	}
	
}

قبل أن نستخدم أي من فئات إطار Spring، سيتعين علينا إضافة تبعياتها إلى مشروع ميفن.

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

الآن دعونا نقوم بإنشاء فئة تكوين Spring.

package com.journaldev.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfiguration {

    @Bean
    public MyBean myBean() {
		return new MyBean();
	}
	
}

لنكتب فئة بسيطة ونكون قد قمنا بتكوين فئتنا البسيطة لتكوين Spring.

package com.journaldev.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MySpringApp {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(MyConfiguration.class);
		ctx.refresh();

		// MyBean mb1 = ctx.getBean(MyBean.class);

		// MyBean mb2 = ctx.getBean(MyBean.class);

		ctx.close();
	}

}

إذا قمت بتشغيل التطبيق أعلاه، سيتم إنتاج إخراج مثل هذا:

May 23, 2018 12:34:54 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Wed May 23 12:34:54 IST 2018]; root of context hierarchy
MyBean instance created
May 23, 2018 12:34:54 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@ff5b51f: startup date [Wed May 23 12:34:54 IST 2018]; root of context hierarchy

توجيه: لاحظ أن الربيع يحمل الفول ويضعه في سياقه قبل أن نطلبه حتى. يتم ذلك للتأكد من تكوين جميع الفول بشكل صحيح وفشل التطبيق بسرعة إذا حدث خطأ ما. أيضًا يجب أن يتم استدعاء ctx.refresh()، وإلا فسوف نحصل على الخطأ التالي عند محاولة الحصول على أي فول من السياق.

Exception in thread "main" java.lang.IllegalStateException: org.springframework.context.annotation.AnnotationConfigApplicationContext@f0f2775 has not been refreshed yet
	at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1076)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1106)
	at com.journaldev.spring.MySpringApp.main(MySpringApp.java:11)

إذا قمت بفك تعليق البيانات حيث أقوم بالحصول على مثيلات MyBean، ستلاحظ أنه لا يتم استدعاء بناء MyBean. هذا لأن النطاق الافتراضي لـ فولات الربيع هو Singleton. يمكننا تغييره باستخدام تعليق @Scope.

ماذا لو قمنا بإزالة @Configuration التعليق؟

ماذا سيحدث إذا قمنا بإزالة التعليق @Configuration من فئة MyConfiguration. ستلاحظ أنه لا يزال يعمل كما هو متوقع ويتم تسجيل واسترجاع فولات الربيع كفئات فردية. ولكن في هذه الحالة، إذا قمنا بالاتصال بطريقة myBean() فسيكون ذلك استدعاءً لطريقة جافا عادية وسنحصل على مثيل جديد من MyBean ولن يظل فرديًا. لإثبات هذه النقطة، دعنا نحدد فولًا آخر سيستخدم مثيل MyBean.

package com.journaldev.spring;

public class MyBeanConsumer {

	public MyBeanConsumer(MyBean myBean) {
		System.out.println("MyBeanConsumer created");
		System.out.println("myBean hashcode = "+myBean.hashCode());
	}

}

فئة تكوين Spring المُحدّثة لدينا هي:

package com.journaldev.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class MyConfiguration {

	@Bean
    public MyBean myBean() {
		return new MyBean();
	}
	
	@Bean
    public MyBeanConsumer myBeanConsumer() {
		return new MyBeanConsumer(myBean());
	}
}

الآن عند تشغيل فئة MySpringApp، يتم إنشاء الناتج التالي.

MyBean instance created
MyBean instance created
MyBeanConsumer created
myBean hashcode = 1647766367

لذلك MyBean لم يعد فريدًا، الآن دعونا نعلّق فئة MyConfiguration مرة أخرى بالتعليق @Configuration ونشغّل فئة MySpringApp. في هذا الوقت، سيكون الناتج كما يلي.

MyBean instance created
MyBeanConsumer created
myBean hashcode = 1095088856

لذلك من الأفضل استخدام التعليق @Configuration مع فئات التكوين للتأكد من سلوك حاوي الربيع يعمل كما نريد. إذا كنت لا ترغب في استخدام @Configuration التعليق لأسباب غريبة، ما زلنا يمكننا إنشاء فئة التكوين الخاصة بنا عن طريق عدم استدعاء طريقة myBean() وبدلاً من ذلك استخدام متغير فئة MyBean المُكوَّن من خلال التعليق @Autowired. شيء مثل الكود أدناه سيعمل أيضًا.

package com.journaldev.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class MyConfiguration {

	@Autowired
	MyBean myBean;
	
	@Bean
    public MyBean myBean() {
		return new MyBean();
	}
	
	@Bean
    public MyBeanConsumer myBeanConsumer() {
		return new MyBeanConsumer(myBean);
	}
}

هذا كل شيء بالنسبة لتعليق تكوين الربيع، سوف نتطلع إلى تفحص تعليقات الربيع الأخرى في المشاركات المستقبلية.

يمكنك تنزيل كود المثال من مستودعنا في GitHub.

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