All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.rxjava.configuration.RxJavaMvcAutoConfiguration Maven / Gradle / Ivy

/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.rxjava.configuration;

import com.taotao.boot.common.constant.StarterName;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.rxjava.annotation.RxJava;
import com.taotao.boot.rxjava.mvc.ObservableReturnValueHandler;
import com.taotao.boot.rxjava.mvc.SingleReturnValueHandler;
import com.taotao.boot.rxjava.properties.RxJavaProperties;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Single;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * RxJavaMvcAutoConfiguration
 *
 * @author shuigedeng
 * @version 2021.9
 * @since 2021-09-07 20:49:34
 */
@AutoConfiguration
@EnableConfigurationProperties({RxJavaProperties.class})
@ConditionalOnProperty(prefix = RxJavaProperties.PREFIX, name = "enabled", havingValue = "true")
public class RxJavaMvcAutoConfiguration implements InitializingBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		LogUtils.started(RxJavaMvcAutoConfiguration.class, StarterName.RXJAVA_STARTER);
	}

	@Bean
	@RxJava
	@ConditionalOnMissingBean
	@ConditionalOnClass(Observable.class)
	public ObservableReturnValueHandler observableReturnValueHandler() {
		LogUtils.started(ObservableReturnValueHandler.class, StarterName.RXJAVA_STARTER);
		return new ObservableReturnValueHandler();
	}

	@Bean
	@RxJava
	@ConditionalOnMissingBean
	@ConditionalOnClass(Single.class)
	public SingleReturnValueHandler singleReturnValueHandler() {
		LogUtils.started(SingleReturnValueHandler.class, StarterName.RXJAVA_STARTER);
		return new SingleReturnValueHandler();
	}

	@Configuration
	@ConditionalOnProperty(prefix = RxJavaProperties.PREFIX
		+ ".mvc", name = "enabled", havingValue = "true")
	public static class RxJavaWebConfiguration implements WebMvcConfigurer {

		private final  List handlers;

		public RxJavaWebConfiguration(List handlers) {
			this.handlers = handlers;
		}

		@Override
		public void addReturnValueHandlers(
			List returnValueHandlers) {
			if (handlers != null) {
				returnValueHandlers.addAll(handlers);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy