Inject In Sling Model

adobe/skills/plugins/aem/cloud-service/skills/code-assessment/inject-in-sling-model

作者 adobe940b8795c0dfApache-2.0197 個星標收錄於 2026年10月9日更新於 2026年10月8日儲存庫今天更新

AEM Cloud Service expert skill — migrate javax.inject.@Inject fields in Sling Model (@Model) classes to injector-specific annotations (@ValueMapValue / @OSGiService / @SlingObject). Self-discoverable (scan for @Model classes with field-level @Inject); the replacement is chosen deterministically from each field's declared type. Use for "fix @Inject in HeroModel.java", "modernize my Sling Models", or when scanning an AEM project for @Inject misuse. Detailed decision table, companion-annotation handling, import management, and file-level skip policy are in recipe.md.

AI 產生的概覽

將 AEM Sling Model 類別中的 javax.inject @Inject 欄位遷移為注入器專用註解。

功能
此技能為 AEM as a Cloud Service 專案提供偵測準則與遷移配方:找出帶有欄位層級 @Inject 註解的 @Model 類別,並將每個註解替換為注入器專用註解,例如 @ValueMapValue、@OSGiService 或 @SlingObject。替換註解依欄位宣告的 Java 型別透過決策表決定,同時處理 @Named、@Default、@Via、@Optional 等伴隨註解並更新匯入。它僅提供說明,產出為修改後的 Java 原始檔,不會進行提交。
適用情境
適用於在 AEM 專案中現代化改造 Sling Model 的情境,例如修正某個模型類別中的 @Inject 用法,或掃描專案中的 @Inject 誤用。它針對 @Model 類別中的欄位層級 @Inject;建構子參數與 setter 方法上的 @Inject 不在範圍內,會觸發檔案層級略過。
執行需求
需要一個包含 Sling Model 類別的 AEM Java 專案,以及所屬技能集合中引用的分析指令碼與執行手冊;此技能本身不附帶指令碼,未說明需要憑證或網路存取。

@Inject in Sling Models — AEM as a Cloud Service

This pattern is executed by the code-assessment runbook — follow ../references/runbook.md for the full flow (preflight → plan → apply → verify, run log). This skill supplies the detection + recipe the runbook applies.

Overview

javax.inject.@Inject on fields of @Model classes is discouraged by the Apache Sling Models documentation. Removing @Inject without a replacement breaks injection, so each field is migrated to an injector-specific annotation chosen from its declared type.

Classification — confirm this pattern applies

  • A *.java file whose class is annotated @Model, with at least one field annotated @Inject.
  • Constructor-parameter and setter-method @Inject are out of scope (trigger the file-level skip).

Discovery

Detection is performed by the analyzer (../scripts/analyze.sh), run by the runbook:

bash
bash ../scripts/analyze.sh <workspace-root> --pattern inject-in-sling-model

Match criteria (what the detector flags): a class annotated @Model (org.apache.sling.models.annotations.Model, confirmed import-aware) with at least one field annotated @Inject (javax.inject.Inject or jakarta.inject.Inject). One finding per matching field. Constructor/setter @Inject and non-@Model classes are not flagged. The replacement annotation is chosen from the field's declared type by the decision table in recipe.md.

Resolution contract

self-evident — the replacement annotation is fully determined by the field's declared Java type via the decision table in recipe.md. No external input is required.

Review checklist

  • Each migrated field maps to exactly one injector-specific annotation per the decision table
  • Sling objects (Resource / ResourceResolver / request / response) → @SlingObject, not @Self
  • @Named merged into the new annotation's name; @Default / @Via preserved
  • @Optional → per-field injectionStrategy = InjectionStrategy.OPTIONAL (never a class-level defaultInjectionStrategy change)
  • Unused imports removed; new imports added and ordered per the file's convention
  • File-level skip respected — no partial migration of a file with any ambiguous field

Recipe

Read recipe.md [blocked] in full before editing: input contract, decision table, companion annotations, import management, unlocatable / skip-file policy, per-file procedure, before/after example, editing strategy.

Handoff

The skill never commits. See ../references/git-workflow.md for git vs in-place handoff and the suggested commit message.

來源與署名

來源:adobe/skills位於plugins/aem/cloud-service/skills/code-assessment/inject-in-sling-model提交940b879

授權條款: Apache-2.0

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架