本文整理汇总了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;
}
}
};
}
示例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;
}
示例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();
}
示例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");
}
示例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;
}
示例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();
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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();
}
示例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);
}
示例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);
}
};
}
示例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();
}
示例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;
}