This commit is contained in:
tanghc
2020-07-29 10:33:45 +08:00
parent 0fea955db9
commit 33e50a1fae
17 changed files with 75 additions and 868 deletions

View File

@@ -0,0 +1,43 @@
package com.gitee.app.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.web.context.support.StandardServletEnvironment;
import java.util.Map;
import java.util.Properties;
/**
* @author tanghc
*/
public class MyPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
super.postProcessBeanFactory(beanFactory);
if (environment instanceof StandardServletEnvironment) {
PropertySources appliedPropertySources = this.getAppliedPropertySources();
for (PropertySource<?> propertySource : appliedPropertySources) {
Object source = propertySource.getSource();
if (source instanceof Map) {
Map map = (Map)source;
map.forEach((key, value)-> {
System.setProperty(key.toString(), value.toString());
});
}
}
}
}
}

View File

@@ -10,6 +10,7 @@ import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscover
import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.configuration.ServiceConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
/**
* 使用支付宝开放平台功能
@@ -17,7 +18,7 @@ import lombok.extern.slf4j.Slf4j;
* @author tanghc
*/
@Slf4j
@EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
@EnableNacosDiscovery
public class OpenServiceConfig extends ServiceConfiguration {
@@ -30,10 +31,12 @@ public class OpenServiceConfig extends ServiceConfiguration {
ServiceConfig.getInstance().setDefaultVersion("1.0");
}
/** 对应tomcat中的contextPath */
private final String contextPath = "/sop-springmvc";
private final String serviceId = "sop-springmvc";
private final int port = 2223;
@Value("${spring.application.name}")
private String serviceId;
@Value("${server.port}")
private int port;
@Value("${server.servlet.context-path}")
private String contextPath;
@NacosInjected
private NamingService namingService;

View File

@@ -0,0 +1,5 @@
spring.application.name=sop-springmvc
server.port=2223
server.servlet.context-path=/sop-springmvc
nacos.server-addr=127.0.0.1:8848

View File

@@ -3,18 +3,23 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<bean class="com.gitee.app.config.MyPropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<!-- 只扫描Controller类 -->
<context:component-scan base-package="com.gitee.app" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

View File

@@ -2,21 +2,23 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.gitee.app.config.MyPropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- 排除Controller类 -->
<context:component-scan base-package="com.gitee.app">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
</beans>