當前位置: 首頁>>代碼示例>>Java>>正文


Java Singleton類代碼示例

本文整理匯總了Java中javax.inject.Singleton的典型用法代碼示例。如果您正苦於以下問題:Java Singleton類的具體用法?Java Singleton怎麽用?Java Singleton使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Singleton類屬於javax.inject包,在下文中一共展示了Singleton類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: provideNetworkInterceptor

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
@NetworkInterceptor
Interceptor provideNetworkInterceptor() {
    return new Interceptor() {
        @Override
        public Response intercept(@NonNull Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            String cacheControl = originalResponse.header("Cache-Control");
            if (cacheControl == null || cacheControl.contains("no-store") || cacheControl.contains("no-cache") ||
                    cacheControl.contains("must-revalidate") || cacheControl.contains("max-age=0")) {
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .header("Cache-Control", "public, max-age=" + 86400)// cache and reuse for 1 day(86400 seconds)
                        .build();
            } else {
                return originalResponse;
            }
        }
    };
}
 
開發者ID:vicky7230,項目名稱:Paprika,代碼行數:22,代碼來源:NetworkModule.java

示例2: provideBaseUrl

import javax.inject.Singleton; //導入依賴的package包/類
@Singleton
@Provides
HttpUrl provideBaseUrl() {
    if (mBaseUrl != null) {
        HttpUrl httpUrl = mBaseUrl.url();
        if (httpUrl != null) {
            return httpUrl;
        }
    }
    return mApiUrl == null ? HttpUrl.parse("https://api.github.com/") : mApiUrl;
}
 
開發者ID:yangxp108,項目名稱:MVPArms_Fragment-fragment,代碼行數:12,代碼來源:GlobalConfigModule.java

示例3: provideOkHttpClient

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
static OkHttpClient provideOkHttpClient() {
  return new OkHttpClient.Builder()
      .readTimeout(DEFAULT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
      .connectTimeout(DEFAULT_CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
      .addNetworkInterceptor(new StethoInterceptor())
      .build();
}
 
開發者ID:Elias33,項目名稱:Quran,代碼行數:10,代碼來源:DebugNetworkModule.java

示例4: createDeployment

import javax.inject.Singleton; //導入依賴的package包/類
@Deployment
public static WebArchive createDeployment() {
    String url = SimpleGetApi.class.getName() + "/mp-rest/url=http://localhost:8080";
    String scope = SimpleGetApi.class.getName() + "/mp-rest/scope=" + Singleton.class.getName();
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
        .addClasses(SimpleGetApi.class, MySingletonApi.class)
        .addAsManifestResource(new StringAsset(url + "\n" + scope), "microprofile-config.properties")
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    return ShrinkWrap.create(WebArchive.class)
        .addAsLibrary(jar)
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
 
開發者ID:eclipse,項目名稱:microprofile-rest-client,代碼行數:13,代碼來源:HasSingletonScopeTest.java

示例5: provideCache

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
public Cache provideCache(@Named("cacheDir") File cacheDir, @Named("cacheSize") long cacheSize) {

    Cache cache = null;

    try {
        cache = new Cache(new File(cacheDir.getPath(), "http-cache"), cacheSize);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cache;
}
 
開發者ID:Ryuuke,項目名稱:LeagueOfAndroid,代碼行數:15,代碼來源:ClientModule.java

示例6: proRxErrorHandler

import javax.inject.Singleton; //導入依賴的package包/類
/**
 * 提供處理 RxJava 錯誤的管理器
 *
 * @return
 */
@Singleton
@Provides
RxErrorHandler proRxErrorHandler(Application application, ResponseErrorListener listener) {
    return RxErrorHandler
            .builder()
            .with(application)
            .responseErrorListener(listener)
            .build();
}
 
開發者ID:Zweihui,項目名稱:Aurora,代碼行數:15,代碼來源:ClientModule.java

示例7: provideRxCacheDirectory

import javax.inject.Singleton; //導入依賴的package包/類
/**
 * 需要單獨給RxCache提供緩存路徑
 * 提供RxCache緩存地址
 */
@Singleton
@Provides
@Named("RxCacheDirectory")
File provideRxCacheDirectory(File cacheDir) {
    File cacheDirectory = new File(cacheDir, "RxCache");
    return DataHelper.makeDirs(cacheDirectory);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:ClientModule.java

示例8: providesGsonConverterFactory

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
public Converter.Factory providesGsonConverterFactory() {
    Gson gson = new GsonBuilder()
            .registerTypeAdapterFactory(AutoValuesGsonAdapterFactory.create())
            .create();
    return GsonConverterFactory.create(gson);
}
 
開發者ID:ecarrara-araujo,項目名稱:yabaking,代碼行數:9,代碼來源:NetworkingModule.java

示例9: dataSource

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
static DataSource dataSource(DatabaseConfig config) {
  HikariConfig hikari = new HikariConfig();
  hikari.setJdbcUrl(config.getJdbcUrl());
  hikari.setUsername(config.getUsername());
  hikari.setPassword(config.getPassword());
  hikari.addDataSourceProperty("cachePrepStmts", "true");
  hikari.addDataSourceProperty(
      "statementInterceptors", "brave.mysql.TracingStatementInterceptor");
  hikari.addDataSourceProperty("useUnicode", "yes");
  hikari.setMetricsTrackerFactory(new PrometheusMetricsTrackerFactory());
  return new HikariDataSource(hikari);
}
 
開發者ID:curioswitch,項目名稱:curiostack,代碼行數:15,代碼來源:DatabaseModule.java

示例10: providePoller

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
Poller providePoller(@IoExecutor Executor ioExecutor,
		@Scheduler ScheduledExecutorService scheduler,
		ConnectionManager connectionManager,
		ConnectionRegistry connectionRegistry, PluginManager pluginManager,
		SecureRandom random, Clock clock, EventBus eventBus) {
	Poller poller = new Poller(ioExecutor, scheduler, connectionManager,
			connectionRegistry, pluginManager, random, clock);
	eventBus.addListener(poller);
	return poller;
}
 
開發者ID:rafjordao,項目名稱:Nird2,代碼行數:13,代碼來源:PluginModule.java

示例11: client

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
OkHttpClient client() {
    return new OkHttpClient.Builder()
            .cache(new Cache(app.getCacheDir(), 1024L * 20))
            .followRedirects(true)
            .retryOnConnectionFailure(true)
            .build();
}
 
開發者ID:noties,項目名稱:Markwon,代碼行數:10,代碼來源:AppModule.java

示例12: provideServerAPI

import javax.inject.Singleton; //導入依賴的package包/類
@Singleton
@Provides
ServerAPI provideServerAPI() {
    return new RestAdapter.Builder()
        .setEndpoint(ServerAPI.ENDPOINT)
        .setLogLevel(RestAdapter.LogLevel.FULL)
        .setLog(new RestAdapter.Log() {
            @Override
            public void log(String message) {
                v("Retrofit", message);
            }
        })
        .build().create(ServerAPI.class);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:15,代碼來源:AppModule.java

示例13: proxyModule

import javax.inject.Singleton; //導入依賴的package包/類
public static <T> Module proxyModule(Class<T> proxyInterface) {

		return new AbstractModule() {
			@Override
			protected void configure() {
				Provider<Queries> provider = getProvider(Queries.class);

				Multibinder<QueriesSource> sourcesBinder = Multibinder.newSetBinder(binder(), QueriesSource.class);
				sourcesBinder.addBinding().toInstance(QueriesSource.ofClass(proxyInterface));

				bind(proxyInterface).toProvider(new ProxyInterfaceProvider<T>(provider, proxyInterface))
						.in(Singleton.class);
			}
		};
	}
 
開發者ID:jaregu,項目名稱:queries,代碼行數:16,代碼來源:QueriesGuiceSupport.java

示例14: provideOkHttpClient

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder()
            .connectTimeout(Config.CONNECT_TIMEOUT, SECONDS)
            .writeTimeout(Config.WRITE_TIMEOUT, SECONDS)
            .readTimeout(Config.READ_TIMEOUT, SECONDS);
    if (BuildConfig.DEBUG) {
        builder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
    }
    return builder.build();
}
 
開發者ID:VDenis,項目名稱:YandexTranslator,代碼行數:13,代碼來源:ModelModule.java

示例15: provideObjectMapper

import javax.inject.Singleton; //導入依賴的package包/類
@Provides
@Singleton
ObjectMapper provideObjectMapper() {
  ObjectMapper objectMapper = new ObjectMapper();

  objectMapper.setPropertyNamingStrategy(
      PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
  objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
  objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

  return objectMapper;
}
 
開發者ID:cerner,項目名稱:beadledom,代碼行數:14,代碼來源:FauxModule.java


注:本文中的javax.inject.Singleton類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。