Spring中获取profile三种方式
方式一:
通过@Profile获取profile并控制方法的访问权限
class Test1 {
@Profile("dev")
public void profile() {
System.out.println("dev");
}
}
或者配置多个:
class Test1 {
@Profile({"dev", "qa"})
public void profile() {
System.out.println("dev");
}
}
方式二:
获取profile的工具类,可以获取到具体的profile
@Component
public final class ProfileUtils implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static String getActiveProfile() {
String[] profiles = context.getEnvironment().getActiveProfiles();
if (profiles.length != 0) {
return profiles[0];
}
return "";
}
}
方式三:
class utils {
@Autowired
private Environment env;
public String testProfile(){
return env.getProperty("profile");
}
}