|
@@ -5,16 +5,28 @@ import java.util.Map;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
|
|
|
import com.leanwo.management.exception.BeanNotInjectException;
|
|
import com.leanwo.management.exception.BeanNotInjectException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+// TODO: Auto-generated Javadoc
|
|
|
|
|
+/**
|
|
|
|
|
+ * The Class SpringUtil.
|
|
|
|
|
+ */
|
|
|
public class SpringUtil implements ApplicationContextAware {
|
|
public class SpringUtil implements ApplicationContextAware {
|
|
|
|
|
+
|
|
|
|
|
+ /** The logger. */
|
|
|
private static Logger logger = LoggerFactory.getLogger(SpringUtil.class);
|
|
private static Logger logger = LoggerFactory.getLogger(SpringUtil.class);
|
|
|
|
|
+
|
|
|
|
|
+ /** The application context. */
|
|
|
private static ApplicationContext applicationContext;
|
|
private static ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
+ /* (non-Javadoc)
|
|
|
|
|
+ * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
|
|
|
|
|
+ */
|
|
|
@Override
|
|
@Override
|
|
|
public void setApplicationContext(ApplicationContext arg0) {
|
|
public void setApplicationContext(ApplicationContext arg0) {
|
|
|
applicationContext = arg0;
|
|
applicationContext = arg0;
|
|
@@ -23,6 +35,11 @@ public class SpringUtil implements ApplicationContextAware {
|
|
|
logger.info("Spring容器初始化成功。");
|
|
logger.info("Spring容器初始化成功。");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Gets the application context.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return the application context
|
|
|
|
|
+ */
|
|
|
public static ApplicationContext getApplicationContext() {
|
|
public static ApplicationContext getApplicationContext() {
|
|
|
checkApplicationContext();
|
|
checkApplicationContext();
|
|
|
return applicationContext;
|
|
return applicationContext;
|
|
@@ -30,6 +47,10 @@ public class SpringUtil implements ApplicationContextAware {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param <T> the generic type
|
|
|
|
|
+ * @param name the name
|
|
|
|
|
+ * @return the bean
|
|
|
*/
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
|
public static <T> T getBean(String name) {
|
|
public static <T> T getBean(String name) {
|
|
@@ -39,6 +60,10 @@ public class SpringUtil implements ApplicationContextAware {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param <T> the generic type
|
|
|
|
|
+ * @param clazz the clazz
|
|
|
|
|
+ * @return the bean
|
|
|
*/
|
|
*/
|
|
|
public static <T> T getBean(Class<T> clazz) {
|
|
public static <T> T getBean(Class<T> clazz) {
|
|
|
checkApplicationContext();
|
|
checkApplicationContext();
|
|
@@ -47,6 +72,9 @@ public class SpringUtil implements ApplicationContextAware {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param clazz the clazz
|
|
|
|
|
+ * @return the single bean
|
|
|
*/
|
|
*/
|
|
|
public static Object getSingleBean(Class clazz) {
|
|
public static Object getSingleBean(Class clazz) {
|
|
|
checkApplicationContext();
|
|
checkApplicationContext();
|
|
@@ -63,9 +91,9 @@ public class SpringUtil implements ApplicationContextAware {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 获取当前IOC所有Bean的名称
|
|
|
|
|
- *
|
|
|
|
|
- * @return
|
|
|
|
|
|
|
+ * 获取当前IOC所有Bean的名称.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return the string[]
|
|
|
*/
|
|
*/
|
|
|
public static String[] beanNames() {
|
|
public static String[] beanNames() {
|
|
|
checkApplicationContext();
|
|
checkApplicationContext();
|
|
@@ -73,6 +101,9 @@ public class SpringUtil implements ApplicationContextAware {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Check application context.
|
|
|
|
|
+ */
|
|
|
private static void checkApplicationContext() {
|
|
private static void checkApplicationContext() {
|
|
|
if (applicationContext == null) {
|
|
if (applicationContext == null) {
|
|
|
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
|
|
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
|