springboot-2.1.X
tanghc 4 years ago
parent c0e165f59b
commit 3d68ea80cd
  1. 17
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/AlipayServiceConfiguration.java
  2. 23
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringmvcConfiguration.java
  3. 5
      sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java
  4. 7
      sop-example/sop-springmvc/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

@ -1,26 +1,9 @@
package com.gitee.sop.servercommon.configuration; package com.gitee.sop.servercommon.configuration;
import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.param.SopHandlerMethodArgumentResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
/** /**
* 具备支付宝开放平台服务提供能力 * 具备支付宝开放平台服务提供能力
* @author tanghc * @author tanghc
*/ */
public class AlipayServiceConfiguration extends BaseServiceConfiguration { public class AlipayServiceConfiguration extends BaseServiceConfiguration {
static {
// 默认版本号为1.0
ServiceConfig.getInstance().setDefaultVersion("1.0");
}
@Override
protected void doAfter() {
super.doAfter();
RequestMappingHandlerAdapter requestMappingHandlerAdapter = getApplicationContext().getBean(RequestMappingHandlerAdapter.class);
SopHandlerMethodArgumentResolver sopHandlerMethodArgumentResolver = ServiceConfig.getInstance().getMethodArgumentResolver();
sopHandlerMethodArgumentResolver.setRequestMappingHandlerAdapter(requestMappingHandlerAdapter);
}
} }

@ -3,18 +3,19 @@ package com.gitee.sop.servercommon.configuration;
import com.gitee.sop.servercommon.bean.ServiceConfig; import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.interceptor.ServiceContextInterceptor; import com.gitee.sop.servercommon.interceptor.ServiceContextInterceptor;
import com.gitee.sop.servercommon.message.ServiceErrorFactory; import com.gitee.sop.servercommon.message.ServiceErrorFactory;
import com.gitee.sop.servercommon.param.SopHandlerMethodArgumentResolver;
import com.gitee.sop.servercommon.route.ServiceRouteController; import com.gitee.sop.servercommon.route.ServiceRouteController;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -24,19 +25,26 @@ import java.util.List;
* @author tanghc * @author tanghc
*/ */
@Slf4j @Slf4j
public class SpringmvcConfiguration implements WebMvcConfigurer, ApplicationContextAware { public class SpringmvcConfiguration implements WebMvcConfigurer, BeanPostProcessor {
public static final String METADATA_SERVER_CONTEXT_PATH = "server.servlet.context-path"; public static final String METADATA_SERVER_CONTEXT_PATH = "server.servlet.context-path";
private ApplicationContext applicationContext; static {
ServiceConfig.getInstance().setDefaultVersion("1.0");
}
public SpringmvcConfiguration() { public SpringmvcConfiguration() {
ServiceConfig.getInstance().getI18nModules().add("i18n/isp/bizerror"); ServiceConfig.getInstance().getI18nModules().add("i18n/isp/bizerror");
} }
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
this.applicationContext = applicationContext; if (bean instanceof RequestMappingHandlerAdapter) {
RequestMappingHandlerAdapter requestMappingHandlerAdapter = (RequestMappingHandlerAdapter) bean;
SopHandlerMethodArgumentResolver sopHandlerMethodArgumentResolver = ServiceConfig.getInstance().getMethodArgumentResolver();
sopHandlerMethodArgumentResolver.setRequestMappingHandlerAdapter(requestMappingHandlerAdapter);
}
return bean;
} }
@Override @Override
@ -96,7 +104,4 @@ public class SpringmvcConfiguration implements WebMvcConfigurer, ApplicationCont
ServiceErrorFactory.initMessageSource(ServiceConfig.getInstance().getI18nModules()); ServiceErrorFactory.initMessageSource(ServiceConfig.getInstance().getI18nModules());
} }
public ApplicationContext getApplicationContext() {
return applicationContext;
}
} }

@ -6,7 +6,6 @@ import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.utils.NetUtils; import com.alibaba.nacos.client.naming.utils.NetUtils;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery; import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.configuration.SpringmvcConfiguration; import com.gitee.sop.servercommon.configuration.SpringmvcConfiguration;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -27,10 +26,6 @@ public class OpenServiceConfig extends SpringmvcConfiguration {
public static final String SERVER_PORT = "server.port"; public static final String SERVER_PORT = "server.port";
public static final String METADATA_TIME_STARTUP = "server.startup-time"; public static final String METADATA_TIME_STARTUP = "server.startup-time";
static {
ServiceConfig.getInstance().setDefaultVersion("1.0");
}
@Value("${spring.application.name}") @Value("${spring.application.name}")
private String serviceId; private String serviceId;
@Value("${server.port}") @Value("${server.port}")

@ -31,12 +31,7 @@
<property name="suffix" value=".jsp" /> <property name="suffix" value=".jsp" />
</bean> </bean>
<!-- 接入SOP配置 --> <mvc:annotation-driven />
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="com.gitee.sop.servercommon.param.ApiArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
<bean class="com.gitee.app.config.OpenServiceConfig"/> <bean class="com.gitee.app.config.OpenServiceConfig"/>

Loading…
Cancel
Save