mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
add spring enviroment extension module
This commit is contained in:
parent
90b21480d7
commit
c7663abffe
35
spring-cloud-alibaba-examples/env-extension/pom.xml
Normal file
35
spring-cloud-alibaba-examples/env-extension/pom.xml
Normal file
@ -0,0 +1,35 @@
|
||||
<?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.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba</artifactId>
|
||||
<version>0.1.2.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>alibaba.com</groupId>
|
||||
<artifactId>env-extension</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>env-extension</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,11 @@
|
||||
package org.springframework.alicloud.env.extension;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ImportExtraConfig {
|
||||
|
||||
String[] name() default "";
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 7:39 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
org.springframework.context.ApplicationListener=\
|
||||
org.springframework.alicloud.env.extension.LoadExtraConfigApplicationListener
|
@ -28,6 +28,7 @@
|
||||
<module>ans-example/ans-provider-example</module>
|
||||
<module>acm-example/acm-local-example</module>
|
||||
<module>rocketmq-example</module>
|
||||
<module>env-extension</module>
|
||||
<module>sms-example</module>
|
||||
<module>spring-cloud-bus-rocketmq-example</module>
|
||||
<module>schedulerx-example/schedulerx-simple-task-example</module>
|
||||
|
@ -31,6 +31,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.1.2.BUILD-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
|
@ -1,13 +1,48 @@
|
||||
package org.springframework.cloud.alibaba.cloud.example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SmsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(SmsApplication.class, args);
|
||||
}
|
||||
}
|
||||
//package org.springframework.cloud.alibaba.cloud.example;
|
||||
//
|
||||
//import org.springframework.alicloud.env.extension.ImportExtraConfig;
|
||||
//import org.springframework.boot.SpringApplication;
|
||||
//import org.springframework.boot.SpringApplicationRunListener;
|
||||
//import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
//import org.springframework.context.ConfigurableApplicationContext;
|
||||
//import org.springframework.core.env.ConfigurableEnvironment;
|
||||
//
|
||||
//@SpringBootApplication
|
||||
//@ImportExtraConfig(name = "/Users/toava/sms.properties")
|
||||
//public class SmsApplication {
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
//
|
||||
// SpringApplication.run(SmsApplication.class, args);
|
||||
// }
|
||||
//
|
||||
// public class EvnExtra implements SpringApplicationRunListener {
|
||||
// @Override
|
||||
// public void starting() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void environmentPrepared(ConfigurableEnvironment environment) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void contextPrepared(ConfigurableApplicationContext context) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void contextLoaded(ConfigurableApplicationContext context) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void finished(ConfigurableApplicationContext context,
|
||||
// Throwable exception) {
|
||||
// String ak = context.getEnvironment()
|
||||
// .getProperty("spring.cloud.alicloud.access-key");
|
||||
// System.err.println(ak);
|
||||
// }
|
||||
// }
|
||||
//}
|
@ -0,0 +1,30 @@
|
||||
package org.springframework.cloud.alibaba.cloud.example.env;
|
||||
|
||||
import org.springframework.alicloud.env.extension.ImportExtraConfig;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
@ImportExtraConfig(name = "/Users/toava/sms.properties")
|
||||
public class SmsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(SmsApplication.class, args);
|
||||
}
|
||||
|
||||
@RestController
|
||||
public class EnvExtraController{
|
||||
|
||||
@Value("${spring.cloud.alicloud.access-key:deshao}")
|
||||
private String ak;
|
||||
|
||||
@GetMapping("/get-ak.do")
|
||||
public String getAk(){
|
||||
return ak;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user