com.baomidou.framework.spring.SpringHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.baomidou.framework.spring.SpringHelper.java

Source

/**
 * Copyright (c) 2011-2014, hubin (jobob@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.framework.spring;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baomidou.framework.spring.SpringContextHolder;

/**
 * <p>
 * spring 
 * </p>
 * @author   hubin
 * @date    2016-06-28
 */
public class SpringHelper {

    /**
     * ? HttpServletRequest
     */
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * <p>
     * spring? bean
     * </p>
     * <p>
     *  ? servlet-context.xml 
     *  <br>
     *  <!-- ?ApplicationContext -->
     *   <bean id="springContextHolder" class="com.baomidou.framework.spring.SpringContextHolder" />
     * </p>
     * @param clazz
     *             bean class
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return SpringContextHolder.getBean(clazz);
    }

}