Skip to content

Instantly share code, notes, and snippets.

@jfut
Last active July 18, 2019 13:07
Show Gist options
  • Save jfut/8400b6b3e6351b0b1a89 to your computer and use it in GitHub Desktop.
Save jfut/8400b6b3e6351b0b1a89 to your computer and use it in GitHub Desktop.
SpringBeanUtil: Spring で SingletonS2Container.getComponent() 相当を実現
/*
* Copyright 2014 Jun Futagawa
*
* 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 jp.integ.spring.util;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;
/**
* Spring が管理している Bean のインスタンスを取得するユーティリティクラスです。
* Bean 定義を XML ファイルではなく、Spring Boot などのアノテーション使用時にも動作します。
* HttpServletRequest に依存しているため、Web Application 以外での使用は考慮していません。
*
* @author Jun Futagawa
*/
public class SpringBeanUtil {
/** {@link ApplicationContext} です。 */
private ApplicationContext applicationContext;
/** 唯一のインスタンスです。 */
private static SpringBeanUtil instance;
/**
* インスタンスを作成します。
*/
public SpringBeanUtil() {
setupApplicationContext();
}
/**
* {@link ApplicationContext} をセットアップします。
*/
protected void setupApplicationContext() {
HttpServletRequest request = getRequest();
if (request == null) {
throw new UnsupportedOperationException();
}
applicationContext =
(ApplicationContext)request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
}
/**
* 唯一のインスタンスを返します。
*
* @return {@link SpringBeanUtil}
*/
public static synchronized SpringBeanUtil getInstance() {
if (instance == null) {
instance = new SpringBeanUtil();
}
return instance;
}
/**
* {@link ApplicationContext} を返します。
*
* @return {@link ApplicationContext}
*/
public ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 指定したクラスの Spring が管理しているインスタンスを返します。
*
* @param clazz
* クラス
* @return Spring が管理しているインスタンス
*/
public static <T> T getBean(Class<T> clazz) {
return getInstance().applicationContext.getBean(clazz);
}
/**
* Spring が管理している {@link HttpServletRequest} を返します。
*
* @return {@link HttpServletRequest}
*/
protected static HttpServletRequest getRequest() {
ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = null;
if (servletRequestAttributes != null) {
request = servletRequestAttributes.getRequest();
}
return request;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment