Tuesday, October 27, 2015

type-safe configs the second minute

in my first post about this topic i explained how to create a type-safe config based on interfaces and the partial-bean-module provided by deltaspike.

this second part is about a new possibility which is available since deltaspike v1.5.1.


until deltaspike v1.5.1, you needed to create and inject the type-safe config for accessing configured values. one part of the example shown in the first post looked like:
@TypeSafeConfig
public interface AppConfig {
String name();
ApplicationVersion version();
}
view raw AppConfig.java hosted with ❤ by GitHub
@Scheduled(cronExpression = "0 0/10 * * * ?")
public class CustomBatchStarter implements org.quartz.Job {
@Inject
private LogService logService;
@Inject
private AppConfig appConfig;
@Inject
private BatchConfig batchConfig;
@Inject
private CustomBatchService batchService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
logService.log(appConfig.name(), appConfig.version(),
getClass().getName() + " started");
batchService.start(batchConfig.batchSize());
}
}


since deltaspike v1.5.1, you can use @Produces in your type-safe config. with that you can directly inject the result instead of the config-class.
@TypeSafeConfig
public interface AppConfig2 {
String name();
@Produces //since ds v1.5.1
ApplicationVersion version();
}
view raw AppConfig2.java hosted with ❤ by GitHub
//example for injecting and using type-safe configs
@Scheduled(cronExpression = "0 0/10 * * * ?")
public class CustomBatchStarter2 implements org.quartz.Job {
@Inject
private LogService logService;
@Inject
private ApplicationVersion version;
@Inject
private BatchConfig batchConfig;
@Inject
private CustomBatchService batchService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
logService.log(appConfig.name(), version, getClass().getName() + " started");
batchService.start(batchConfig.batchSize());
}
}