0
点赞
收藏
分享

微信扫一扫

补充知识点八:Spring 中获取Bean的不同方式

(一)案例背景介绍
  我们都知道,在Spring框架中,我们可以通过“@Autowired”注解来注入各种Bean对象,但在某些场景下,是无法通过“@Autowired”注解来注入对象的,比如:
  场景一:我们在静态方法中就无法使用“@Autowired”注解注入的对象(这个问题不能在被注入的对象前面加“static”,Spring就不容许注入静态对象)
  场景二:我们在Spring拦截器中也无法通过“@Autowired”注解获取对象(因为拦截器是先于一些注解运行的,比如“@service”注解)
(二)解决办法
  解决办法的核心就是“ApplicationContextAware”接口,通过它来实现调用spring容器中的bean。
1、创建SpringContextHolder工具类继承“ApplicationContextAware”接口

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
* @author **
* @date 2018/6/14 16:25
*/
public class SpringContextHolder implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringContextHolder.class);
private static ApplicationContext applicationContext;

/**
* 实现ApplicationContextAware接口的context注入函数,将其存入静态变量
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
/**
* 去的存储在静态变量中的ApplicationContext
*/
public static ApplicationContext getApplicationContext(){
if (applicationContext == null){
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext-web.xml中注册SpringContextHolder" +
"或者在applicationContext.xml中配置扫描SpringContextHolder所在的包,然后给SpringContextHolder加上" +
"@Component注解");
}
return applicationContext;
}
/**
* 从静态变量ApplicationContext取出bean,并为bean转型
*/
public static <T> T getBean(Class<T> clazz){
return applicationContext.getBean(clazz);
}
}

2、在Spring配置文件中注册该工具类

<bean id="springContextHolder" class="com.imooc.util.SpringContextHolder"/>

  或者在applicationContext.xml中配置扫描SpringContextHolder所在的包,然后给SpringContextHolder加上@Component注解。

(三)SpringContextHolder解析
  主要功能:那些没有归入spring框架管理的类却要调用spring容器中的bean提供的工具类。
  在spring中要通过IOC依赖注入来取得对应的对象,但是该类通过实现ApplicationContextAware接口,以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext。
  如此就不能说说org.springframework.context.ApplicationContextAware这个接口了:
  当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。
  “ApplicationContext”封装的是web.xml 内部的信息,而你的web.xml里面有spring的配置文件,所以就能获取所有spring的信息。同样也能获取和web.xml有关系的所有其他信息。web.xml文件中封装的Spring配置文件信息:

<!-- 配置spring监听器 start -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- spring监听器加载的配置文件 -->
<param-value>classpath:spring/root-context.xml</param-value>
</context-param>
<listener>
<!--ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring监听器 end -->

  ​​classpath:spring/root-context.xml​​封装了Spring配置信息。

(三)SpringContextHolder使用
  Spring中使用很简单,就不再赘述了!

  其他环境下使用:

public class App {
public static void main(String[] args) {

System.out.println("加载spring");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:spring/spring-context.xml"});
context.start();

SpringContextHolder springContextHolder = new SpringContextHolder();
springContextHolder.setApplicationContext(context);
//SpringContextHolder.getBean("queryDealService");
System.out.println("Hello World!");
}
}


举报

相关推荐

0 条评论