ApplicationContextAware 接口的作用

先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述:

 
即是说,当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean。换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象。


如何使用 ApplicationContextAware 接口

如何使用该接口?很简单。

1、定义一个工具类,实现 ApplicationContextAware,实现 setApplicationContext方法

public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext context; @Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
SpringContextUtils.context = context;
} public static ApplicationContext getContext(){
return context;
} }
14
 
1
public class SpringContextUtils implements ApplicationContextAware { 
2
    private static ApplicationContext context;
3

4
    @Override
5
    public void setApplicationContext(ApplicationContext context)
6
            throws BeansException {
7
        SpringContextUtils.context = context;
8
    }
9

10
    public static ApplicationContext getContext(){
11
        return context;
12
    }
13

14
}
如此一来,我们就可以通过该工具类,来获得 ApplicationContext,进而使用其getBean方法来获取我们需要的bean。

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

之所以我们能如此方便地使用该工具类来获取,正是因为Spring能够为我们自动地执行 setApplicationContext 方法,显然,这也是因为IOC的缘故,所以必然这个工具类也是需要在Spring的配置文件中进行配置的。
<!--Spring中bean获取的工具类-->
<bean id="springContextUtils" class="com.zker.common.util.SpringContextUtils" />
2
 
1
<!--Spring中bean获取的工具类-->
2
<bean id="springContextUtils" class="com.zker.common.util.SpringContextUtils" />

3、编写方法进行使用

一切就绪,我们就可以在需要使用的地方调用该方法来获取bean了。
    /**
* 利用Ajax实现注册的用户名重复性校验
* @return
*/
public String ajaxRegister() throws IOException {
UserDao userDao = (UserDao)SpringContextUtils.getContext().getBean("userDao");
if (userDao.findAdminByLoginName(loginName) != null
|| userDao.findUserByLoginName(loginName) != null) {
message.setMsg("用户名已存在");
message.setStatus(false);
} else {
message.setMsg("用户名可以注册");
message.setStatus(true);
} return "register";
}
17
 
1
    /**
2
     * 利用Ajax实现注册的用户名重复性校验
3
     * @return
4
     */
5
    public String ajaxRegister() throws IOException {
6
        UserDao userDao = (UserDao)SpringContextUtils.getContext().getBean("userDao");
7
        if (userDao.findAdminByLoginName(loginName) != null
8
                || userDao.findUserByLoginName(loginName) != null) {
9
            message.setMsg("用户名已存在");
10
            message.setStatus(false);
11
        } else {
12
            message.setMsg("用户名可以注册");
13
            message.setStatus(true);
14
        }
15

16
        return "register";
17
    }


参考链接



如何手动获取Spring容器中的bean(ApplicationContextAware 接口)的更多相关文章

  1. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

  2. 获取Spring容器中的Bean协助调试

    在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...

  3. 获取Spring容器中的Bean

    摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...

  4. [十]SpringBoot 之 普通类获取Spring容器中的bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

  5. Spring Boot中普通类获取Spring容器中的Bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...

  6. SpringBoot之普通类获取Spring容器中的bean

    package com.geostar.geostack.git_branch_manager.common; import org.springframework.beans.BeansExcept ...

  7. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  8. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  9. java web中如何获取spring容器中定义的bean----WebApplicationContext的使用

    本文简单编写一个servlet来获取spring容器中管理的<bean  id="dateBean" class="java.util.Date" sin ...

随机推荐

  1. Wincc flexable的按钮组态

    1.题目 2.画面切换按钮组态 1)新建变量 2)组态画面进行命名 3)组态按钮常规 4)组态按钮属性 5)在事件中选择单击,系统函数中选择画面中的ActivateScreen函数切换画面 3.给变量 ...

  2. Linux学习总结(十二)—— CentOS用户管理:创建用户、修改用户、修改密码、密码有效期、禁用账户、解锁账户、删除用户、查看所有用户信息

    文章首发于Linux学习总结(十二)-- CentOS用户管理,请尊重原创保留原文链接. 创建用户 useradd -g webadmin -d /home/zhangsan zhangsan pas ...

  3. easyui+ztree 后台管理系统模板

    用easyui+ztree做了个后台管理系统模板,效果图: 下载地址: csdn:http://download.csdn.net/detail/jackpay/6744505 github:http ...

  4. eclipse的格式化规则(即format.xml文件).

    eclipse的格式化规则(即format.xml文件) 下面是文件内容 <?xml version="1.0" encoding="UTF-8" sta ...

  5. SpringMVC详解(六)------与json交互

    Json(JavaScript Object Notation),它是一种轻量级数据交换格式,格式简单,易于读写,目前使用特别广泛.那么这篇博客我们主要谈谈在 SpringMVC 中,如何对 json ...

  6. C# 逆变与协变

    该文章中使用了较多的 委托delegate和Lambda表达式,如果你并不熟悉这些,请查看我的文章<委托与匿名委托>.<匿名委托与Lambda表达式>以便帮你建立完整的知识体系 ...

  7. 我的前端故事----我为什么用GraphQL

    背景 今年我在做一个有关商户的app,这是一个包含商户从入网到审核.从驳回提交到入网维护的完整的生命周期线下推广人员使用的客户端软件,但故事并没有这么简单... 疑问 随着app的逐渐完善,遇到的问题 ...

  8. html复选框

    1.添加复选框 <input type="checkbox" id="yc" name="yc" value="year&q ...

  9. JAVA实用案例之文件导入导出(POI方式)

    1.介绍 java实现文件的导入导出数据库,目前在大部分系统中是比较常见的功能了,今天写个小demo来理解其原理,没接触过的同学也可以看看参考下. 目前我所接触过的导入导出技术主要有POI和iRepo ...

  10. SynchronizedMap和ConcurrentHashMap 区别

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt215 SynchronizedMap和ConcurrentHashMap的深 ...