一、ApplicationContextAware 用处

  Spring 提供了ApplicationContextAware类,通过它可以获取所有bean上下文。

二、怎么用?

  ①、定义一个工具类,去实现 ApplicationContextAware,实现 setApplicationContext方法即可

public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext; // Spring应用上下文环境

    @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
} /**
* 获取对象
*
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
} }

  ②、业务代码

public interface UserReadService {

    public UserInfo getUserInfoById(Long id);
} @Component("userReadService")
public class UserReadServiceImpl implements UserReadService { @Override
public UserInfo getUserInfoById(Long id) {
System.out.println("获取用户信息");
return null;
} }

  ③、xml 配置

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config />
<!--自动扫描含有@Service将其注入为bean -->
<context:component-scan base-package="com.mycompany.yuanmeng.springdemo.aop" /> <bean id="springContextUtil" class="com.mycompany.yuanmeng.springdemo.aop.SpringContextUtil" /> </beans>

  ④、test

public static void test() {

        new ClassPathXmlApplicationContext("spring.xml"); // 加载ApplicationContext(模拟启动web服务), 当然如果是用web工程的话,可以直接在web.xml配置,容器会去加载此文件。

        UserReadService userReadService = (UserReadService) SpringContextUtil.getBean("userReadService");

        userReadService.getUserInfoById(null);
}

Spring ApplicationContextAware获取上下文的更多相关文章

  1. Spring Spring boot 获取IOC中的bean,ApplicationContext

    https://blog.csdn.net/weixin_38361347/article/details/89304414 https://www.jianshu.com/p/9ea13b00b1d ...

  2. spring中获取dao或对象中方法的实例化对象

    spring中获取dao的中方法的实例化对象: //获取应用上下文对象 ApplicationContext ctx = new ClassPathXmlApplicationContext(&quo ...

  3. spring controller 获取context

    想要获取context需要先熟悉spring是怎么在web容器中启动的,spring启动过程其实就是其IOC容器的启动过程,对于web程序,IOC容器启动过程即是建立上下文的过程 spring启动过程 ...

  4. 基于Spring注解的上下文初始化过程源码解析(一)

    最近工作之余有时间和精力,加上平时对源码比较感兴趣,就开始啃起了Spring源码.为加深印象写了这篇博客,如有错误,望各位大佬不吝指正. 我看的是Spring5的源码,从同性社区download下来后 ...

  5. ApplicationContextAware获取bean

    ApplicationContextAware获取bean 概述 在某些特殊的情况下,Bean需要实现某个功能,但该功能必须借助于Spring容器才能实现,此时就必须让该Bean先获取Spring容器 ...

  6. 前后台获取上下文context

    1.web server端获取上下文:Context ctx = WafContext.getInstance().getContext();上下文中包含当前登录组织.当前登录用户.语种.数据库.客户 ...

  7. spring java 获取webapp下文件路径

    spring java 获取webapp下文件路径 @RequestMapping("/act/worldcup_schedule_time/imgdownload") @Resp ...

  8. spring mvc获取路径参数的几种方式 - 浅夏的个人空间 - 开源中国社区

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  9. Spring Boot获取前端页面参数的几种方式总结

    Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...

随机推荐

  1. HTML DOM与XML DOM之间,既有区别

    http://kb.cnblogs.com/page/74971/ HTML文档可以使用Core API和HTML API两者: 而XML文档只能使用Core API. 换句话说,HTML与XML重叠 ...

  2. [译]Atomic VS. Non-Atomic 操作

    原文链接:atomic-vs-non-atomic-operations 在网上已经写了很多关于原子操作的文章,但是通常都集中在原子的读-修改-写(RMW. read-modify-write)操作. ...

  3. C语言动态生成二维数组

    # 动态创建二维数组示例 #include "stdlib.h" #include "stdio.h" #include <malloc.h> in ...

  4. java中synchronized的用法详解

    记下来,很重要. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchron ...

  5. I.MX6 wm8962 0-001a: DC servo timed out

    /******************************************************************************* * I.MX6 wm8962 0-00 ...

  6. js变量作用域

    for(var i =0;i<100;i++) { } alert(i);//100 if(true){ var i="91d"; } alert(i);//91d func ...

  7. SQL游标

    最近工作中有用到游标,特简单总结一下: 一.简介      游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力.我们 ...

  8. lightoj 1016

    水题,排个序直接搞. #include<cstdio> #include<string> #include<cstring> #include<iostrea ...

  9. as3+java+mysql(mybatis) 数据自动工具(六)

    这篇来写一些常量定义的实例.我一般在配置常量的时候,都会让 bitOffset = 20,这样是一个比较好的分配,就是每个分组可以有 0xFFFFF(1048575) 个常量,0xFFF(4095) ...

  10. bzoj 1058 [ZJOI2007]报表统计(set)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1058 [题意] 一个序列,提供插入,查询相邻最小差值,查询任意最小差值的操作. [思路 ...