spring获取容器中的对象
一、代码工具类
package com.feifan.study.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* 这里必须实现ApplicationContextAware接口,注入上下文容器
*/
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
/**
* 获取上下文对象
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过对象在容器中的名称获取对象
*
* @param beanName
* @param <T>
* @return
*/
public static <T> T getBean(String beanName) {
return (T) applicationContext.getBean(beanName);
}
/**
* 通过class类型获取容器中的对象
*
* @param beanClass
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> beanClass) {
return (T) applicationContext.getBean(beanClass);
}
/**
* Spring容器启动后,会把 applicationContext 给自动注入进来,然后我们把 applicationContext 赋值到静态变量中,方便后续拿到容器对象
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
}
二、使用
直接在代码用调用SpringContextUtils静态方法获取bean就ok,可以通过bean的id获取,也可以通过bean的类型获取
@RequestMapping("/people")
public String getInfo() {
People people = SpringContextUtils.getBean("people");
return people.getInfo();
}
三、注意事项以及说明
- 工具类必须交给spring管理创建;
- 工具类需实现ApplicationContextAware接口,为了注入上下文容器;
- 工具方法可以直接通过类名点方法名调用,方便快捷。