java AEM 6.5@Reference(target=“(component.name=componentName)”)不工作
我正在尝试使用@Reference
注释的target
属性注入AEM组件
这就是我试图注入的组件
import org.osgi.service.component.annotations.Component;
import okhttp3.Interceptor;
@Component(immediate = true, name = "myInterceptorComponent", service = {Interceptor.class})
public class myInterceptor implements Interceptor {
public static final String COMPONENT_NAME = "myInterceptorComponent";
//... some implementation
}
这是使用它的组件,它是:
import okhttp3.Interceptor;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(immediate = true)
public final class anotherComponent {
@Reference(target = "(component.name=myInterceptorComponent)")
private Interceptor myInterceptor;
//... another implementation
}
这是我正在运行的测试:
@ExtendWith({AemContextExtension.class})
class anotherComponent Test {
private final AemContext context = AppAemContext.newAemContext();
@BeforeEach
public void setUp() {
this.context.registerInjectActivateService(myInterceptor.class);
this.context.registerInjectActivateService(anotherComponent.class);
}
@Test
void testMethod(){
//Test implementation
}
}
运行测试时,出现以下错误:
org.apache.sling.testing.mock.osgi.ReferenceViolationException: Unable to inject mandatory reference 'myInterceptor' for class com.test.anotherComponent : no matching services were found.
如果我使用@Reference
引用组件,而不使用target
属性,那么测试工作正常。不幸的是,我有不止一个拦截器,component.name
属性让我区分它们
你知道为什么会失败吗
# 1 楼答案
注册服务时,需要传递其他配置以指定目标名称。 在您的情况下,类似于: