Skip to content
Michael Edgar edited this page Apr 19, 2018 · 1 revision

Usage

1) Simple

Property Inject relies on convention in the absence of configuration. In the example below, Property Inject searches for a system property passed to the Java process as package + class + field.

-Dcom.example.injection.Example.simple=theValue

If no such system property is found, Property Inject searches for property key 'simple' in properties file com/example/injection/Example.properties on the class path.

When neither a system property nor a matching properties file is found, the value will be set to "theDefault" as configured. If no defaultValue is configured, the value will be null.

package com.example.injection;

import javax.inject.Inject;
import io.xlate.inject.Property;

public class Example {
    @Inject
    @Property(defaultValue = "theDefault")
    private String simple;
}

2) Overriding the System Property Name

package com.example.injection;

import javax.inject.Inject;
import io.xlate.inject.Property;

public class Example {
    @Inject
    @Property(systemProperty = "example.simple")
    private String simple;
}

In this example, the extension searches for a system property passed to the Java process as named by the systemProperty attribute of @Property.

-Dexample.simple=theValue

As with the first example, if not found in the system properties, searches for property key 'simple' in properties file /com/example/injection/Example.properties on the class path.

When neither a system property nor a matching properties file is found, the value will be set to null since no defaultValue has been configured on the @Property.

3) Specifying a properties file

package com.example.injection;

import javax.inject.Inject;
import io.xlate.inject.Property;

public class Example {
    @Inject
    @Property(resource = @PropertyResource("classpath:config/my-app.properties"))
    private String simple;
}

In this example, the resource attribute of @Property specifies that a file named config/my-app.properties is available on the CLASSPATH. The properties will be loaded and the value of key simple injected to the field. Note that the properties will only be searched if no value is found for system property com.example.injection.Example.simple.

If the properties file does not exist or there is no key simple, the value will be set to null since no defaultValue has been configured on the @Property.

Clone this wiki locally