zoukankan      html  css  js  c++  java
  • 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();
    }

  • 相关阅读:
    lntelliJ IDEA 皮肤设置
    Maven安装与配置
    lntelliJ IDEA 使用 Maven 与 每次新建项目都需要重新配置的解决方案
    Spring Boot 扫描机制说明
    Spring Boot Filter 使用指南
    Gradle构建CAS4.2.7爬坑指南
    Java的垃圾回收
    final与static
    angular directive自定义指令
    ui-router
  • 原文地址:https://www.cnblogs.com/adamas21/p/5806793.html
Copyright © 2011-2022 走看看