Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
fschoeppl edited this page Apr 25, 2012 · 3 revisions

CDI is a lifecycle and dependency injection framework for Java (mainly EE). This project uses the reference implementation weld (v. 1.1.7-Final) which can be found [here] (http://seamframework.org/Weld), documentation [here] (http://seamframework.org/Weld/WeldDocumentation).

CDI is based on annotations, however one configuration file META-INF\beans.xml must be provided for each project. It must at least look like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

The BusinessLogic-Implementations must be available only once per application, therefore we specify the @ApplicationScoped annotation:

@ApplicationScoped
public class BusinessLogicImpl implements BusinessLogic {
  @Inject
  private DataAccessLayer dal;
  ...
}

As we need a DataAccessLayer implementation, we specify the @Inject-annotation. Note: we could use @Named(value=...) if we want a specific implementation. If CDI should call a factory method to create an instance of a type, we can use the @Produces annotation:

public class EntityManagerFactoryProducer {
  // creates one EntityManagerFactory for the whole application
  @Produces
  @ApplicationScoped
  public EntityManagerFactory create() {
    return Persistence.createEntityManagerFactory("org.backmeup.jpa");
  }
	
  // we close the EntityManagerFactory before it disposes
  public void destroy(@Disposes EntityManagerFactory factory) {
    factory.close();
  }
}

Configuration values might also be provided by factory methods, e.g. the plugin layer requires certain properties:

@ApplicationScoped
public class PluginImpl implements Plugin {
  ...
  @Inject @Named("deploymentDirectory")
  private File deploymentDirectory;
  @Inject @Named("temporaryDirectory")
  private File temporaryDirectory;	
  @Inject @Named("exportedPackages")
  private String exportedPackages;
  ...
}

A configuration class might provide these properties in the following way:

public class PluginImplConfiguration {
  private Properties loadProperties() {
    ... // e. g. load from a file
  }

  @Produces
  @Named("deploymentDirectory")
  public File getDeploymentDirectory() {
    return new File(this.loadProperties().getProperty("osgi.deploymentDirectory", "autodeploy"));
  }

  @Produces
  @Named("temporaryDirectory")
  public File getTemporaryDirectory() {
    return new File(this.loadProperties().getProperty("osgi.temporaryDirectory", "osgiTmp"));
  }

  @Produces
  @Named("exportedPackages")
  public String getExportedPackages() {
    return this.loadProperties().getProperty("osgi.exportedPackages");
  }
}

Starting Weld from a Client

The following snippet illustrates how to create an instance of weld and get access to the implementation of the BusinessLogic-interface:

Weld weld = new Weld();
WeldContainer container = weld.initialize();			    
BusinessLogic logic = container.instance().select(BusinessLogic.class).get();

The [REST layer] (wiki/rest-layer) uses this technique to get access to the Business Logic.

Clone this wiki locally