03_spring获取上下文工具类
2016-01-09 04:10
876 查看
通过读取配置文件方式在工具类中注入applicationContext
package com.xy.utils.SpringUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring工具类,读取配置文件 * @author javaw * * */ public class SpringUtilsFromClassPathXml { public static ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml"); public static Object getBean(String beanName){ return context.getBean(beanName); } @SuppressWarnings("unchecked") public static Object getBean(Class clazz){ return context.getBean(clazz); } }这种方式不推荐,applicationContext会加载多次。web.xml种会加载,工具类中还会加载。
实现ApplicationContextAware接口
package com.xy.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * Spring工具类1,继承ApplicationContextAware * @author javaw * */ public class SpringContextUtils implements ApplicationContextAware { private static ApplicationContext context = null; public void setApplicationContext(ApplicationContext context) throws BeansException { if(this.context==null){ this.context = context; } } public static ApplicationContext getApplicationContext() { return context; } @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) context.getBean(name); } @SuppressWarnings("unchecked") public static <T> T getBean(Class clazz){ return (T) context.getBean(clazz); } }
继承ApplicationObjectSupport抽象类
[align=left]抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。[/align]Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
package com.xy.utils.SpringUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ApplicationObjectSupport; /** * Spring工具类4,继承ApplicationContextObjectSupport * @author javaw * */ public class SpringUtilsFromApplicationObjectSupport extends ApplicationObjectSupport { private static ApplicationContext context1 = null; public void setContext(ApplicationContext context) { this.context1 = context; } public ApplicationContext getContext() { return context1; } public static Object getBean(String beanName){ return context1.getBean(beanName); } @SuppressWarnings("unchecked") public static Object getBean(Class clazz){ return context1.getBean(clazz); } }
继承WebApplicationObjectSupport
[align=left]实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。[/align]Spring初始化时,会通过该方法将ApplicationContext对象注入。
package com.xy.utils.SpringUtils; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationObjectSupport; /** * Spring工具类5,继承WebApplicationContextObjectSupport * @author javaw * */ public class SpringUtilsFromWebApplicationObjectSupport extends WebApplicationObjectSupport { private static ApplicationContext context = null; @Override protected void initApplicationContext(ApplicationContext context) { super.initApplicationContext(context); this.context = context; } public static Object getBean(String beanName){ return context.getBean(beanName); } @SuppressWarnings("unchecked") public static Object getBean(Class clazz){ return context.getBean(clazz); } }
使用ContextLoader获取上下文
package com.xy.utils.SpringUtils; import org.springframework.context.ApplicationContext; import org.springframework.web.context.ContextLoader; /** * Spring工具类3,从contextLoader中获取 * @author javaw * */ public class SpringUtilsFromCurrentWebApplictionContext { private static ApplicationContext context = null; public SpringUtilsFromCurrentWebApplictionContext() { context = ContextLoader.getCurrentWebApplicationContext(); } public static Object getBean(String beanName){ return context.getBean(beanName); } @SuppressWarnings("unchecked") public static Object getBean(Class clazz){ return context.getBean(clazz); } }
实现ApplicationListener接口
常量类:package com.xy.common; import org.springframework.context.ApplicationContext; /** * 常量类 * @author javaw * */ public class SysContent { public static ApplicationContext applicationContext = null; }注入类:
package com.xy.service.serviceImpl; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import com.xy.common.SysContent; /** * Spring文件初始化,将上下文放入SysContent中,方便程序调用。 * @author javaw * */ public class SystemInitImpl implements ApplicationListener<ContextRefreshedEvent>{ /** *实现onApplicationEvent方法 */ public void onApplicationEvent(ContextRefreshedEvent evet) { SysContent.applicationContext = evet.getApplicationContext(); } }在spring中初始化:
< bean class ="com.xy.service.impl.SystemInitImpl"/>
相关文章推荐
- Spring3工具类
- Spring工具类,获取Spring容器中的Bean
- 看Spring工具类一文,附自己的使用心得(改)
- Java设计模式(Design Patterns In Java)读书摘要——第1章 绪论
- 01_spring基本命名空间及各种类型注入方式
- java中static{}块
- 简易的Java树节点
- 彻底 卸载/删除 jdk java
- 微服务与Java EE
- Java基础复习一之多线程(并发,内存模型)
- java中 try return finally return(转)
- JAVA RMI 学习一
- Java基础学习笔记1
- javaweb项目Session的无操作失效时间
- 插入排序
- Spring Boot 启动加载数据 CommandLineRunner的使用
- 使用eclipse创建maven web项目
- Introduction to Java Remote Method Invocation (RMI
- 利用JAVA反射机制将列表转为对象
- java socket 编程入门