use singleton annotation instead of binding as singleton.

this is the correct way to do it. the way it is currently done means
that if a component is not declared in a module, guice still finds it
but does not use the singleton lifecycle

binding as singleton in the module is meant to be used for classes that
we don't have the sourcecode for (i.e. jdk classes)
This commit is contained in:
Christoph Sturm 2019-08-06 17:24:36 +02:00
parent aa4813d7b1
commit 643c0daef0
19 changed files with 89 additions and 39 deletions

View File

@ -38,6 +38,7 @@ import org.bitcoinj.utils.Fiat;
import org.bitcoinj.utils.MonetaryFormat;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
@ -59,6 +60,7 @@ import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
@Slf4j
@Singleton
public class BSFormatter {
public final static String RANGE_SEPARATOR = " - ";

View File

@ -35,6 +35,7 @@ import org.bitcoinj.core.Coin;
import org.bitcoinj.utils.MonetaryFormat;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.text.DecimalFormat;
import java.text.NumberFormat;
@ -44,6 +45,7 @@ import java.util.Locale;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Singleton
public class BsqFormatter extends BSFormatter {
@SuppressWarnings("PointlessBooleanExpression")
private static final boolean useBsqAddressFormat = true || !DevEnv.isDevMode();

View File

@ -18,26 +18,12 @@
package bisq.desktop;
import bisq.desktop.common.fxml.FxmlViewLoader;
import bisq.desktop.common.view.CachingViewLoader;
import bisq.desktop.common.view.ViewFactory;
import bisq.desktop.common.view.ViewLoader;
import bisq.desktop.common.view.guice.InjectorViewFactory;
import bisq.desktop.main.dao.bonding.BondingViewUtils;
import bisq.desktop.main.funds.transactions.DisplayedTransactionsFactory;
import bisq.desktop.main.funds.transactions.TradableRepository;
import bisq.desktop.main.funds.transactions.TransactionAwareTradableFactory;
import bisq.desktop.main.funds.transactions.TransactionListItemFactory;
import bisq.desktop.main.offer.offerbook.OfferBook;
import bisq.desktop.main.overlays.notifications.NotificationCenter;
import bisq.desktop.main.overlays.windows.TorNetworkSettingsWindow;
import bisq.desktop.main.presentation.DaoPresentation;
import bisq.desktop.main.presentation.MarketPricePresentation;
import bisq.desktop.util.Transitions;
import bisq.core.app.AppOptionKeys;
import bisq.core.locale.Res;
import bisq.core.util.BSFormatter;
import bisq.core.util.BsqFormatter;
import bisq.common.app.AppModule;
@ -57,32 +43,10 @@ public class DesktopModule extends AppModule {
@Override
protected void configure() {
bind(InjectorViewFactory.class).in(Singleton.class);
bind(ViewFactory.class).to(InjectorViewFactory.class);
bind(CachingViewLoader.class).in(Singleton.class);
bind(ResourceBundle.class).toInstance(Res.getResourceBundle());
bind(ViewLoader.class).to(FxmlViewLoader.class).in(Singleton.class);
bind(CachingViewLoader.class).in(Singleton.class);
bind(Navigation.class).in(Singleton.class);
bind(NotificationCenter.class).in(Singleton.class);
bind(OfferBook.class).in(Singleton.class);
bind(BSFormatter.class).in(Singleton.class);
bind(BsqFormatter.class).in(Singleton.class);
bind(TorNetworkSettingsWindow.class).in(Singleton.class);
bind(MarketPricePresentation.class).in(Singleton.class);
bind(DaoPresentation.class).in(Singleton.class);
bind(Transitions.class).in(Singleton.class);
bind(TradableRepository.class).in(Singleton.class);
bind(TransactionListItemFactory.class).in(Singleton.class);
bind(TransactionAwareTradableFactory.class).in(Singleton.class);
bind(DisplayedTransactionsFactory.class).in(Singleton.class);
bind(BondingViewUtils.class).in(Singleton.class);
bindConstant().annotatedWith(Names.named(AppOptionKeys.APP_NAME_KEY)).to(environment.getRequiredProperty(AppOptionKeys.APP_NAME_KEY));
}

View File

@ -28,6 +28,8 @@ import bisq.common.storage.Storage;
import com.google.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -41,6 +43,7 @@ import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
@Slf4j
@Singleton
public final class Navigation implements PersistedDataHost {
private static final ViewPath DEFAULT_VIEW_PATH = ViewPath.to(MainView.class, MarketView.class);

View File

@ -26,6 +26,7 @@ import bisq.desktop.common.view.ViewLoader;
import org.springframework.core.annotation.AnnotationUtils;
import javax.inject.Inject;
import javax.inject.Singleton;
import javafx.fxml.FXMLLoader;
@ -38,6 +39,7 @@ import java.util.ResourceBundle;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue;
@Singleton
public class FxmlViewLoader implements ViewLoader {
private final ViewFactory viewFactory;

View File

@ -18,9 +18,11 @@
package bisq.desktop.common.view;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.HashMap;
@Singleton
public class CachingViewLoader implements ViewLoader {
private final HashMap<Object, View> cache = new HashMap<>();

View File

@ -21,8 +21,11 @@ import bisq.desktop.common.view.ViewFactory;
import com.google.inject.Injector;
import javax.inject.Singleton;
import com.google.common.base.Preconditions;
@Singleton
public class InjectorViewFactory implements ViewFactory {
private Injector injector;

View File

@ -47,6 +47,7 @@ import org.bitcoinj.core.Coin;
import org.bitcoinj.core.InsufficientMoneyException;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Optional;
import java.util.function.Consumer;
@ -56,6 +57,7 @@ import lombok.extern.slf4j.Slf4j;
import static com.google.common.base.Preconditions.checkArgument;
@Slf4j
@Singleton
public class BondingViewUtils {
private final P2PService p2PService;
private final MyReputationListService myReputationListService;

View File

@ -20,7 +20,9 @@ package bisq.desktop.main.funds.transactions;
import bisq.core.btc.wallet.BtcWalletService;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class DisplayedTransactionsFactory {
private final BtcWalletService btcWalletService;
private final TradableRepository tradableRepository;

View File

@ -24,11 +24,13 @@ import bisq.core.trade.closed.ClosedTradableManager;
import bisq.core.trade.failed.FailedTradesManager;
import javax.inject.Inject;
import javax.inject.Singleton;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
@Singleton
public class TradableRepository {
private final OpenOfferManager openOfferManager;
private final TradeManager tradeManager;

View File

@ -23,7 +23,9 @@ import bisq.core.trade.Tradable;
import bisq.core.trade.Trade;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class TransactionAwareTradableFactory {
private final DisputeManager disputeManager;

View File

@ -27,11 +27,13 @@ import bisq.core.util.BSFormatter;
import org.bitcoinj.core.Transaction;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Optional;
import javax.annotation.Nullable;
@Singleton
public class TransactionListItemFactory {
private final BtcWalletService btcWalletService;
private final BsqWalletService bsqWalletService;

View File

@ -22,6 +22,7 @@ import bisq.core.offer.OfferBookService;
import bisq.core.trade.TradeManager;
import javax.inject.Inject;
import javax.inject.Singleton;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
@ -42,6 +43,7 @@ import static bisq.core.offer.OfferPayload.Direction.BUY;
* It also use OfferRepository.Listener as the lists items class and we don't want to get any dependency out of the
* package for that.
*/
@Singleton
@Slf4j
public class OfferBook {
private final OfferBookService offerBookService;

View File

@ -38,6 +38,8 @@ import bisq.common.UserThread;
import com.google.inject.Inject;
import javax.inject.Singleton;
import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription;
@ -54,6 +56,7 @@ import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
@Slf4j
@Singleton
public class NotificationCenter {
///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -38,6 +38,7 @@ import bisq.common.util.Tuple4;
import bisq.common.util.Utilities;
import javax.inject.Inject;
import javax.inject.Singleton;
import javafx.scene.Scene;
import javafx.scene.control.Button;
@ -72,6 +73,7 @@ import lombok.extern.slf4j.Slf4j;
import static bisq.desktop.util.FormBuilder.*;
@Slf4j
@Singleton
public class TorNetworkSettingsWindow extends Overlay<TorNetworkSettingsWindow> {
public enum BridgeOption {

View File

@ -12,6 +12,7 @@ import bisq.core.user.Preferences;
import bisq.common.app.DevEnv;
import javax.inject.Inject;
import javax.inject.Singleton;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
@ -25,6 +26,7 @@ import javafx.collections.MapChangeListener;
import lombok.Getter;
@Singleton
public class DaoPresentation implements DaoStateListener {
public static final String DAO_NEWS = "daoNewsVersion1.0.0";

View File

@ -35,6 +35,7 @@ import bisq.core.util.BSFormatter;
import bisq.common.UserThread;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription;
@ -60,6 +61,7 @@ import java.util.stream.Collectors;
import lombok.Getter;
@Singleton
public class MarketPricePresentation {
private final Preferences preferences;
private final BSFormatter formatter;

View File

@ -22,6 +22,7 @@ import bisq.core.user.Preferences;
import bisq.common.UserThread;
import javax.inject.Inject;
import javax.inject.Singleton;
import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
@ -39,6 +40,7 @@ import javafx.event.EventHandler;
import javafx.util.Duration;
@Singleton
public class Transitions {
public final static int DEFAULT_DURATION = 600;

View File

@ -2,23 +2,72 @@ package bisq.desktop;
import bisq.desktop.app.BisqAppModule;
import bisq.desktop.common.view.CachingViewLoader;
import bisq.desktop.common.view.ViewLoader;
import bisq.desktop.common.view.guice.InjectorViewFactory;
import bisq.desktop.main.dao.bonding.BondingViewUtils;
import bisq.desktop.main.funds.transactions.DisplayedTransactionsFactory;
import bisq.desktop.main.funds.transactions.TradableRepository;
import bisq.desktop.main.funds.transactions.TransactionAwareTradableFactory;
import bisq.desktop.main.funds.transactions.TransactionListItemFactory;
import bisq.desktop.main.offer.offerbook.OfferBook;
import bisq.desktop.main.overlays.notifications.NotificationCenter;
import bisq.desktop.main.overlays.windows.TorNetworkSettingsWindow;
import bisq.desktop.main.presentation.DaoPresentation;
import bisq.desktop.main.presentation.MarketPricePresentation;
import bisq.desktop.util.Transitions;
import bisq.core.app.AvoidStandbyModeService;
import bisq.core.app.BisqEnvironment;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import bisq.core.util.BSFormatter;
import bisq.core.util.BsqFormatter;
import org.springframework.mock.env.MockPropertySource;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertSame;
public class GuiceSetupTest {
private Injector injector;
@Before
public void setUp() {
Res.setup();
CurrencyUtil.setup();
injector = Guice.createInjector(new BisqAppModule(new BisqEnvironment(new MockPropertySource())));
}
@Test
public void testGuiceSetup() {
BisqAppModule module = new BisqAppModule(new BisqEnvironment(new MockPropertySource()));
Injector injector = Guice.createInjector(module);
injector.getInstance(CachingViewLoader.class);
injector.getInstance(AvoidStandbyModeService.class);
assertSingleton(OfferBook.class);
assertSingleton(CachingViewLoader.class);
assertSingleton(Navigation.class);
assertSingleton(InjectorViewFactory.class);
assertSingleton(NotificationCenter.class);
assertSingleton(BSFormatter.class);
assertSingleton(BsqFormatter.class);
assertSingleton(TorNetworkSettingsWindow.class);
assertSingleton(MarketPricePresentation.class);
assertSingleton(ViewLoader.class);
assertSingleton(DaoPresentation.class);
assertSingleton(Transitions.class);
assertSingleton(TradableRepository.class);
assertSingleton(TransactionListItemFactory.class);
assertSingleton(TransactionAwareTradableFactory.class);
assertSingleton(DisplayedTransactionsFactory.class);
assertSingleton(BondingViewUtils.class);
}
private void assertSingleton(Class type) {
assertSame(injector.getInstance(type), injector.getInstance(type));
}
}