Skip to content

Instantly share code, notes, and snippets.

@sizovs
Created July 31, 2014 13:30
Show Gist options
  • Save sizovs/aed5d4e225aebe1e0918 to your computer and use it in GitHub Desktop.
Save sizovs/aed5d4e225aebe1e0918 to your computer and use it in GitHub Desktop.
@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.PARAMETER])
@BindingAnnotation
@AutoBind
@interface MySQL {
String databaseName()
}
@Immutable
class MySQLImpl implements MySQL {
final String databaseName
String databaseName() {
databaseName
}
// This is specified in java.lang.Annotation.
int hashCode() {
(127 * "databaseName".hashCode()) ^ databaseName.hashCode()
}
class<? extends Annotation> annotationType() {
MySQL
}
}
class MySQLAutoBindProvider implements AutoBindProvider<MySQL> {
public void configure(Binder binder, MySQL annotation) {
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/${annotation.databaseName()}?user=admin&password=d0cker")
binder.bind(Sql).annotatedWith(new MySQLImpl(annotation.databaseName())).toInstance(sql)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment