1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

add spring enviroment extension module for examples

This commit is contained in:
得少 2019-01-09 23:02:04 +08:00
parent ee2c696bdf
commit a4f8cc7d28
9 changed files with 133 additions and 8 deletions

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>alibaba.com</groupId>
<artifactId>env-extension</artifactId>
<version>0.2.2.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>env-extension</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,12 @@
package org.springframework.alicloud.env.extension;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ImportExtraConfig {
String[] name() default "";
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2019 the original author or authors.
*
* 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
*
* http://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 org.springframework.alicloud.env.extension;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.PropertiesPropertySource;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* @author pbting
* @date 2019-01-09 9:00 PM
*/
public class LoadExtraConfigApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
SpringApplication springApplication = event.getSpringApplication();
Class clazz = springApplication.getMainApplicationClass();
if (!clazz.isAnnotationPresent(ImportExtraConfig.class)) {
return;
}
ImportExtraConfig annotation = (ImportExtraConfig) clazz
.getAnnotation(ImportExtraConfig.class);
String[] extraConfig = annotation.name();
if (extraConfig == null || extraConfig.length == 0) {
return;
}
for (String config : extraConfig) {
try {
Properties properties = new Properties();
properties.load(new FileInputStream(config));
event.getEnvironment().getPropertySources()
.addFirst(new PropertiesPropertySource(config, properties));
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,2 @@
org.springframework.context.ApplicationListener=\
org.springframework.alicloud.env.extension.LoadExtraConfigApplicationListener

View File

@ -23,6 +23,7 @@
<module>sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api</module>
<module>nacos-example/nacos-discovery-example</module>
<module>nacos-example/nacos-config-example</module>
<module>env-extension</module>
<module>oss-example</module>
<module>ans-example/ans-consumer-feign-example</module>
<module>ans-example/ans-consumer-ribbon-example</module>

View File

@ -59,6 +59,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-sms</artifactId>
</dependency>
<dependency>
<groupId>alibaba.com</groupId>
<artifactId>env-extension</artifactId>
<version>0.2.2.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
<build>

View File

@ -15,6 +15,7 @@
*/
package org.springframework.cloud.alibaba.cloud.example;
import org.springframework.alicloud.env.extension.ImportExtraConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -22,10 +23,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*
*/
@SpringBootApplication
@ImportExtraConfig(name = "/Users/toava/sms.properties")
public class SmsApplication {
public static void main(String[] args) {
public static void main(String[] args) throws Exception{
SpringApplication.run(SmsApplication.class, args);
}
}

View File

@ -4,6 +4,8 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alicloud.sms.ISmsService;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@ -16,12 +18,21 @@ import com.aliyuncs.http.MethodType;
@RestController
public class SmsController {
@Autowired
private Environment environment;
@Autowired
private ISmsService smsService;
@Autowired
private SmsReportMessageListener smsReportMessageListener;
@GetMapping("/report-queue.do")
public String getSmsReportQueuename(){
return environment.getProperty("spring.cloud.alicloud.sms.up-queue-name");
}
/**
* 短信发送 Example
* @param code

View File

@ -2,8 +2,3 @@ spring.application.name=sca-sms-example
server.port=9051
# config management
management.endpoints.web.exposure.include=*
#config sms
spring.cloud.alicloud.access-key=******
spring.cloud.alicloud.secret-key=******
spring.cloud.alicloud.sms.report-queue-name=*****
spring.cloud.alicloud.sms.up-queue-name=******