如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
ApplicationContextAware 接口的作用

如何使用 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;
}
}
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;
}
}
2、在Spring配置文件中注册该工具类
<!--Spring中bean获取的工具类-->
<bean id="springContextUtils" class="com.zker.common.util.SpringContextUtils" />
<!--Spring中bean获取的工具类-->
<bean id="springContextUtils" class="com.zker.common.util.SpringContextUtils" />
3、编写方法进行使用
/**
* 利用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";
}
/**
* 利用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";
}
参考链接
如何手动获取Spring容器中的bean(ApplicationContextAware 接口)的更多相关文章
- SpringBoot 之 普通类获取Spring容器中的bean
[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...
- 获取Spring容器中的Bean协助调试
在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...
- 获取Spring容器中的Bean
摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...
- [十]SpringBoot 之 普通类获取Spring容器中的bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...
- Spring Boot中普通类获取Spring容器中的Bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...
- SpringBoot之普通类获取Spring容器中的bean
package com.geostar.geostack.git_branch_manager.common; import org.springframework.beans.BeansExcept ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...
- java web中如何获取spring容器中定义的bean----WebApplicationContext的使用
本文简单编写一个servlet来获取spring容器中管理的<bean id="dateBean" class="java.util.Date" sin ...
随机推荐
- python自动化运维五:pexpect
p { margin-bottom: 0.25cm; line-height: 120% } a:link { } pexpect是expect的封装,可以实现ssh,ftp,passwd,telne ...
- jdbc电话本项目
整体思路:在登陆之后才能查看自己的电话本,电话本中包含用户名,联系人名字,电话,性别,分类: 1.登陆注册页面--数据库User表,注册登陆使用 2.电话本的前段显示,用表格和表单, 3.创建存取的电 ...
- 师兄写的一个JAVA播放器的源代码(转)
师兄写的一个JAVA播放器的源代码 MediaPlayer.java------------------------------------------------------------------ ...
- spring boot 登录注册 demo (一)
Welcome to Spring Boot 代码结构 src/main/java 下 controller层,路由功能dao层,数据库的访问domain,bean的存放service,业务层appl ...
- 【Ubuntu 16】源码包安装Apache Httpd
源码包安装适合初学者使用. 背景信息 APR (Apache Portable Runtime) Apache可移植运行的任务(APR)项目是创建和维护软件库,为底层平台的具体实现提供了一个可预见的和 ...
- Windows常用的一些DOS命令整理
Windows常用的一些DOS命令整理.. MS DOS 命令大全 ---清空Dos屏幕使用命令:cls -- Dos切换当前目录到D盘:cd d: 一.基础命令 1 dir 无参数:查看当前所在目录 ...
- Markdown简单教程
前言:大家好 我是阿飞~ 今天给大家推荐一个不错的东西 MarkDown 今后的潮流就是用它来写readme文件 具体好处多多 就把它理解为可以快速设置样式的txt文档吧~ 标题 # 每一级标题通过# ...
- ABP+AdminLTE+Bootstrap Table权限管理系统第二节--在ABP的基础做数据库脚本处理
返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 第一点,上一篇文章中我们讲到codefirst中一些问题包括如图,codefirst在每次执行命令的时候会生成新的 ...
- Andrew Ng机器学习课程笔记--week9(上)(异常检测&推荐系统)
本周内容较多,故分为上下两篇文章. 一.内容概要 1. Anomaly Detection Density Estimation Problem Motivation Gaussian Distrib ...
- 使用 qemu 搭建内核开发环境
本文主要介绍在 MacOS 上使用 qemu 搭建 Linux Kernel 的开发环境.(在开始之前需要注意的是,本文中的 Linux 开发环境是一个远程服务器,而 qemu 被安装在本地的 Mac ...