2

我正在尝试使用自定义 Sling 模型为 AEM 核心组件创建代理组件,该模型将部分功能委托回核心组件的 Sling 模型。

我正在遵循https://github.com/adobe/aem-core-wcm-components/wiki/Delegation-Pattern-for-Sling-Models中的示例,但它会导致java.lang.IllegalArgumentException: Can not set com.example.core.models.Title field.

设置

  • AEM 版本 6.4.4
  • core.wcm.components.content 版本 2.4.0

来源

/apps/myproject/components/pageHeadline/.content.xml

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="cq:Component"
    jcr:title="Page Headline"
    jcr:description="Display Page Heading"
    sling:resourceSuperType="core/wcm/components/title/v2/title"
    componentGroup="My Project"/>

com.example.core.models.PageHeadline

package com.example.core.models;

import com.adobe.cq.wcm.core.components.models.Title;
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.via.ResourceSuperType;

@Model(adaptables = SlingHttpServletRequest.class, adapters = Title.class, resourceType = "myproject/components/pageHeadline")
public class PageHeadline implements Title {

    @ScriptVariable
    private Page currentPage;

    @Self @Via(type = ResourceSuperType.class)
    private Title delegate;

    @Override
    public String getText() {
        return currentPage.getTitle();
    }

    @Override
    public String getType() {
        return delegate.getType();
    }
}

结果

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.example.core.models.Title cannot be correctly instantiated by the Use API
...
Caused by: org.apache.sling.scripting.sightly.SightlyException: Identifier com.example.core.models.Title cannot be correctly instantiated by the Use API
    at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:78) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:69) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.sightly.apps.core.wcm.components.title.v2.title.title_html.render(title_html.java:41)
    at org.apache.sling.scripting.sightly.java.compiler.RenderUnit.render(RenderUnit.java:48) [org.apache.sling.scripting.sightly.compiler.java:1.0.26.1_4_0]
    at org.apache.sling.scripting.sightly.impl.engine.SightlyCompiledScript.eval(SightlyCompiledScript.java:61) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:386) [org.apache.sling.scripting.core:2.0.54]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:184) [org.apache.sling.scripting.core:2.0.54]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:491) [org.apache.sling.scripting.core:2.0.54]
    ... 269 common frames omitted
Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.example.core.models.PageHeadline
    at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:765) [org.apache.sling.models.impl:1.4.10]
    at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:448) [org.apache.sling.models.impl:1.4.10]
    at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:314) [org.apache.sling.models.impl:1.4.10]
    at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:126) [org.apache.sling.scripting.sightly.models.provider:1.0.6]
    at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:73) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    ... 276 common frames omitted
    Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private com.example.core.models.Title com.example.core.models.PageHeadline.title
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:749) [org.apache.sling.models.impl:1.4.10]
        ... 280 common frames omitted
    Caused by: org.apache.sling.models.factory.ModelClassException: Could not inject field due to reflection issues
        at org.apache.sling.models.impl.model.InjectableField.set(InjectableField.java:48) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.setField(ModelAdapterFactory.java:989) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.access$200(ModelAdapterFactory.java:132) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory$SetFieldCallback.inject(ModelAdapterFactory.java:500) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:595) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:744) [org.apache.sling.models.impl:1.4.10]
        ... 280 common frames omitted
    Caused by: java.lang.IllegalArgumentException: Can not set com.example.core.models.Title field com.example.core.models.PageHeadline.title to com.adobe.cq.wcm.core.components.internal.models.v1.TitleImpl
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
        at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
        at java.lang.reflect.Field.set(Field.java:764)
        at org.apache.sling.models.impl.model.InjectableField.set(InjectableField.java:46) [org.apache.sling.models.impl:1.4.10]
        ... 285 common frames omitted
4

1 回答 1

1

我遇到了同样的问题,委托模式不起作用,具体来说,如果我使用了注入的属性,则为 null @Optional,或者如果省略,则与发布的相同的堆栈跟踪@Optional

我的问题是我们正在使用以下Embed-Dependency代码:

<Embed-Dependency>
  *;scope=compile|runtime
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>

maven-bundle-plugin. 问题是核心组件依赖被设置为嵌入式依赖。一旦我解决了这个问题,委托模式就可以正常工作。

<Embed-Dependency>
  *;scope=compile|runtime;artifactId=!core.wcm.components.core
</Embed-Dependency>
于 2019-07-10T19:42:54.383 回答