非本地环境条件注解
This commit is contained in:
parent
7521095914
commit
a18d1b0c30
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>cn.axzo.framework</groupId>
|
<groupId>cn.axzo.framework</groupId>
|
||||||
<artifactId>common-common</artifactId>
|
<artifactId>common-common</artifactId>
|
||||||
<version>1.0.9</version>
|
<version>1.0.10</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|||||||
@ -11,6 +11,10 @@ public class Constants {
|
|||||||
|
|
||||||
/** 签名*/
|
/** 签名*/
|
||||||
public static final String SIGN = "sign";
|
public static final String SIGN = "sign";
|
||||||
|
|
||||||
|
/** 本地环境 */
|
||||||
|
public static final String LOCAL = "local";
|
||||||
|
|
||||||
/** 签名方式*/
|
/** 签名方式*/
|
||||||
public static final String SIGN_TYPE = "sign_type";
|
public static final String SIGN_TYPE = "sign_type";
|
||||||
/** 签名版本号*/
|
/** 签名版本号*/
|
||||||
|
|||||||
@ -0,0 +1,25 @@
|
|||||||
|
package cn.azxo.framework.common.spring.condition;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Conditional;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 非本地环境
|
||||||
|
*
|
||||||
|
* @author zhaoyong
|
||||||
|
* @see NonLocalEnvironment
|
||||||
|
* @since 2021-08-10 13:21
|
||||||
|
*/
|
||||||
|
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@Conditional(NonLocalEnvironmentCondition.class)
|
||||||
|
public @interface NonLocalEnvironment {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
package cn.azxo.framework.common.spring.condition;
|
||||||
|
|
||||||
|
import cn.azxo.framework.common.constatns.Constants;
|
||||||
|
import org.springframework.context.annotation.Condition;
|
||||||
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 非本地环境条件
|
||||||
|
*
|
||||||
|
* @author zhaoyong
|
||||||
|
* @see NonLocalEnvironmentCondition
|
||||||
|
* @since 2021-08-10 11:57
|
||||||
|
*/
|
||||||
|
public class NonLocalEnvironmentCondition implements Condition {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||||
|
if(isLocal(context)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否本地环境
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean isLocal(ConditionContext context){
|
||||||
|
Environment environment = context.getEnvironment();
|
||||||
|
String[] activeProfiles = environment.getActiveProfiles();
|
||||||
|
for (String activeProfile : activeProfiles) {
|
||||||
|
if(Constants.LOCAL.equals(activeProfile)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user