ApplicationContextAware加载Spring上下文环境
来源:互联网 发布:fw是什么软件 编辑:程序博客网 时间:2024/06/05 19:30
项目用到了ApplicationContextAware,通过它spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法。
我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。
使用方法如下:
1.实现ApplicationContextAware接口:
- package com.bis.majian.practice.module.spring.util;
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- public class SpringContextHelper implements ApplicationContextAware {
- private static ApplicationContext context = null;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- context = applicationContext;
- }
- public static Object getBean(String name){
- return context.getBean(name);
- }
- public static Map<String, Class> getBeansOfType(Classclass){
- return context.getBeansOfType(class);
- }
- }
2.在Spring的配置文件中配置这个类,Spring容器会在加载完Spring容器后把上下文对象调用这个对象中的setApplicationContext方法:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">
- <bean id="springContextHelper" class="com.bis.majian.practice.module.spring.util.SpringContextHelper"></bean>
- <context:component-scan base-package="com.bis.majian.practice.module.*" />
- </beans>
3.在web项目中的web.xml中配置加载Spring容器的Listener:
- <!-- 初始化Spring容器,让Spring容器随Web应用的启动而自动启动 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
4.在项目中即可通过这个SpringContextHelper调用getBean()方法得到Spring容器中的对象了。getBeansOfType(class)能获取beanName与bean对象的map集合
阅读全文
0 0
- 加载Spring上下文环境ApplicationContextAware
- ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境(获取bean)
- ApplicationContextAware 获取Spring 上下文
- Spring ApplicationContextAware获取上下文
- Spring ApplicationContextAware获取上下文
- 程序运行过程中获取Spring上下文通过ApplicationContextAware
- spring 如何加载上下文
- ApplicationContextAware获取上下文bean
- 获取spring环境上下文
- spring ApplicationContextAware
- Spring ApplicationContextAware
- java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
- Javascript 面向对象编程(一):封装
- 嵌入式开发—C语言面试题
- 深度学习研究草稿笔记(1)
- eclipse常用快捷键
- ApplicationContextAware加载Spring上下文环境
- stm32 CAN通信 TJA1040
- WebView 简单使用
- Jenkins集成TestNG Junit自动化测试
- 一张表中两个字段互相关联
- jmeter企业内训简报
- square开源日历控件—CalendarPickerView源码导读
- ArrayList与LinkedList的普通for循环遍历效率对比
- ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your