SeamFramework.orgCommunity Documentation

Chapter 5. Injecting Resources and System Properties

5.1. Resource Loading
5.1.1. Extending the Resource Loader
5.2. System Properties

Solder provides an extensible, injectable resource loader. The resource loader can provide URLs or managed input streams. By default the resource loader will look at the classpath, and the servlet context if available.

If the resource name is known at development time, the resource can be injected, either as a URL or an InputStream:

   @Inject

     @Resource("WEB-INF/beans.xml")
     URL beansXml;
     @Inject
     @Resource("WEB-INF/web.xml")
     InputStream webXml;

If the resource name is not known, the ResourceProvider can be injected, and the resource looked up dynamically:

   @Inject

     void readXml(ResourceProvider provider, String fileName) {
        InputStream is = provider.loadResourceStream(fileName);
     }

If you need access to all resources under a given name known to the resource loader (as opposed to first resource loaded), you can inject a collection of resources:

   @Inject

     @Resource("WEB-INF/beans.xml")
     Collection<URL> beansXmls;
     @Inject
     @Resource("WEB-INF/web.xml")
     Collection<InputStream> webXmls;

If the resource is a Properties bundle, you can also inject it as a set of Properties:

   @Inject

     @Resource("META-INF/aws.properties")
     Properties awsProperties;

Solder allows system properties to be easily injected using the @System qualifier. The following code snippet shows how you can inject system properties directly into your own bean:

import java.util.Properties;
import org.jboss.solder.core.System;
import javax.inject.Inject;

public class Foo {
    @Inject @System Properties properties;
    
    //..
}

Solder also exposes the system properties as a named bean called sysProp, allowing them to be referenced directly via EL (Expression Language), for example from a JSF page definition. Please refer to the org.jboss.solder.system.SystemProperties class in the Solder API documentation for a list of the available methods.