Java Standalone CDI Example using Apache OpenWebBeans..

Here we go..

Directory Layout

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>biz.tugay</groupId>
    <artifactId>cdiexample</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <dependencies>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-impl</artifactId>
            <version>1.6.3</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.cdiexample.TestClass</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

beans.xml
<beans></beans>

ServiceDependency.java
package biz.tugay.cdiexample;
 
/* User: koray@tugay.biz Date: 2016/08/12 */
 
import javax.inject.Named;

public class ServiceDependency {
 
    public void foo(){
        System.out.println("Hello!");
    }
}

Service.java
package biz.tugay.cdiexample;
 
import javax.inject.Inject;
import javax.inject.Named;
 
/* User: koray@tugay.biz Date: 2016/08/12 */
 
@Named
public class Service {
 
    @Inject
    private ServiceDependency serviceDependency;
 
    public void foo(){
        serviceDependency.foo();
    }
}

TestClass.java
import javax.enterprise.inject.spi.Bean;
import java.util.Set;
 
/* User: koray@tugay.biz Date: 2016/08/12 */
public class TestClass {
 
    public static void main(String[] args) {
        final ContainerLifecycle lifecycle
                = WebBeansContext.currentInstance().getService(ContainerLifecycle.class);
        lifecycle.startApplication(null);
 
        final Set<Bean<?>> serviceBean =
                lifecycle.getBeanManager().getBeans("service");
 
        final Bean<?> next = serviceBean.iterator().next();
        final Service service = 
            (Service) lifecycle.getBeanManager().getReference(next, Service.class, null);
        service.foo();
    }
}

In Action
Korays-MacBook-Pro:cdiexample koraytugay$ mvn clean install
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.078 s
[INFO] Finished at: 2016-08-12T14:04:32+03:00
[INFO] Final Memory: 18M/300M
[INFO] ------------------------------------------------------------------------
 
Korays-MacBook-Pro:cdiexample koraytugay$ mvn exec:java
Aug 12, 2016 2:04:35 PM org.apache.webbeans.lifecycle.AbstractLifeCycle bootstrapApplication
INFO: OpenWebBeans Container is starting...
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: file:/Users/koraytugay/Desktop/cdiexample/target/classes/META-INF/beans.xml
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/MRJToolkit.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/jai_codec.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/j3dcore.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/j3dutils.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/jai_core.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/dns_sd.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/vecmath.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/mlibwrapper_jai.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/j3daudio.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery addWebBeansXmlLocation
INFO: added beans archive URL: jar:file:/Library/Java/Extensions/AppleScriptEngine.jar!/
Aug 12, 2016 2:04:35 PM org.apache.webbeans.config.BeansDeployer validateInjectionPoints
INFO: All injection points were validated successfully.
Aug 12, 2016 2:04:35 PM org.apache.webbeans.lifecycle.AbstractLifeCycle bootstrapApplication
INFO: OpenWebBeans Container has started, it took [212] ms.
Hello!