Skip to content

Instantly share code, notes, and snippets.

@chenminhua
Last active May 9, 2018 09:57
Show Gist options
  • Save chenminhua/c5da2e81dcc63a88018fbdb52db6d7e6 to your computer and use it in GitHub Desktop.
Save chenminhua/c5da2e81dcc63a88018fbdb52db6d7e6 to your computer and use it in GitHub Desktop.
Spring Auto Configure
@Configuration
public class PrometheusAutoConfigure {
@Bean
SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) {
SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(publicMetrics);
springBootMetricsCollector.register();
return springBootMetricsCollector;
}
@Bean
ServletRegistrationBean servletRegistrationBean() {
DefaultExports.initialize();
return new ServletRegistrationBean(new MetricsServlet(), "/prometheus");
}
}
import io.sentry.spring.SentryExceptionResolver
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
@ConfigurationProperties("sentry")
@ConditionalOnProperty("sentry.dsn")
open class SentryAutoConfigure() {
@Value("\${sentry.dsn}")
lateinit var value: String
@Bean @ConditionalOnMissingBean
open fun sentryExceptionResolver(): SentryExceptionResolver {
io.sentry.Sentry.init(value)
return io.sentry.spring.SentryExceptionResolver()
}
@Bean @ConditionalOnMissingBean
open fun sentryServletContextInitializer() = io.sentry.spring.SentryServletContextInitializer()
}
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(BarrenProperties::class)
open class SwaggerAutoConfigure {
@Autowired
lateinit var barrenProperties: BarrenProperties
@Bean
open fun api(): Docket {
return Docket(DocumentationType.SWAGGER_2)
.groupName("service")
.apiInfo(info())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/[vV][0-9]+/.*"))
.build()
}
@Bean
open fun all(): Docket {
return Docket(DocumentationType.SWAGGER_2)
.groupName("all")
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
}
open fun info(): ApiInfo {
return ApiInfoBuilder()
.title(barrenProperties.appName)
.version(barrenProperties.version)
.build()
}
}
@Configuration
@ConditionalOnClass(UndertowEmbeddedServletContainerFactory::class)
open class UndertowAutoConfiguration {
@Bean @ConditionalOnMissingBean
open fun embeddedServletContainerFactory(): UndertowEmbeddedServletContainerFactory {
val factory = UndertowEmbeddedServletContainerFactory()
factory.addBuilderCustomizers(
UndertowBuilderCustomizer {
it.setServerOption(UndertowOptions.ENABLE_HTTP2, true)
}
)
return factory
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment