• springContext


    方法一

    package com.hsh.utils;

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    @Component
    public class SpringContexts implements ApplicationContextAware {

    private static ApplicationContext context;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringContexts.context = applicationContext;
    }

    public static ApplicationContext getContext() {
    return context;
    }

    /**
    * 获取spring 管理的bean
    * @param name
    * @param requiredType
    * @return
    */
    public static <T> T getBean(String name, Class<T> requiredType) {
    return context.getBean(name, requiredType);
    }

    public static Object getBean(String beanName) {
    return context.getBean(beanName);
    }

    }

    方法二

    //解决service为null无法注入问题
    BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
    userOpenidService = (UserOpenidService) factory.getBean("userOpenidService");

    获取session和request

    /**
    * 取request
    * @return
    */
    public static HttpServletRequest getRequest () {
    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    return request;
    }
    /**
    * 取Session
    * @return
    */
    public static HttpSession getSession () {
    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    return request.getSession();
    }

  • 相关阅读:
    ICMP协议
    观察者模式-Observer
    模板方法模式-Template Method
    Java的演变过程
    汉诺塔-Hanoi
    外观模式-Facade
    JDK5-增强for循环
    JDK5-可变参数
    动态代理与AOP
    代理模式-Proxy
  • 原文地址:https://www.cnblogs.com/adamas21/p/5806793.html
Copyright © 2020-2023  润新知