Внедрение зависимостей не работает в Wildfly-Swarm-2016.12.1
Я новичок в рою полевых мух. Я смог подготовить простое приложение JAX-RS в Swarm и работал как шарм. Но когда я попытался ввести CDI в свое приложение, все пошло не так. Инъекция бобов не работает! Я получаю NULLPointerException в тех местах, где я пытался вызвать методы bean. Я перепробовал все возможные решения, которые мог найти через Интернет. Но ничего не сработало. Ниже приведен мой конфиг pom.xml;
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.uss</groupId>
<artifactId>hatts-swarm</artifactId>
<version>0.0.1-M0</version>
<packaging>war</packaging>
<properties>
<version.wildfly.swarm>2016.12.1</version.wildfly.swarm>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>bom-all</artifactId>
<version>${version.wildfly.swarm}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>jax_cdi</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly.swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
<version>2016.12.1</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs-cdi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>weld</artifactId>
</dependency>
</dependencies>
</project>
и следующий мой основной класс;
public class BootStrap {
/**
* @param args
*/
public static void main(String[] args) {
try {
Swarm swarm = new Swarm(true);
JAXRSArchive deployment = ShrinkWrap.create(JAXRSArchive.class, "jax_cdi_microprofile");
deployment.addClass(RESTEntryManager.class).addClass(HTML5Generator.class)
.addClass(HattsACKTestService.class)
.addAsWebInfResource(new StringAsset(new StringBuilder("<?xml version=\"1.0\"?>")
.append(" <beans bean-discovery-mode=\"all\" version=\"1.1\" ")
.append(" xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"")
.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"")
.append(" xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd\"/>")
.toString()), "beans.xml")
.addAllDependencies();
swarm.start().deploy(deployment);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
и следующее - мое объявление ресурса JAX-RS;
@Path("/handshake-test")
@Stateless
public class HattsACKTestService {
@Resource(lookup = "java:jboss/ee/concurrency/executor/default")
private ManagedExecutorService managedExecutorService;
@Inject
private Authenticator authenticator;
public HattsACKTestService() {
// INTENTIONALLY LEFT AS BLANK
}
@GET
public void handshake(@Suspended AsyncResponse asyncResponse) {
/*
* new Thread(() -> {
*
* HTML5Generator html5Generator = new HTML5Generatorg.Builder()
* .setPageTitle("HATTS Handshake Service Response").appendPageBody(
* "H1", "Handshake Initiated...") .build();
*
* asyncResponse.resume(html5Generator.getContentBuilder().toString());
*
* }).start();
*/
/*managedExecutorService.submit(() -> {
asyncResponse.resume("Hi World!!!.. This is Wildfly in CDI !!!");
});*/
asyncResponse.resume(authenticator.authenticate());
}
}
и далее - бин Authenticator;
@Stateless
public class Authenticator {
/**
*
*/
public Authenticator() {
// TODO Auto-generated constructor stub
}
public String authenticate() {
return "Authenticated !!";
}
}
Даже не ManagedExectorService
вводится! Может ли кто-нибудь помочь мне разобраться с этим?
Обновление с последней полученной ошибкой, после попытки решения, упомянутого Кеном
2017-01-08 20:31:01,290 ERROR [stderr] (main) java.lang.reflect.InvocationTargetException
2017-01-08 20:31:01,291 ERROR [stderr] (main) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-01-08 20:31:01,291 ERROR [stderr] (main) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-01-08 20:31:01,292 ERROR [stderr] (main) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-01-08 20:31:01,292 ERROR [stderr] (main) at java.lang.reflect.Method.invoke(Method.java:498)
2017-01-08 20:31:01,292 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.MainInvoker.invoke(MainInvoker.java:37)
2017-01-08 20:31:01,293 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.Main.run(Main.java:44)
2017-01-08 20:31:01,293 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.Main.main(Main.java:35)
2017-01-08 20:31:01,294 ERROR [stderr] (main) Caused by: org.wildfly.swarm.container.DeploymentException: WFSWARM0007: Deployment failed: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"hatts.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"hatts.war\".WeldStartService: Failed to start service
2017-01-08 20:31:01,294 ERROR [stderr] (main) Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Authenticator with qualifiers @Default
2017-01-08 20:31:01,294 ERROR [stderr] (main) at injection point [UnbackedAnnotatedField] @Inject private com.uss.hatts.rest.acktest.HattsACKTestService.authenticator
2017-01-08 20:31:01,294 ERROR [stderr] (main) at com.uss.hatts.rest.acktest.HattsACKTestService.authenticator(HattsACKTestService.java:0)
2017-01-08 20:31:01,294 ERROR [stderr] (main) "},"WFLYCTL0412: Required services that are not installed:" => ["jboss.deployment.unit.\"hatts.war\".WeldStartService"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
2017-01-08 20:31:01,295 ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:280)
2017-01-08 20:31:01,295 ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:95)
2017-01-08 20:31:01,295 ERROR [stderr] (main) at org.wildfly.swarm.Swarm.deploy(Swarm.java:475)
2017-01-08 20:31:01,295 ERROR [stderr] (main) at org.wildfly.swarm.Swarm.main(Swarm.java:612)
2017-01-08 20:31:01,295 ERROR [stderr] (main) ... 7 more
2017-01-08 20:31:01,296 ERROR [stderr] (main) Exception in thread "main" java.lang.reflect.InvocationTargetException
2017-01-08 20:31:01,296 ERROR [stderr] (main) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017-01-08 20:31:01,301 ERROR [stderr] (main) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2017-01-08 20:31:01,303 ERROR [stderr] (main) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2017-01-08 20:31:01,304 ERROR [stderr] (main) at java.lang.reflect.Method.invoke(Method.java:498)
2017-01-08 20:31:01,304 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.MainInvoker.invoke(MainInvoker.java:37)
2017-01-08 20:31:01,304 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.Main.run(Main.java:44)
2017-01-08 20:31:01,304 ERROR [stderr] (main) at org.wildfly.swarm.bootstrap.Main.main(Main.java:35)
2017-01-08 20:31:01,305 ERROR [stderr] (main) Caused by: org.wildfly.swarm.container.DeploymentException: WFSWARM0007: Deployment failed: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"hatts.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"hatts.war\".WeldStartService: Failed to start service
2017-01-08 20:31:01,305 ERROR [stderr] (main) Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Authenticator with qualifiers @Default
2017-01-08 20:31:01,305 ERROR [stderr] (main) at injection point [UnbackedAnnotatedField] @Inject private com.uss.hatts.rest.acktest.HattsACKTestService.authenticator
2017-01-08 20:31:01,305 ERROR [stderr] (main) at com.uss.hatts.rest.acktest.HattsACKTestService.authenticator(HattsACKTestService.java:0)
2017-01-08 20:31:01,305 ERROR [stderr] (main) "},"WFLYCTL0412: Required services that are not installed:" => ["jboss.deployment.unit.\"hatts.war\".WeldStartService"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
2017-01-08 20:31:01,305 ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:280)
2017-01-08 20:31:01,306 ERROR [stderr] (main) at org.wildfly.swarm.container.runtime.RuntimeDeployer.deploy(RuntimeDeployer.java:95)
2017-01-08 20:31:01,306 ERROR [stderr] (main) at org.wildfly.swarm.Swarm.deploy(Swarm.java:475)
2017-01-08 20:31:01,306 ERROR [stderr] (main) at org.wildfly.swarm.Swarm.main(Swarm.java:612)
2017-01-08 20:31:01,308 ERROR [stderr] (main) ... 7 more
1 ответ
Ваши зависимости Maven должны быть:
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>
weld
был переименован в cdi
несколько релизов назад, и jaxrs-cdi
больше не требуется явно. Это принесено для вас.
С тем, что у вас есть в пользовательском main(), я бы рекомендовал не использовать его вообще, а просто позволить WF Swarm создать ваше развертывание для вас.