方案一:
@Value("${spring.profiles.active}")private String profile;
方案二:
@Componentpublic class PrintSpringContext implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { PrintSpringContext.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { if (applicationContext == null) { throw new IllegalStateException("applicationContext not defined!"); } else { return applicationContext; } } public String getRunModel() { String[] profiles = applicationContext.getEnvironment().getActiveProfiles(); if(profiles.length > 0){ return profiles[0]; } return null; }}
调用测试:
@Componentpublic class PrintConverService { @Value("${spring.profiles.active}") private String profile; @Autowired private PrintSpringContext printSpringContext; public String findPrintDomainUrl(){ if(StringUtils.isEmpty(profile)){ profile = printSpringContext.getRunModel(); } if("develop".equalsIgnoreCase(profile)){ return ""; } else if("release".equalsIgnoreCase(profile)) { return ""; } return null; }}