Spring:ApplicationContextAware
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);
- }
- }
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容器中的物件了。
相關文章
- spring(1)-ApplicationContextAware詳解SpringAPPContext
- ApplicationContextAware使用理解APPContext
- ApplicationContextAware使用說明APPContext
- spring、spring MVC、spring BootMVCSpring Boot
- Spring系列之初識Spring Spring概述Spring
- Spring框架 - Spring和Spring框架組成Spring框架
- Spring Boot/Spring MVCSpring BootMVC
- Learn Spring – Spring DAOSpring
- Spring Boot —— Spring SecuritySpring Boot
- Spring Boot整合Spring BatchSpring BootBAT
- Spring Boot整合Spring SecuritySpring Boot
- Spring Boot整合Spring AopSpring Boot
- Spring系列—Spring入門Spring
- 理解Spring(一):Spring 與 IoCSpring
- Spring Boot:整合Spring Data JPASpring Boot
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- 「Spring認證」Spring 框架概述Spring框架
- Spring---Spring專題(二)Spring
- spring:spring與mybatis的整合SpringMyBatis
- spring框架——Spring框架簡介Spring框架
- Spring review--Spring框架概述SpringView框架
- Spring review--Spring框架搭建SpringView框架
- spring-boot+spring-session整合SpringbootSession
- SPRING-spring註解整理Spring
- SpringSpring
- Spring框架系列(2) - Spring簡單例子引入Spring要點Spring框架單例
- spring-boot 整合 spring-securitySpringboot
- 【Spring系列】- Spring迴圈依賴Spring
- 【Spring】Spring後置處理器Spring
- spring-boot 整合 spring-sessionSpringbootSession
- Spring Boot (十三): Spring Boot 小技巧Spring Boot
- spring學習:spring原始碼_BeanDefinitionSpring原始碼Bean
- Spring security(四)-spring boot +spring security簡訊認證+redis整合Spring BootRedis
- Spring、Spring Boot和TestNG測試指南 – 使用Spring Testing工具Spring Boot
- 如何從Spring Security 5遷移到Spring Security 6/Spring Boot 3Spring Boot
- 【spring】迴圈依賴 Java Vs SpringSpringJava
- Spring Cloud Stream事件路由 - spring.ioSpringCloud事件路由
- Spring入門(一):建立Spring專案Spring