com.mdaniline:spring-autoproperties

Automatically implement interfaces using Value annotations

License

License

Categories

Categories

Auto Application Layer Libs Code Generators
GroupId

GroupId

com.mdaniline
ArtifactId

ArtifactId

spring-autoproperties
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

com.mdaniline:spring-autoproperties
Automatically implement interfaces using Value annotations
Project URL

Project URL

https://github.com/mdaniline/spring-autoproperties
Source Code Management

Source Code Management

http://github.com/mdaniline/spring-autoproperties

Download spring-autoproperties

How to add to project

<!-- https://jarcasting.com/artifacts/com.mdaniline/spring-autoproperties/ -->
<dependency>
    <groupId>com.mdaniline</groupId>
    <artifactId>spring-autoproperties</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.mdaniline/spring-autoproperties/
implementation 'com.mdaniline:spring-autoproperties:1.0.0'
// https://jarcasting.com/artifacts/com.mdaniline/spring-autoproperties/
implementation ("com.mdaniline:spring-autoproperties:1.0.0")
'com.mdaniline:spring-autoproperties:jar:1.0.0'
<dependency org="com.mdaniline" name="spring-autoproperties" rev="1.0.0">
  <artifact name="spring-autoproperties" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.mdaniline', module='spring-autoproperties', version='1.0.0')
)
libraryDependencies += "com.mdaniline" % "spring-autoproperties" % "1.0.0"
[com.mdaniline/spring-autoproperties "1.0.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.springframework : spring-core jar [3.2,)
org.springframework : spring-context jar [3.2,)
org.springframework : spring-beans jar [3.2,)
com.google.guava : guava jar [12,)

test (3)

Group / Artifact Type Version
junit : junit jar 4.11
org.hamcrest : hamcrest-library jar 1.3
org.springframework : spring-test jar [3.2,)

Project Modules

There are no modules declared in this project.

spring-autoproperties

Build Status

A Spring plugin to automatically generate implementations of interfaces using @Value annotations. This is handy for organising multiple related properties into a single beans without needing to implement the bean itself.

Installation

Get the jar through Maven:

<dependency>
    <groupId>com.mdaniline</groupId>
    <artifactId>spring-autoproperties</artifactId>
    <version>1.0.1</version>
</dependency>

Dependencies

  • Java 1.6+
  • Spring 4.2+

Usage

  1. Annotate your application config bean with @EnableAutoProperties. You will need to specify the basePackages and/or basePackageClasses properties to configure which packages should be scanned.
  2. Create an interface which you want to proxy and annotate it with @AutoProperties. Every method should be in getter-style (non-void return value and no parameters) and annotated with @Value. For example:
@AutoProperties
public interface DatabaseSettings {

    @Value("${db.connectionString}")
    String getConnectionString();

    @Value("${db.username}")
    String getUsername();

    @Value("${db.password}")
    String getPassword();

    @Value("${db.pool.maxActive}")
    Integer getMaxActiveConnections();
}

Why would I bother?

You can now inject this single interface into your beans instead of a long, messy list of properties, e.g.

public class CustomDataSource {
    @Autowired
    public CustomDataSource(DatabaseSettings settings) {
        ...
    }
    ...
}

instead of

public class CustomDataSource {
    @Autowired
    public CustomDataSource(@Value("${db.connectionString}") String connectionString,
                            @Value("${db.username}") String username,
                            @Value("${db.password}") String password,
                            @Value("${db.pool.maxActive}") Integer maxActiveConnections) {
        ...
    }
    ...
}

Versions

Version
1.0.0