org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication Java Examples

The following examples show how to use org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebHasorConfiguration.java    From hasor with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: OghamThymeleafV2Configuration.java    From ogham with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: OghamThymeleafV3Configuration.java    From ogham with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: WebConfig.java    From platform with Apache License 2.0 5 votes vote down vote up
/**
 * StatViewServlet
 *
 * @return {@link StatViewServlet}
 */
@Bean
@ConditionalOnWebApplication
public ServletRegistrationBean<StatViewServlet> statViewServlet() {
    ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>();
    bean.setServlet(new StatViewServlet());
    bean.addUrlMappings("/druid/*");
    return bean;
}
 
Example #5
Source File: WebConfig.java    From platform with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: AutoloadCacheAutoConfigure.java    From AutoLoadCache with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #7
Source File: BasicHasorConfiguration.java    From hasor with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: ErrorsAutoConfiguration.java    From errors-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #9
Source File: K8sMetricsAutoConfiguration.java    From foremast with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public WebFluxTagsProvider serviceFluxCallerTag() {
    if (metricsProperties.hasCaller()) {
        return new CallerWebFluxTagsProvider(metricsProperties.getCallerHeader());
    }
    else {
        return new DefaultWebFluxTagsProvider();
    }
}
 
Example #10
Source File: K8sMetricsAutoConfiguration.java    From foremast with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public WebMvcTagsProvider serviceCallerTag() {
    if (metricsProperties.hasCaller()) {
        return new CallerWebMvcTagsProvider(metricsProperties.getCallerHeader());
    }
    else {
        return new DefaultWebMvcTagsProvider();
    }
}
 
Example #11
Source File: XsuaaResourceServerJwkAutoConfiguration.java    From cloud-security-xsuaa-integration with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #12
Source File: SpringDocConfiguration.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #13
Source File: WingtipsSpringBootConfiguration.java    From wingtips with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example #14
Source File: WingtipsSpringBoot2WebfluxConfiguration.java    From wingtips with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example #15
Source File: OghamThymeleafV3Configuration.java    From ogham with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnWebApplication
public WebContextProvider webContextProvider() {
	return new RequestContextHolderWebContextProvider();
}
 
Example #16
Source File: OghamThymeleafV2Configuration.java    From ogham with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnWebApplication
public WebContextProvider webContextProvider() {
	return new RequestContextHolderWebContextProvider();
}
 
Example #17
Source File: CompatibleSpringBoot1AutoConfiguration.java    From sofa-ark with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnBean(IntrospectBizEndpoint.class)
@ConditionalOnWebApplication
public IntrospectBizEndpointMvcAdapter introspectBizEndpointMvcAdapter(IntrospectBizEndpoint introspectBizEndpoint) {
    return new IntrospectBizEndpointMvcAdapter(introspectBizEndpoint);
}
 
Example #18
Source File: VelocityToolsAutoConfiguration.java    From velocity-spring-boot-project with Apache License 2.0 4 votes vote down vote up
@ConditionalOnBean({VelocityToolsRepository.class})
@ConditionalOnWebApplication
@Bean
public ApplicationListener<ContextRefreshedEvent> embeddedVelocityViewResolverApplicationListener() {
    return new EmbeddedVelocityViewResolverApplicationListener();
}
 
Example #19
Source File: AutoloadCacheAutoConfigure.java    From AutoLoadCache with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnWebApplication
@ConditionalOnMissingBean(AutoloadCacheController.class)
public AutoloadCacheController AutoloadCacheController(CacheHandler autoloadCacheHandler) {
    return new AutoloadCacheController(autoloadCacheHandler);
}
 
Example #20
Source File: WebApplicationSpecificConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@ConditionalOnWebApplication
HealthCheckController healthCheckController() {
    return new HealthCheckController();
}
 
Example #21
Source File: SpringDocConfiguration.java    From springdoc-openapi with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}