in the mythosil

Rhythm & Biology.

Weld: Java SE環境でCDI利用

  • Java EEのアプリケーションコンテナを利用せず、Java SE環境でCDIを利用する方法。
  • CDIコンテナにはWeldを利用。

ソースコード

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>com.mythosil</groupId>
    <artifactId>weldse</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-se</artifactId>
            <version>1.0.1-Final</version>
        </dependency>
    </dependencies>
</project>

WeldApplication.java

package com.mythosil.weldse;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

@Singleton
public class WeldApplication {
    
    @Inject
    private Calc calc;
    
    public void run() {
        System.out.println(calc.add(1, 3));
    }
    
    @PostConstruct
    public void postConstruct() {
        System.out.println("WeldApplication: PostConstruct");
    }
    
    @PreDestroy
    public void preDestroy() {
        System.out.println("WeldApplication: PreDestroy");
    }
    
    public static void main(String[] args) {
        Weld weld = new Weld();
        WeldContainer container = weld.initialize();
        WeldApplication app = container.instance().select(WeldApplication.class).get();
        app.run();
        weld.shutdown();
    }
    
}

Calc.java

package com.mythosil.weldse;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Calc {
    
    public int add(int a, int b) {
        return a + b;
    }
    
    @PostConstruct
    public void postConstruct() {
        System.out.println("Calc: PostConstruct");
    }
    
    @PreDestroy
    public void preDestroy() {
        System.out.println("Calc: PreDestroy");
    }
    
}

実行

$ mvn exec:java -Dexec.mainClass=com.mythosil.weldse.WeldApplication                                      [~/NetBeansProjects/weldse]
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building weldse 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ weldse ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
70 [com.mythosil.weldse.WeldApplication.main()] INFO org.jboss.weld.Version - WELD-000900 1.0.1 (Final)
88 [com.mythosil.weldse.WeldApplication.main()] INFO org.jboss.weld.Bootstrap - WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
570 [com.mythosil.weldse.WeldApplication.main()] WARN org.jboss.interceptor.model.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
571 [com.mythosil.weldse.WeldApplication.main()] WARN org.jboss.interceptor.model.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
Calc: PostConstruct
WeldApplication: PostConstruct
4
WeldApplication: PreDestroy
Calc: PreDestroy
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.856 s
[INFO] Finished at: 2014-10-22T23:25:21+09:00
[INFO] Final Memory: 11M/114M
[INFO] ------------------------------------------------------------------------

CDI 2.0