第一種方法:
yml 文件:
spring:
main:
allow-bean-definition-overriding: true
cloud:
consul:
host: 192.168.1.1
port: 8500
discovery:
enabled: true
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
#健康檢查失敗后10秒會注銷該服務
health-check-critical-timeout: 10s
config:
fail-fast: true
redis:
open: true # 是否開啟redis緩存 true開啟 false關閉
database: 0
host: 192.168.1.1
port: 6379
password: aaa123 # 密碼(默認為空)
timeout: 6000ms # 連接超時時長(毫秒)
jedis:
pool:
max-active: 1000 # 連接池最大連接數(使用負值表示沒有限制)
max-wait: -1ms # 連接池最大阻塞等待時間(使用負值表示沒有限制)
max-idle: 20 # 連接池中的最大空閑連接
min-idle: 5 # 連接池中的最小空閑連接
2: 使用:
@Aspect
@Configuration
public class RedisAspect {
private Logger logger = LoggerFactory.getLogger(getClass());
//是否開啟redis緩存 true開啟 false關閉
@Value("${spring.redis.open: false}")
private boolean open;
@Around("execution(* com.zhangtao.common.utils.RedisUtils.*(..))")
public Object around(ProceedingJoinPoint point) throws Throwable {
Object result = null;
if(open){
try{
result = point.proceed();
}catch (Exception e){
logger.error("redis error", e);
throw new RRException("Redis服務異常");
}
}
return result;
}
}
第二種方法:
1: 在yml 中設置參數:
umeng:
push:
androidAppKey: 5580e58867e58e44f2000000
androidAppMasterSecret: iyqviswd8fynx5snbpga9og9vg000000
iosAppKey: 55b9e72c67e58e1cbc000000
iosAppMasterSecret: liubgwrxmtyh83n03muqkwgvu000000
productionMode: false
2:新建class 文件:
@Configuration @ConfigurationProperties(prefix = "umeng.push") public class UmengConfig { private String androidAppKey; private String androidAppMasterSecret; private String iosAppKey; private String iosAppMasterSecret; private boolean productionMode; private String aliasType; public String getAndroidAppKey() { return androidAppKey; } public void setAndroidAppKey(String androidAppKey) { this.androidAppKey = androidAppKey; } public String getAndroidAppMasterSecret() { return androidAppMasterSecret; } public void setAndroidAppMasterSecret(String androidAppMasterSecret) { this.androidAppMasterSecret = androidAppMasterSecret; } public String getIosAppKey() { return iosAppKey; } public void setIosAppKey(String iosAppKey) { this.iosAppKey = iosAppKey; } public String getIosAppMasterSecret() { return iosAppMasterSecret; } public void setIosAppMasterSecret(String iosAppMasterSecret) { this.iosAppMasterSecret = iosAppMasterSecret; } public boolean isProductionMode() { return productionMode; } public void setProductionMode(boolean productionMode) { this.productionMode = productionMode; } public String getAliasType() { return aliasType; } public void setAliasType(String aliasType) { this.aliasType = aliasType; } }
3: 使用yml 中的參數:
在另外的class 中:
先注入:
@Autowired
private UmengConfig umengProperty;
即可在方法中使用:
IOSUnicast unicast = new IOSUnicast(umengProperty.getIosAppKey(), umengProperty.getIosAppMasterSecret());