|
@@ -1,27 +1,60 @@
|
|
package com.elab.spring.utils;
|
|
package com.elab.spring.utils;
|
|
|
|
|
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
import org.springframework.beans.BeansException;
|
|
import org.springframework.beans.BeansException;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-/**
|
|
|
|
- * @author Liukx
|
|
|
|
- * @create 2017-07-28 14:01
|
|
|
|
- * @email liukx@elab-plus.com
|
|
|
|
- **/
|
|
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
@Component
|
|
@Component
|
|
public class SpringUtils implements ApplicationContextAware {
|
|
public class SpringUtils implements ApplicationContextAware {
|
|
|
|
|
|
- private ApplicationContext applicationContext;
|
|
|
|
|
|
+ private static ApplicationContext applicationContext;
|
|
|
|
+
|
|
|
|
+ public SpringUtils() {
|
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
- this.applicationContext = applicationContext;
|
|
|
|
|
|
+ SpringUtils.applicationContext = applicationContext;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static ApplicationContext getApplicationContext() {
|
|
|
|
+ return applicationContext;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static <T> T getBean(String name) {
|
|
|
|
+ return (T) applicationContext.getBean(name);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static <T> T getBean(Class<T> clazz) {
|
|
|
|
+ return applicationContext.getBean(clazz);
|
|
}
|
|
}
|
|
|
|
|
|
- public Object getBean(String beanId) {
|
|
|
|
- return applicationContext.getBean(beanId);
|
|
|
|
|
|
+ public static <T> T getBean(String name, Class<T> clazz) {
|
|
|
|
+ return applicationContext.getBean(name, clazz);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static <T> Map<String, T> getBeansOfType(Class<T> type) {
|
|
|
|
+ return applicationContext.getBeansOfType(type);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String[] getBeanNamesForType(Class<?> type) {
|
|
|
|
+ return applicationContext.getBeanNamesForType(type);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getProperty(String key) {
|
|
|
|
+ return applicationContext.getEnvironment().getProperty(key);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String[] getActiveProfiles() {
|
|
|
|
+ return applicationContext.getEnvironment().getActiveProfiles();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getActiveProfile() {
|
|
|
|
+ String[] activeProfiles = getActiveProfiles();
|
|
|
|
+ return ArrayUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
|
|
|
|
+ }
|
|
}
|
|
}
|