0

Java SE CDI maven dependencies?

public class FooProcessor {

    private FooService fooService;

    @Inject
    public FooProcessor(FooService fooService) {
        this.fooService = fooService;
    }
}

What are the jar dependencies for CDI for Java SE?

Which jar dependency has @Inject? Tried

compile "org.jboss.weld.se:weld-se-core:3.0.2.Final"
compile 'org.apache.deltaspike.cdictrl:deltaspike-cdictrl-api'
compile group: 'javax.enterprise', name: 'cdi-api', version: '2.0.SP1'
compile group: 'javax', name: 'javaee-api', version: '7.0'
eastwater
  • 4,624
  • 9
  • 49
  • 118
  • Out of curiosity, what did you try in search engines? And the javaee-api should have it, so most likely you have other problems. – Kukeltje Apr 20 '20 at 09:20
  • 'javax.inject:javax.inject:1' is required to resolve @Inject. If it is removed, compiler error. – eastwater Apr 20 '20 at 22:34

2 Answers2

0

You can configure CDI in standalone mode by using Weld reference implementation.

Example:

Weld weld = new Weld();
WeldContainer container = weld.initialize();
FooProcessor fooProcessor = container.instance().select(FooProcessor.class).get();
weld.shutdown();

and weld dependency

<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-core</artifactId>
    <version>3.1.4.Final</version>
</dependency>

Regarding @Inject it coming from

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

and this coming from JSR 330: Dependency Injection For Java that introduces a standard set of annotations that can be used for dependency injection.

CodeScale
  • 3,046
  • 1
  • 12
  • 20
  • Did you notice that effectively OP has both these configured? Weld in an older version though and the `@Inject` in the broader javaee-api... – Kukeltje Apr 20 '20 at 09:21
  • 1
    but javaee-api does not have @Inject. – eastwater Apr 20 '20 at 15:43
  • gradle build added: compile 'javax.inject:javax.inject:1', but it does not contain @Inject. – eastwater Apr 20 '20 at 15:47
  • Added this ? ```compile group: 'javax.inject', name: 'javax.inject', version: '1'``` – CodeScale Apr 20 '20 at 15:55
  • @Sunnyday: Please prove me wrong. The [javaee-api pom](https://repo1.maven.org/maven2/javax/javaee-api/7.0/javaee-api-7.0.pom) has the javaee-web-api (https://repo1.maven.org/maven2/javax/javaee-web-api/7.0/javaee-web-api-7.0.pom) which has exactly the `javax.inject` with both the group and artifact id and version 1. 100% identical to what you posted. But regardles of this, **why** would the complete javaee-api **NOT** contain `@Inject`? So both you as a commenter and the upvoter are wrong imo (or my build system has been broken for many years and automagically fixing things I did wrong) – Kukeltje Apr 20 '20 at 17:47
  • And if even the explicit usage of `javax.inject` from this imo unhelpful answer (how well intended it might be, so no negative things here) does not work, you have a totally invisible problem (what is the package you imported for `@Inject`?) – Kukeltje Apr 20 '20 at 17:49
  • Unhelpful? Depends... for cdi SE I said "weld" for @inject I said "java.inject" jar ... is it an answer to the question? Seems yes... remember the question : What are the jar dependencies for CDI for Java SE? – – CodeScale Apr 20 '20 at 18:38
  • Is there a standard way to create CDI container independent of CDI implementations such as Weld? – eastwater Apr 20 '20 at 22:09
  • What do mean by standard way? Depending on the application server you running to you could have different implementation in action. Example in Apache Tomee the implementation is not weld but openwebbeans. Openwebbeans can be running in se mode too .``` org.apache.openwebbeans openwebbeans-se ``` – CodeScale Apr 20 '20 at 23:28
  • But OP already had the javaee-api as compile in its project, that contains `@Inject`... That is all I said.. So if that did not work, there is another prolem. If in your answer you'd explicitly stated that the javaee-api is to broad and the `javax.inject` is sufficient, that would be better, still if including the javaee-api bom (even if too broad) did not help. OP has a different problem. Which is confirmed by OP posting that adding `javax.inject` ALSO does not work... – Kukeltje Apr 21 '20 at 08:36
0

nowadays the annotation(s) mostly can come from 'javax.inject' and 'jakarta.inject' packages.

when starting sample within e.g. Weld 5.1.0 it is 'jakarta.inject', bundled into Weld itself

<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-shaded</artifactId>
    <version>5.1.0.Final</version>
</dependency>

also you NEED your class to be annotated with e.g. '@ApplicationScoped' - to be recognized by Weld as Weld-managed bean, and you NEED beans.xml (can set only 'bean-discovery-mode' value and define no beans, btw):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" bean-discovery-mode="annotated"/>

... in /META-INF of your classpath for Weld to init successfully.

this is enough to make the sample take off. you can optionally add

<dependency>
    <groupId>io.smallrye</groupId>
    <artifactId>jandex</artifactId>
    <version>3.0.5</version>
</dependency>

... for your Weld to start up (a little) faster.

p.s. and forget about @Inject static Logger log with Weld - even if strongly used in SE context, Weld(5.1.0) DOESN'T support it.

KDV
  • 31
  • 4