Skip to content

Instantly share code, notes, and snippets.

@ljnelson
Created November 15, 2016 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljnelson/54ce8da402c69257e8e633c1e81bc6e5 to your computer and use it in GitHub Desktop.
Save ljnelson/54ce8da402c69257e8e633c1e81bc6e5 to your computer and use it in GitHub Desktop.
Start and run a CDI 2.0 container in a vendor-independent fashion
/*
* Copyright 2016 Laird Nelson.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.foobar;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
/**
* A class whose {@linkplain #main(String[]) <code>main</code> method}
* {@linkplain SeContainerInitializer#initialize() initializes} a new
* {@link SeContainer}.
*
* @author <a href="http://about.me/lairdnelson" target="_parent">Laird Nelson</a>
*
* @see SeContainerInitializer#initialize()
*/
public class Main {
/**
* Creates a new {@link Main}.
*/
public Main() {
super();
}
/**
* {@linkplain SeContainerInitializer#initialize() Initializes} a
* new {@link SeContainer} and then {@linkplain SeContainer#close()
* closes} it.
*
* @param args command-line arguments; may be {@code null}
*
* @exception Exception if an error occurs
*
* @see SeContainerInitializer#newInstance()
*
* @see SeContainerInitializer#initialize()
*
* @see SeContainer#close()
*/
public static final void main(final String[] args) throws Exception {
final SeContainerInitializer containerInitializer = SeContainerInitializer.newInstance();
assert containerInitializer != null;
try (final SeContainer container = containerInitializer.initialize()) {
assert container != null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment