ApplicationContextAware接口实现,Spring上下文环境

一、ApplicationContextAware接口有什么用?

当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。

1、BeanFactory是什么?

BeanFactory作为基础的IoC容器,管理了spring所有的Bean,提供了最基本的容器功能,但是BeanFactory是一个接口。

2、ApplicationContext是什么?

ApplicationContext 是一个高级形态的 IoC 容器,它在 BeanFactory 基础上附加了好多其他功能, 提供了更多面向框架的功能,因此我们一般使用 ApplicationContext 进行开发

二、怎么使用这个接口?

1、自定义的类,实现ApplicationContextAware接口,例如ApplicationContextUtil实现接口,代码如下:

package com.upincar.contract.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextUtil implements ApplicationContextAware{

    private static ApplicationContext applicationContext;

    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     *
     * @param applicationContext spring上下文对象
     * @throws BeansException 抛出spring异常
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    	ApplicationContextUtil.applicationContext = applicationContext;
    }

    /**
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 获取对象
     *
     * @param name spring配置文件中配置的bean名或注解的名称
     * @return 一个以所给名字注册的bean的实例
     * @throws BeansException 抛出spring异常
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) applicationContext.getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clazz 需要获取的bean的类型
     * @return 该类型的一个在ioc容器中的bean
     * @throws BeansException 抛出spring异常
     */
    public static <T> T getBean(Class<T> clazz) throws BeansException {
        return applicationContext.getBean(clazz);
    }

    /**
     * 如果ioc容器中包含一个与所给名称匹配的bean定义,则返回true否则返回false
     *
     * @param name ioc容器中注册的bean名称
     * @return 存在返回true否则返回false
     */
    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }
}

2、在spring的配置文件中,注册方法类ApplicationContextUtil

注:方法类ApplicationContextUtil是一个bean,之所以方法类ApplicationContextUtil能够灵活自如地获取ApplicationContext就是因为spring能够为我们自动地执行了setApplicationContext。

但是spring不会无缘无故地为某个类执行它的方法的,所以就很有必要通过注册方法类ApplicationContextUtil的方式告知spring有这样子一个类的存在;

将方法类ApplicationContextUtil作为一个普通的bean在spring的配置文件中进行注册,在xml文件中配置如下:

<!-- 注入ApplicationContextAware接口实现类,spring上下文环境 -->
	<bean id="applicationContextUtil" class="com.upincar.contract.util.ApplicationContextUtil"/>

3、ApplicationContextUtil类的使用

   通过getBean方法来获取spring容器中的某个Bean,例如

   SystemInfoService s=ApplicationContextUtil.getBean("systemInfoServiceImpl");

public class DataEnDeUtil {
    private static final Logger log = Logger.getLogger(DataEnDeUtil.class);

    //和数据库的system_info表的code对应
    private static final String SYSTEM_INFO_SYSLEGALIP ="SYSLegalIP";
    
    public static String getSysLegalIP(String code){
    	if (StringUtils.isEmpty(code)) {
			return "";
		}
    	String sysLegalIP="";
    	SystemInfoExample exp=new SystemInfoExample();
    	exp.createCriteria().andCodeEqualTo(code);
    	SystemInfoService s=ApplicationContextUtil.getBean("systemInfoServiceImpl");
    	List<SystemInfo> lst=s.selectByExample(exp);
    	System.out.println(lst.size());
    	SystemInfo systemInfo=lst.get(0);
    	sysLegalIP=systemInfo.getContent();
    	return 	sysLegalIP;
    }
        
}

注:配置文件中关于systemInfoServiceImpl的内容:

<!-- 系统信息,systemInfoService -->
	<bean id="systemInfoServiceImpl" class="com.upincar.service.impl.SystemInfoServiceImpl"/>

-------------------------------------------------------------------------------------------------------------------------------------

ApplicationContextAware接口在springboot中的使用

由于springboot默认扫描规则是:自动扫描启动器类的同包或者其子包的下的注解。

所以在springboot项目中不需要在xml中配置,只需要在ApplicationContextUtil类中使用@Component注解,把ApplicationContextUtil注册为spring的组件,就可以在项目中使用ApplicationContextUtil获取spring容器中的某个Bean了。

猜你喜欢

转载自blog.csdn.net/qq_42714869/article/details/83069030