下面列出了怎么用org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication的API类实例代码及写法,或者点击链接到github查看源代码。
@Bean
@ConditionalOnMissingBean(ThymeleafContextConverter.class)
@ConditionalOnWebApplication
public ThymeleafContextConverter springWebThymeleafContextConverter(
StaticVariablesProvider staticVariablesProvider,
ThymeleafEvaluationContextProvider thymeleafEvaluationContextProvider,
ContextMerger contextMerger,
ApplicationContext applicationContext,
WebContextProvider webContextProvider,
@Autowired(required=false) org.thymeleaf.spring5.SpringTemplateEngine springTemplateEngine) {
return new SpringWebThymeleafContextConverter(
springThymeleafContextConverter(staticVariablesProvider, thymeleafEvaluationContextProvider, contextMerger),
SpringContextVariableNames.SPRING_REQUEST_CONTEXT,
applicationContext,
webContextProvider,
new SpringWebMvcThymeleafRequestContextWrapper(),
new WebExpressionContextProvider(springTemplateEngine),
contextMerger);
}
@Bean
@ConditionalOnMissingBean(ThymeleafContextConverter.class)
@ConditionalOnWebApplication
public ThymeleafContextConverter springWebThymeleafContextConverter(
StaticVariablesProvider staticVariablesProvider,
ThymeleafEvaluationContextProvider thymeleafEvaluationContextProvider,
ContextMerger contextMerger,
ApplicationContext applicationContext,
WebContextProvider webContextProvider) {
return new SpringWebThymeleafContextConverter(
springThymeleafContextConverter(staticVariablesProvider, thymeleafEvaluationContextProvider, contextMerger),
SpringContextVariableNames.SPRING_REQUEST_CONTEXT,
applicationContext,
webContextProvider,
new NoOpRequestContextWrapper(),
new SpringWebContextProvider(),
contextMerger);
}
@Bean
@ConditionalOnWebApplication
@ConditionalOnClass(name = "net.hasor.web.startup.RuntimeFilter")
public FilterRegistrationBean<?> hasorRuntimeFilter() {
Objects.requireNonNull(this.appContext, "AppContext is not inject.");
Filter runtimeFilter = null;
if (this.filterWorkAt == WorkAt.Filter) {
runtimeFilter = new RuntimeFilter(this.appContext); // 过滤器模式
} else {
runtimeFilter = new EmptyFilter(); // 拦截器模式
}
//
FilterRegistrationBean<Filter> filterBean = //
new FilterRegistrationBean<>(runtimeFilter);
filterBean.setUrlPatterns(Collections.singletonList(this.filterPath));
filterBean.setOrder(this.filterOrder);
filterBean.setName(RuntimeFilter.class.getName());
return filterBean;
}
/**
* Request body builder request body builder.
*
* @param parameterBuilder the parameter builder
* @return the request body builder
*/
@Bean
@ConditionalOnWebApplication
@ConditionalOnMissingBean
RequestBodyBuilder requestBodyBuilder(GenericParameterBuilder parameterBuilder) {
return new RequestBodyBuilder(parameterBuilder);
}
@Bean
@ConditionalOnBean({ XsuaaServiceConfiguration.class, RestOperations.class })
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnMissingBean
public JwtDecoder xsuaaJwtDecoder(XsuaaServiceConfiguration xsuaaServiceConfiguration,
RestOperations xsuaaRestOperations) {
logger.debug("auto-configures JwtDecoder using restOperations of type: {}", xsuaaRestOperations);
return new XsuaaJwtDecoderBuilder(xsuaaServiceConfiguration)
.withRestOperations(xsuaaRestOperations)
.build();
}
@Bean
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public WebMvcTagsProvider serviceCallerTag() {
if (metricsProperties.hasCaller()) {
return new CallerWebMvcTagsProvider(metricsProperties.getCallerHeader());
}
else {
return new DefaultWebMvcTagsProvider();
}
}
@Bean
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public WebFluxTagsProvider serviceFluxCallerTag() {
if (metricsProperties.hasCaller()) {
return new CallerWebFluxTagsProvider(metricsProperties.getCallerHeader());
}
else {
return new DefaultWebFluxTagsProvider();
}
}
/**
* Registers a {@link WebErrorHandler} to handle new Servlet exceptions defined in Spring Framework 5.1.
* This handler would be registered iff we're using Spring Boot 2.1.0+.
*
* @return A web error handler to handle a few new servlet exceptions.
*/
@Bean
@ConditionalOnBean(WebErrorHandlers.class)
@ConditionalOnWebApplication(type = SERVLET)
@ConditionalOnClass(name = "org.springframework.web.bind.MissingRequestHeaderException")
public MissingRequestParametersWebErrorHandler missingRequestParametersWebErrorHandler() {
return new MissingRequestParametersWebErrorHandler();
}
@Bean
@ConditionalOnWebApplication
public AppContext webAppContext(ApplicationContext applicationContext) {
ServletContext parent = null;
if (applicationContext instanceof WebApplicationContext) {
parent = ((WebApplicationContext) applicationContext).getServletContext();
} else {
throw new IllegalStateException("miss ServletContext.");
}
return this.createAppContext(parent, applicationContext);
}
@Bean
@ConditionalOnWebApplication
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
HTTPBasicAuthorizeAttribute httpBasicFilter = new HTTPBasicAuthorizeAttribute(config);
registrationBean.setFilter(httpBasicFilter);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/autoload-cache-ui.html");
urlPatterns.add("/autoload-cache/*");
registrationBean.setUrlPatterns(urlPatterns);
return registrationBean;
}
/**
* WebStatFilter
*
* @return {@link WebStatFilter}
*/
@Bean
@ConditionalOnWebApplication
public FilterRegistrationBean<WebStatFilter> druidStatFilter() {
FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(new WebStatFilter());
bean.addUrlPatterns("/*");
bean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/static/*,/resources/*,/mobile/*,/app-update/*");
return bean;
}
/**
* StatViewServlet
*
* @return {@link StatViewServlet}
*/
@Bean
@ConditionalOnWebApplication
public ServletRegistrationBean<StatViewServlet> statViewServlet() {
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>();
bean.setServlet(new StatViewServlet());
bean.addUrlMappings("/druid/*");
return bean;
}
@ConditionalOnBean({VelocityToolsRepository.class})
@ConditionalOnWebApplication
@Bean
public ApplicationListener<ContextRefreshedEvent> embeddedVelocityViewResolverApplicationListener() {
return new EmbeddedVelocityViewResolverApplicationListener();
}
@Bean
@ConditionalOnBean(IntrospectBizEndpoint.class)
@ConditionalOnWebApplication
public IntrospectBizEndpointMvcAdapter introspectBizEndpointMvcAdapter(IntrospectBizEndpoint introspectBizEndpoint) {
return new IntrospectBizEndpointMvcAdapter(introspectBizEndpoint);
}
/**
* Create and return a {@link RequestTracingFilter}, which will auto-register itself with the Spring Boot app as
* a servlet filter and enable Wingtips tracing for incoming requests.
*
* @return The {@link RequestTracingFilter} that should be used.
*/
@Bean
@ConditionalOnWebApplication
public FilterRegistrationBean wingtipsRequestTracingFilter() {
if (wingtipsProperties.isWingtipsDisabled()) {
// We can't return null or create a FilterRegistrationBean that has a null filter inside as it will result
// in a NullPointerException. So instead we'll return a do-nothing servlet filter.
return new FilterRegistrationBean(new DoNothingServletFilter());
}
// Allow projects to completely override the filter that gets used if desired. If not overridden then create
// a new one.
if (requestTracingFilter == null) {
requestTracingFilter = new RequestTracingFilter();
}
FilterRegistrationBean frb = new FilterRegistrationBean(requestTracingFilter);
// Add the user ID header keys init param if specified in the wingtips properties.
if (wingtipsProperties.getUserIdHeaderKeys() != null) {
frb.addInitParameter(
RequestTracingFilter.USER_ID_HEADER_KEYS_LIST_INIT_PARAM_NAME,
wingtipsProperties.getUserIdHeaderKeys()
);
}
// Add the tagging strategy init param if specified in the wingtips properties.
if (wingtipsProperties.getServerSideSpanTaggingStrategy() != null) {
frb.addInitParameter(
RequestTracingFilter.TAG_AND_SPAN_NAMING_STRATEGY_INIT_PARAM_NAME,
wingtipsProperties.getServerSideSpanTaggingStrategy()
);
}
// Add the tagging adapter init param if specified in the wingtips properties.
if (wingtipsProperties.getServerSideSpanTaggingAdapter() != null) {
frb.addInitParameter(
RequestTracingFilter.TAG_AND_SPAN_NAMING_ADAPTER_INIT_PARAM_NAME,
wingtipsProperties.getServerSideSpanTaggingAdapter()
);
}
// Set the order so that the tracing filter is registered first
frb.setOrder(Ordered.HIGHEST_PRECEDENCE);
return frb;
}
/**
* Create and return a {@link WingtipsSpringWebfluxWebFilter}, which will auto-register itself with the
* Spring Boot 2 WebFlux app as a {@link org.springframework.web.server.WebFilter} and enable Wingtips tracing
* for incoming requests.
*
* <p>NOTE: This will return null (and essentially be a no-op for Spring) in the following cases:
* <ul>
* <li>
* When {@link WingtipsSpringBoot2WebfluxProperties#isWingtipsDisabled()} is true. In this case the
* application specifically does *not* want a {@link WingtipsSpringWebfluxWebFilter} registered.
* </li>
* <li>
* When {@link #customSpringWebfluxWebFilter} is non-null. In this case the application has a custom
* implementation of {@link WingtipsSpringWebfluxWebFilter} that they want to use instead of whatever
* default one this method would provide, so we should return null here to allow the custom application
* impl to be used.
* </li>
* </ul>
*
* @return The {@link WingtipsSpringWebfluxWebFilter} that should be used, or null in the case that
* {@link WingtipsSpringBoot2WebfluxProperties#isWingtipsDisabled()} is true or if the application already
* defined a custom override.
*/
@Bean
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public WingtipsSpringWebfluxWebFilter wingtipsSpringWebfluxWebFilter() {
if (wingtipsProperties.isWingtipsDisabled()) {
return null;
}
// Allow projects to completely override the filter that gets used if desired. If not overridden then create
// a new one.
if (customSpringWebfluxWebFilter != null) {
return null;
}
// We need to create a new one.
return WingtipsSpringWebfluxWebFilter
.newBuilder()
.withUserIdHeaderKeys(extractUserIdHeaderKeysAsList(wingtipsProperties))
.withTagAndNamingStrategy(extractTagAndNamingStrategy(wingtipsProperties))
.withTagAndNamingAdapter(extractTagAndNamingAdapter(wingtipsProperties))
.build();
}
@Bean
@ConditionalOnWebApplication
public WebContextProvider webContextProvider() {
return new RequestContextHolderWebContextProvider();
}
@Bean
@ConditionalOnWebApplication
public WebContextProvider webContextProvider() {
return new RequestContextHolderWebContextProvider();
}
@Bean
@ConditionalOnWebApplication
@ConditionalOnMissingBean(AutoloadCacheController.class)
public AutoloadCacheController AutoloadCacheController(CacheHandler autoloadCacheHandler) {
return new AutoloadCacheController(autoloadCacheHandler);
}
@ConditionalOnWebApplication
HealthCheckController healthCheckController() {
return new HealthCheckController();
}
/**
* Operation builder operation builder.
*
* @param parameterBuilder the parameter builder
* @param requestBodyBuilder the request body builder
* @param securityParser the security parser
* @param propertyResolverUtils the property resolver utils
* @return the operation builder
*/
@Bean
@ConditionalOnWebApplication
@ConditionalOnMissingBean
OperationBuilder operationBuilder(GenericParameterBuilder parameterBuilder, RequestBodyBuilder requestBodyBuilder,
SecurityParser securityParser, PropertyResolverUtils propertyResolverUtils) {
return new OperationBuilder(parameterBuilder, requestBodyBuilder,
securityParser, propertyResolverUtils);
}