一、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. ColorBox常见问题

    发现colorbox官方网站的troubleshoot写的比较好,转载一下. 1,flash覆盖colorbox: This is not a ColorBox specific problem, b ...

  2. eclipse如何导入PHP的项目

    http://zhidao.baidu.com/link?url=2jvsgawRlEWzE63-Wm-e51_Nl0dWH1Z4z5VS_s2E824y2fYqsvNzdZ8GfEh6DOVtjY8 ...

  3. init进程解析rc文件的相关函数分析

    init进程的源码文件位于system/core/init,其中解析rc文件语法的代码放在五个函数中, init_parse_config_file (init_parser.c), read_fil ...

  4. Android 权限查寻表

    示例 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=" ...

  5. WINCE6.0去掉桌面快捷方式

    WINCE6.0去掉桌面快捷方式,主要是修改xxx.bat文件,比如我要去掉My Documents和Media Player的快捷方式. (1)    去掉My Documents桌面快捷方式 找到 ...

  6. eclipse 中创建maven web项目

    Maven的Eclipse插件m2eclipse在线安装地址 http://m2eclipse.sonatype.org/sites/m2e:我又试了link方式安装也没什么作用,不知怎么回事? 还有 ...

  7. Asp.net调用百度搜索引擎

    ASP.NET 调用百度搜索引擎 百度搜索引擎提供了一段嵌入到页面中的代码 <form action="http://www.baidu.com/baidu" target= ...

  8. SPOJ DQUERY:D-query

    主席树/树状数组.给一个区间,多次询问[l,r]内有多少个不重复的元素.每个前缀都建线段树,询问直接r的[l,r]就可以了.(似乎对主席树有一点了解了?...话说spoj好高级的样子... #incl ...

  9. mysql 触发器学习(可以将mysql数据同步到redis)

    1. 一个简单的例子 1.1. 创建表: create table t(s1 integer); 1.2. 触发器: delimiter | create trigger t_trigger befo ...

  10. POJ 1815 Friendship ★(字典序最小点割集)

    [题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...