gpt4 book ai didi

jersey - 无法在我的资源类中注入(inject) @Service 和 @Contract 依赖项

转载 作者:行者123 更新时间:2023-12-02 05:20:26 25 4
gpt4 key购买 nike

根据本博客的指南,Roll your own Auto Discovery with Jersey and HK2 ,我有以下资源 POJO:

@Path("Test")
public class TestResource {
@Inject
private TestService service;
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Set<Test> getTests() {
return service.getAllTests();
}
}

TestService:

@Contract
public interface TestService {
public Set<Test> getAllTests();
}

TestServiceImpl

@Service
public class TestServiceImpl implements TestService {

@Override
public Set<Test> getAllTests() {
Set<Test> tests = new HashSet<>();
Test c = new Test();
c.setName("test");
tests.add(c);
return tests;
}
}

pom.xml 中的 Jersey 依赖项版本为 2.25.1

   <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2</artifactId>
<version>2.5.0-b36</version>
</dependency>

为了让 Jersey 自动扫描 @Service 和 @Contract 类,我使用了版本 2.5.0-b36 的 inhabitant-generator 插件:

<plugin>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-inhabitant-generator</artifactId>
<version>2.5.0-b36</version>
<executions>
<execution>
<goals>
<goal>generate-inhabitants</goal>
</goals>
</execution>
</executions>
</plugin>

有相应的Feature实现:

public class AutoServiceDiscovery implements Feature {

@Override
public boolean configure(FeatureContext context) {
ServiceLocator locator = ServiceLocatorProvider.getServiceLocator(context);
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
Populator populator = dcs.getPopulator();
try {
populator.populate(new ClasspathDescriptorFileFinder(this.getClass().getClassLoader()),
new DuplicatePostProcessor());
} catch (IOException | MultiException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
return true;
}

}

它确实是通过我的 ResourceConfig 类注册的:

@ApplicationPath("/*")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
packages("resources");
register(new AutoServiceDiscovery());
}

}但是,我向 /test 发送请求,收到以下错误:

MultiException has 3 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for
injection at SystemInjecteeImpl(requiredType=TestService,parent=TestResource,qualifiers=
{},position=-1,optional=false,self=false,unqualified=null,1947073589)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of
rx.practice.ee.jaxrs.resources.TestResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on
rx.practice.ee.jaxrs.resources.TestResource

org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:89)
org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:250)
org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:358)
org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)
...

问题:有人知道为什么@Service类无法注入(inject)吗?我正在使用 Tomcat 服务器

最佳答案

经过几天的研究inhabitat-generator的源代码,我发现在 Web 应用程序包的情况下,war定位器文件未在 META-INF/hk2-locator 中生成如 HK2 Inhabitant Generator office site 所示如果使用 jar作为部署包。 source code of AbstractInhabitantsGeneratorMojo.java告诉如果 war定位器文件在 hk2-locator 中生成,而HK2 Inhabitant Generator office site中没有提到这一点。 .

但是,在构造 ClasspathDescriptorFileFinder 时如果引导类中没有目录名称参数,AutoServiceDiscovery ,仅与 jar 兼容作为部署包,这意味着它只能在 META-INF/hk2-locator 中查找文件.

所以更好的解决方案是不要使用 inhabitant-generator插件但是 metadata-generator依赖项,它是编译时的一个注释处理器,并且被证明是开箱即用的。

如果有人坚持使用这个插件,他/她可以创建他/她自己的ClasspathDescriptorFileFinder这样它就能够从hk2-locator找到定位器文件

最后但并非最不重要的一点是,我还尝试使用 inhabitants-generator插件的选项用于在hk2-locator中生成定位器文件,但这似乎也是几乎不可能的

关于jersey - 无法在我的资源类中注入(inject) @Service 和 @Contract 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60731674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com