1. 程式人生 > >主動獲取spring容器工具類SpringContextUtil

主動獲取spring容器工具類SpringContextUtil

/**
 * 獲取spring容器,以訪問容器中定義的其他bean
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    // Spring應用上下文環境
    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 實現ApplicationContextAware介面的回撥方法,設定上下文環境
     * 
     * @param applicationContext
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * @return ApplicationContext
     */
    public ApplicationContext getApplicationContext() {
        return this.applicationContext;
    }

    /**
     * 獲取物件 這裡重寫了bean方法,起主要作用
     * 
     * @param name
     * @return Object 一個以所給名字註冊的bean的例項
     * @throws BeansException
     */
    public Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

}

使用
@Autowired
private SpringContextUtil springContextUtil;

springContextUtil.getApplicationContext().getBean(this.getClass());


相關推薦

主動獲取spring容器工具SpringContextUtil

/** * 獲取spring容器,以訪問容器中定義的其他bean */ @Component public class SpringContextUtil implements ApplicationContextAware { // Spring應用上下文

springboot配置:通過工具獲取spring容器中的bean

**由於公司電腦限制,完全手敲,有單字錯誤望理解** @component @SuppressWarnings("static-access") public class AppContext implements ApplicationContextAware{   &n

普通Java獲取spring 容器的bean的5種方法 Spring注入非單例bean以及scope的作用範圍

本文轉載自:http://www.cnblogs.com/duanxz/archive/2014/06/18/3794075.html 方法一:在初始化時儲存ApplicationContext物件方法二:通過Spring提供的工具類獲取ApplicationContext物件方法三:繼承自抽象類Appli

Spring boot 獲取yml檔案工具

package com.common.base.utils.base; import com.common.base.generator.ResourceManager; import org.yaml.snakeyaml.Yaml; import java.io.InputStream;

SpringBoot 之 普通獲取Spring容器中的bean

我們知道如果我們要在一個類使用spring提供的bean物件,我們需要把這個類注入到spring容器中,交給spring容器進行管理,但是在實際當中,我們往往會碰到在一個普通的Java類中,想直接使用spring提供的其他物件或者說有一些不需要交給spring管理,但是需要

非託管獲取Spring容器資訊

非託管類: 不受Spring容器管理的類; 獲取bean @Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext a

框架---spring---Filter中獲取Spring容器中的bean物件

@Override public void init(FilterConfig filterConfig) throws ServletException { //獲取w

Spring如何管理Java普通(Java獲取Spring容器的bean)

方法一:在初始化時儲存ApplicationContext物件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.x

java使用Calendar獲取常用簡單工具

凱哥java前言:在工作中,我們經常會用到時間相關的。比如月初、月末、年初、年末、指定月份所在季度的季末、當前時間加X天、x月、x年等這些常用的雖說不難,但是如果要用到,立馬又想不起來。這裏凱哥歸納了一些常用的放在一個工具類中。有需要的朋友可以拿去或是收藏。如果大家有更好的,歡迎留言。如果凱哥哪裏不對,歡迎大

Spring boot 工具靜態屬性註入及多環境配置

data 地址 val 文件 source 其他 username eat serve 由於需要訪問MongoDB,但是本地開發環境不能直接連接MongoDB,需要通過SecureCRT使用127.0.0.2本地IP代理。但是程序部署到線上生產環境後,是可以直接訪問Mong

java從Swagger Api接口獲取數據工具

api接口 png 獲取數據 com http bubuko inf ima 技術 java從Swagger Api接口獲取數據工具類

redis整合spring(redisTemplate工具) redis整合spring(redisTemplate工具)

redis整合spring(redisTemplate工具類) 原文地址:http://blog.csdn.net/qq_34021712/article/details/75949706   ©王賽超 前言 關於哨兵模式的配置,我是參考網上

Java service層獲取HttpServletRequest的工具

大家都知道 能在Controller/action層獲取 HttpServletRequest , 但是這裡給大家備份的 是 從程式碼內部  service層獲取HttpServletRequest 工具類。  具體如下: package com.base.common.

spring註解工具AnnotatedElementUtils和AnnotationUtils

一、前言   spring為開發人員提供了兩個搜尋註解的工具類,分別是AnnotatedElementUtils和AnnotationUtils。在使用的時候,總是傻傻分不清,什麼情況下使用哪一個。於是我做了如下的整理和總結。 二、AnnotationUtils官方解釋   功能   用於處理註解,處

mybatis免sql外掛之JpaMapper-以Jpa hibernate的風格寫mybatis(獲取spring容器中mybatis的mapper)

mybatis免sql外掛之JpaMapper-以Jpa hibernate的風格寫mybatis(獲取spring容器中mybatis的mapper) 簡介 JpaMapper以Jpa hibernate的風格寫mybatis的程式碼,可以減少手動寫sql的煩惱。 優勢:

Spring(4) Bean獲取Spring容器

當一個bean需要獲取它所在spring容器的時候,實際上這種情況是很常見的,例如要輸出國際化資訊,釋出事件等。 bean可以通過實現BeanFactoryAware介面實現獲取它所在的spring容器,BeanFactoryAware只有一個setBeanFactory方法,sprin

實現ApplicationContextAware介面,java(new或者java反射獲取的物件)中獲取spring容器的bean

本文參考了https://blog.csdn.net/bailinbbc/article/details/76446594,其實是拷貝了很多內容: 在Web應用中,Spring容器通常採用宣告式方式配置產生:開發者只要在web.xml中配置一個Listener,該Listener將會負責初始化S

springboot整合filter之在filter中如何獲取spring容器中的bean物件

本人在專案使用的是springboot,具業務需要本人使用了filter過濾器,進行一個路徑攔截,本人配置的是攔截所有/*,然後對路徑中包含的特有欄位進行處理。 filter中需要使用service的bean物件,我使用@Autowired註解注入物件,然後啟動,訪問時報空

訪問url獲取頁面內容工具

package com.guanyong.fbimonitor.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.EOFException; im

FilesNameUtil獲取檔名稱工具

借鑑網路資料整理而成 import java.io.File; import java.util.ArrayList; import java.util.List; public class FilesNameUtil { private static final String sep