您的位置:首页 > 编程语言 > Java开发

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"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息