SpringContextHolder.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package cn.com.qmth.ac.bean;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.ApplicationContextAware;
  4. import org.springframework.core.ResolvableType;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. public class SpringContextHolder implements ApplicationContextAware {
  8. private static ApplicationContext ctx = null;
  9. @Override
  10. public void setApplicationContext(ApplicationContext ctx) {
  11. SpringContextHolder.ctx = ctx;
  12. }
  13. public static ApplicationContext getApplicationContext() {
  14. return ctx;
  15. }
  16. public static Object getBean(String name) {
  17. return ctx.getBean(name);
  18. }
  19. public static <T> T getBean(String name, Class<T> requiredType) {
  20. return ctx.getBean(name, requiredType);
  21. }
  22. public static <T> T getBean(Class<T> requiredType) {
  23. return ctx.getBean(requiredType);
  24. }
  25. public static Object getBean(String name, Object... args) {
  26. return ctx.getBean(name, args);
  27. }
  28. public static <T> T getBean(Class<T> requiredType, Object... args) {
  29. return ctx.getBean(requiredType, args);
  30. }
  31. public static boolean containsBean(String name) {
  32. return ctx.containsBean(name);
  33. }
  34. public static boolean isSingleton(String name) {
  35. return ctx.isSingleton(name);
  36. }
  37. public static boolean isPrototype(String name) {
  38. return ctx.isPrototype(name);
  39. }
  40. public static boolean isTypeMatch(String name, ResolvableType typeToMatch) {
  41. return ctx.isTypeMatch(name, typeToMatch);
  42. }
  43. public static boolean isTypeMatch(String name, Class<?> typeToMatch) {
  44. return ctx.isTypeMatch(name, typeToMatch);
  45. }
  46. public static Class<?> getType(String name) {
  47. return ctx.getType(name);
  48. }
  49. public static String[] getAliases(String name) {
  50. return ctx.getAliases(name);
  51. }
  52. }