Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Built in Annotations

Mahmoud Ben Hassine edited this page Jun 7, 2020 · 14 revisions

Introduction

Easy Props provides 9 annotations to load configuration properties from a variety of sources. It also allows you to write your own annotations to load properties from a custom location.

@Property

This annotation can be used to load a property from a java properties file.

The annotation can be declared on a field to tell Easy Props to inject the specified key value from the properties file into the annotated field.

Attributes of this annotation are described in the following table:

Attribute Type Required Description
source String yes The properties file name
key String yes The key to load from the source properties file

Example:

@Property(source = "myProperties.properties", key = "bean.name")
private String beanName;

In this example, Easy Props will look for the property bean.name in the myProperties.properties properties file in the classpath and set its value to the beanName field.

NB: You can use one of resource prefixes "classpath:" or "file:" to load properties from the classpath or the file system.

@SystemProperty

This annotation allows you to inject a property from the system properties passed to your java program at JVM startup using the -D prefix.

The @SystemProperty annotation has the following attributes:

Attribute Type Required Description
value String yes The system property to inject in the annotated field
defaultValue String no The default value to set in case the system property does not exist

Example:

@SystemProperty("user.home")
private String userHome;

In this example, Easy Props will look for the system property user.home and set its value to the userHome field.

@EnvironmentVariable

This annotation allows you to inject an environment variable in a field.

The @EnvironmentVariable annotation has the following attributes:

Attribute Type Required Description
value String yes The environment variable to inject in the annotated field
defaultValue String no The default value to set in case the environment variable does not exist

Example:

@EnvironmentVariable("JAVA_HOME")
private String javaHome;

In this example, Easy Props will look for the environment variable JAVA_HOME and set its value to the javaHome field.

@I18NProperty

This annotation allows you to inject a property from a java resource bundle for a specified locale.

The annotation can be declared on a field to tell Easy Props to inject the specified key value from the resource bundle into the annotated field.

Attributes of this annotation are described in the following table:

Attribute Type Required Description
bundle String yes The resource bundle containing the property to load
key String yes The key to load from the resource bundle
language String no The locale language to use (default locale language)
country String no The locale country to use (default locale country)
variant String no The locale variant to use (default locale variant)

Example:

@I18NProperty(bundle = "i18n/messages", key = "my.message")
private String message;

In this example, Easy Props will look for the property my.message in the resource bundle i18n/messages.properties in the classpath and set its value to the message field.

Note that this annotation is not suited to applications where the locale may change during the application lifetime. This is not a limitation of Easy Props but it is related to the fact that java annotation attributes can only have constant values.

@DBProperty

This annotation can be used to load properties from a database.

Attributes of this annotation are described in the following table:

Attribute Type Required Description
configuration String yes The configuration file containing database connection properties.
key String yes The key to load from the specified column in database table

The annotation processor of @DBProperty will load database connection properties from the file specified in the configuration attribute. The following is an example of a configuration file:

db.driver=org.hsqldb.jdbcDriver
db.url=jdbc:hsqldb:mem:test
db.user=sa
db.password=pwd
db.schema=public
db.table=ApplicationProperties
db.table.keyColumn=key
db.table.valueColumn=value

Properties db.driver, db.url, db.user, db.password and db.schema are self explanatory: Easy Props will use these parameters to connect to the database.

db.table specifies the table containing keys (in db.table.keyColumn column) and values (in db.table.valueColumn column).

Example:

@DBProperty(configuration = "database.properties", key = "bean.name")
private String name;

In this example, if we use the previous configuration file, Easy Props will look for the key bean.name in the key column of the ApplicationProperties table defined in test database and set its value to the name field.

Note that Easy Props caches properties loaded from a database for further reuse. If you have more than one field annotated with @DBProperty with the same database configuration, Easy Props will connect only once the the specified database.

@JNDIProperty

This annotation can be declared on a field to inject a property or an object from a JNDI context.

The annotation have a single attribute that corresponds to the object name in JNDI context.

Example:

@JNDIProperty("jdbc/dataSource")
private javax.sql.DataSource dataSource;

In this example, Easy Props will look for an object named jdbc/dataSource in JNDI context and inject it in dataSource field.

Of course, you should have already specified JNDI parameters (context provider, factory, etc) via system properties or a properties file jndi.properties in the classpath (standard JNDI configuration).

@ManifestProperty

This annotation can be used to inject a header value from a META-INF/MANIFEST.MF file in the annotated field.

Attributes of this annotation are described in the following table:

Attribute Type Required Description
jar String yes The jar file containing the manifest file to load
header String yes The header to load from the specified manifest file

Example:

@ManifestProperty(jar = "junit.jar", header = "Created-By")
private String createdBy;

In this example, we are injecting the Created-By header value of the META-INF/MANIFEST.MF file of the jar junit.jar into the createdBy field.

You may ask for a use case of such an annotation. Actually, this can be useful if you like to create a dashboard which lists your application's dependencies versions at runtime.

@MavenProperty

This annotation can be used to load and inject maven properties into java objects. A typical usage of this annotation is to know the application version at runtime (a bit like @ManifestProperty).

Attributes of this annotation are described in the following table:

Attribute Type Required Description
source String no The source file containing maven properties (META-INF/maven/pom.properties by default)
key String yes The key to load from the specified pom properties
groupId String yes The groupId of the JAR containing the pom.properties file
artifactId String yes The artifact of the JAR containing the pom.properties file

Example:

@MavenProperty(key = "version", groupId = "commons-beanutils", artifactId = "commons-beanutils")
private String pomVersion;

In this example, we are injecting the current version number of the commons-beanutils dependency into the pomVersion field.

Note that Easy Props caches maven context loaded from pom.properties for further reuse.

NB: You can use one of resource prefixes "classpath:" or "file:" to load properties from the classpath or the file system.

@Properties

This annotation can be declared on a field of type java.util.Properties and allows you to inject all properties of a properties file in that field.

The annotation have a single attribute that corresponds to the properties file name.

Example:

@Properties("myProperties.properties")
private java.util.Properties myProperties;

In this example, Easy Props will populate the myProperties field with properties from the file myProperties.properties.

NB: You can use one of resource prefixes "classpath:" or "file:" to load properties from the classpath or the file system.