SpringContextHolder.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package cn.com.qmth.archive.utils;
  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. /**
  7. * spring context holder.
  8. *
  9. */
  10. @Component
  11. public class SpringContextHolder implements ApplicationContextAware {
  12. private static ApplicationContext ctx = null;
  13. @Override
  14. public void setApplicationContext(ApplicationContext ctx) {
  15. SpringContextHolder.ctx = ctx;
  16. }
  17. public static ApplicationContext getApplicationContext() {
  18. return ctx;
  19. }
  20. public static Object getBean(String name) {
  21. return ctx.getBean(name);
  22. }
  23. public static <T> T getBean(String name, Class<T> requiredType) {
  24. return ctx.getBean(name, requiredType);
  25. }
  26. public static <T> T getBean(Class<T> requiredType) {
  27. return ctx.getBean(requiredType);
  28. }
  29. public static Object getBean(String name, Object... args) {
  30. return ctx.getBean(name, args);
  31. }
  32. public static <T> T getBean(Class<T> requiredType, Object... args) {
  33. return ctx.getBean(requiredType, args);
  34. }
  35. public static boolean containsBean(String name) {
  36. return ctx.containsBean(name);
  37. }
  38. public static boolean isSingleton(String name) {
  39. return ctx.isSingleton(name);
  40. }
  41. public static boolean isPrototype(String name) {
  42. return ctx.isPrototype(name);
  43. }
  44. public static boolean isTypeMatch(String name, ResolvableType typeToMatch) {
  45. return ctx.isTypeMatch(name, typeToMatch);
  46. }
  47. public static boolean isTypeMatch(String name, Class<?> typeToMatch) {
  48. return ctx.isTypeMatch(name, typeToMatch);
  49. }
  50. public static Class<?> getType(String name) {
  51. return ctx.getType(name);
  52. }
  53. public static String[] getAliases(String name) {
  54. return ctx.getAliases(name);
  55. }
  56. }