Automotive/EclipseRCP

Eclipse RCP (Rich Client Platform) - 9.의존성 주입과 Eclipse

TimeSave 2022. 1. 21. 21:24

 

9. Dependency injection and Eclipse

: 의존성 주입과 Eclipse

 

9.1. Define class dependencies in Eclipse

: Eclipse 에서 class dependencies 정의

 

Eclipse의 programming model 은 Java Specification Request 330 (JSR330)에 따라 constructor, method, field injection을 지원합니다.

Eclipse는 또한, dependency injection을 목적으로, 추가적인 annotation을 지원합니다. 가장 중요한 annotation은 Eclipse의 class dependencies를 정의하기 위한 Annotaion에서 다루고, 다른 더 특별한 annotation은 해당 장에서 다룹니다.

 

Eclipse dependency framework는 key와 inject된 object의 type이 올바른지 확실하게 합니다. 예를들어, 다음 field 선언에서와 같이 "xyz" key에 대해 Todo type의 object를 갖도록 지정하면 framework는 assign 가능한 type이 있는 object를 찾은 경우에만 object를 ​​inject합니다.

@Inject @Named("xyz") Todo todo;
 

9.2. Annotations to define class dependencies in Eclipse

: Eclipse의 class dependency 정의를 위한 annotation

 

아래의 table은 dependency inject에 연관된 annotation 대한 overview를 제공합니다. 이 annotation은 JSR330과 Eclipse 특정 주석에 기반합니다.

 

Table 8. Basic annotations for dependency injection

: dependency injection을 위한 기본 annotation

 
Annotation
Description
@javax.inject.Inject
Defined by JSR330, can be added to a field, a constructor or a method. The Eclipse framework tries to inject the corresponding objects into the fields or the parameters of the instance.
@javax.inject.Named
Defined by JSR330, defines the key for the value which should be injected. By default, the fully qualified class name is used as the key. Several keys for default values are defined as constants in the IServiceConstants interface.
@Optional
Eclipse specific annotation, marks an injected value to be optional. If no valid object can be determined for the given key (and type), the framework does not throw an exception.
The specific behavior depends on where the @Optional is placed. The following description is based on the key. If the key cannot be resolved the following happens:
* for parameters: a null value will be injected; * for methods: the method calls will be skipped * for fields: the values will not be injected.
Note that null is an acceptable value to be set in the context, and it is different from a key being removed from the context. For example, if the following is called context.set(SOMEKEY, null), anybody listening for SOMEKEY will be injected with null.
@GroupUpdates
Eclipse specific annotation, indicates that updates for this @Inject should be batched. If you change such objects in the Eclipse context, the update is triggered by the processWaiting() method of the IEclipseContext object. This annotation is intended to be used by the platform for performance optimization and should rarely be necessary in RCP applications.

 

정보 : Ecliplse platform은 특별한 목적의 추가적인 주석을 지원합니다. 예를 들어, 이벤트 수용(event service에서 보내짐) 혹은 preference설정 관련.

 

9.3. On which objects does Eclipse perform dependency injection?

: 어떤 object에서 Eclipse가 dependency injection을 수행합니까?

 

Eclipse runtime은 application model에서 참조되는 Java class들을 위한 object를 생성합니다. 이 instantiation 동안 Eclipse runtime은 annotation에 대한 class definition을 scan 합니다. 이 annotation을 근거로 하여, Eclipse framework는 injection을 수행합니다.

 

Eclipse는, new 연산자를 사용하여 코드에서 생성된 객체에 자동적으로 object에 dependency injection을 하지 않습니다.

 

cf) 인스턴스화와 초기화는 다릅니다.

(https://pythonq.com/so/java/325371)

인스턴스화 : object에 메모리 할당

초기화 : 할당된 메모리에 값을 할당.

 

9.4. Dynamic dependency injection based on key / value changes

: key/value 변경에 근거한 동적 Dependency injection

 

Eclipse framwork는 object가 어떤 key와 type에 dependency를 표시했는지 추적합니다. 만약, key가 가리키는 value가 변경되었다면, Eclipse framework는 새 value를 object에 re-inject 합니다. 이 object는 상응하는 type에 대한 dependency가 표현된 것입니다.

 

이 뜻은, application은 listener를 install(그리고 remove)하지 않아도 된다는 것입니다.

 

예를 들어, @inject를 통해 당신이 현재 선택 항목을 inject하게 정의 할 수 있습니다. 만약, 선택을 변경하면, Eclipse framework는 새 value를 inject 할 것입니다.

 

@Inject로 표시된 method와 field에서만 re-injection은 동작합니다.

이것은 @PostConstruct로 표시된 constructor와 method에 inject된 parameter들에는 동작하지 않습니다. 이 method는 한번만 실행되기 때문입니다.

 

정보 : 이것은 Eclipse가 key가 가리키는 value의 field를 추적하지 않는 다는 뜻이 아닙니다. 예를들어, mykey1 이라는 key가 Todo object를 value로 가리키고 있고, 이 key는 새로운 object를 가리킬 떄, 이것은 연관된 class dependency가 있는 모든 object에 re-injection을 일으킵니다. 그러나, 만약 Todo object 내부의 field가 변경되면, 이것은 re-injection을 일으키지 않습니다.

 

9.5. OSGi services and Eclipse dependency injection

: OSGi service와 Eclipse dependency injection

 

OSGI service는 Eclipse application에서 dependency injection이 가능합니다. 만약, 당신이 custom OSGi service를 정의한다면, 당신의 model object를 service에 inject할 수 있습니다. 이것은 당신의 application의 data 접근을 위한 singleton or factory implementation을 제거할 수 있습니다.

 

If a requested key is not found in the Eclipse context hierarchy, the Eclipse framework dynamically queries for a fitting OSGi service in the OSGi registry.

만약, 요청된 key를 Eclipse context hierachy에서 찾을 수 없다면, Eclipse framework는 적합한 OSGi registry 내부의 OSGi service를 동적으로 요청합니다(query)

 

사진 삭제

사진 설명을 입력하세요.

예를들어, 만약 TaskService interface에 대해 선언된, OSGi service를 가지고 있다면, 아래 code snippet을 통해 Eclipse part의 field에 inject 할 수 있습니다.

@Inject TaskService service;