欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

《ApplicationContextAware》

程序员文章站 2022-03-11 08:54:46
ApplicationContextAware感知应用上下文1.ApplicationContextAware在项目中的应用2.聚合所有Service(结合ApplicationContextAware),实现无需注入,也可拿到Service,任意地方使用,带来更大的灵活性。1.ApplicationContextAware在项目中的应用实现ApplicationContextAware接口,拿到应用上下文就可以拿到IOC里面的Bean,自己需注册到IOCimport org.springfram...

1.ApplicationContextAware在项目中的应用

  • 实现ApplicationContextAware接口,拿到应用上下文就可以拿到IOC里面的Bean,自己需注册到IOC
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component(value = "applicationContextUtils")
public class ApplicationContextUtils implements ApplicationContextAware {
    private static ApplicationContext context;//应用上下文
    //本类维护自己注册到IOC容器的Bean
    private static ApplicationContextUtils applicationContextUtils;
    @Value(value = "${spring.application.name}")
    private String applicationName;//应用名
    @Value(value = "${server.port}")
    private String serverPort;//服务器端口号
    @Value(value = "${server.servlet.context-path}")
    private String contextPath;//应用上下文访问前缀
    private static String serverInfo;//服务器信息
    private static final Class<ApplicationContextUtils> CLASS = ApplicationContextUtils.class;


    public static String getContextPath(){
        if (applicationContextUtils == null){
            applicationContextUtils = getBean(CLASS);
        }
        return applicationContextUtils.contextPath;
    }

    public static String getServerInfo(){
        if (serverInfo == null){
            if (applicationContextUtils == null){
                applicationContextUtils =  getBean(CLASS);
            }
            serverInfo = applicationContextUtils.applicationName+"_"+applicationContextUtils.serverPort;
        }
        return serverInfo;
    }

    public static String getApplicationName(){
        if (applicationContextUtils == null){
            applicationContextUtils = getBean(CLASS);
        }
        return applicationContextUtils.applicationName;
    }

    public static String getServerPort(){
        if (applicationContextUtils == null){
            applicationContextUtils = getBean(CLASS);
        }
        return applicationContextUtils.serverPort;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getContext(){
        return context;
    }

    public static<T> T getBean(Class<T> clazz){
        T result = null;
        if (context != null){
            result = context.getBean(clazz);
        }
        return result;
    }
}

2.聚合所有Service(结合ApplicationContextAware),实现无需注入,也可拿到Service,任意地方使用,带来更大的灵活性。

import com.wpq.service.IServiceA;
import com.wpq.service.IServiceB;
import com.wpq.service.IServiceC;

//玩转IOC
public class ServiceHolder {
    //全部为接口
    private static IServiceA serviceA;
    private static IServiceB serviceB;
    private static IServiceC serviceC;

    public static IServiceA getServiceA() {

        if (serviceA == null){
            serviceA = ApplicationContextUtils.getBean(IServiceA.class);
        }
        return serviceA;
    }

    public static IServiceB getServiceB() {
        if (serviceB == null){
            serviceB = ApplicationContextUtils.getBean(IServiceB.class);
        }
        return serviceB;
    }

    public static IServiceC getServiceC() {
        if (serviceC == null){
            serviceC = ApplicationContextUtils.getBean(IServiceC.class);
        }
        return serviceC;
    }
}

本文地址:https://blog.csdn.net/weixin_43766298/article/details/107394847