public interface Provider<T> {
T get();
}
public class DatabaseTransactionLogProvider implements Provider<TransactionLog> {
private final Connection connection;
@Inject
public DatabaseTransactionLogProvider(Connection connection) {
this.connection = connection;
}
public TransactionLog get() {
DatabaseTransactionLog transactionLog = new DatabaseTransactionLog();
transactionLog.setConnection(connection);
return transactionLog;
}
}
@Override
public void bindServices(ServiceCollection serviceCollection) {
serviceCollection.bindServiceProvider(TransactionLog.class, DatabaseTransactionLogProvider.class);
}
public class LogService {
@Inject
private Provider<TransactionLog> logProvider;
public void log(String message){
TransactionLog logger = logProvider.get();
// do something ....
}
}