技术标签: spring 工具类 容器 Spring 学习笔记 Spring bean 获取
package org.jiankunking.utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
/**
* Created by jiankunking on 2016/12/22.
*/
public class SpringBeanUtils implements BeanFactoryAware {
private static BeanFactory beanFactory;
/**
* 注入BeanFactory实例
*/
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
SpringBeanUtils.beanFactory = beanFactory;
}
/**
* 根据bean的名称获取相应类型的对象
*
* @param beanName bean的名称
* @return Object类型的对象
*/
public static Object getBeanByName(String beanName) {
return beanFactory.getBean(beanName);
}
/**
* 根据bean的类型获取相应类型的对象
*
* @param clazz bean的类型,没有使用泛型
* @return Object类型的对象
*/
public static Object getBeanByClass(Class clazz) {
WebApplicationContext wac = ContextLoader
.getCurrentWebApplicationContext();
Object bean = wac.getBean(clazz);
return bean;
}
/**
* 根据bean的名称获取相应类型的对象
*
* @param clazz bean的类型,使用泛型
* @return T类型的对象
*/
public static <T> T getGenericsBeanByClass(Class<T> clazz) {
WebApplicationContext wac = ContextLoader
.getCurrentWebApplicationContext();
T bean = wac.getBean(clazz);
return bean;
}
}
在spring.xml中添加以下部分,已获取工具类bean实例:
<bean id="springBeanUtils" class="org.jiankunking.utils.SpringBeanUtils"/>
package org.jiankunking.utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.DefaultResourceLoader;
import java.io.IOException;
/**
* Spring的工具类,用来获得Spring中的bean
*/
public class SpringBeanUtils implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringBeanUtils.applicationContext = applicationContext; // NOSONAR
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBean(clazz);
}
/***
* 类似于getBean(String name)只是在参数中提供了需要返回到的类型。
*
* @param name
* @param requiredType
* @return
* @throws BeansException
*/
public static <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
*
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 清除applicationContext静态变量.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义UtilsSpringContext");
}
}
public static String getRootRealPath() {
String rootRealPath = "";
try {
rootRealPath = getApplicationContext().getResource("").getFile().getAbsolutePath();
} catch (IOException e) {
//获取系统根目录失败
}
return rootRealPath;
}
public static String getResourceRootRealPath() {
String rootRealPath = "";
try {
rootRealPath = new DefaultResourceLoader().getResource("").getFile().getAbsolutePath();
} catch (IOException e) {
//获取系统根目录失败
}
return rootRealPath;
}
@Override
public void destroy() throws Exception {
SpringBeanUtils.cleanApplicationContext();
}
}
在spring.xml中添加以下部分,已获取工具类bean实例:
<bean id="springBeanUtils" class="org.jiankunking.utils.SpringBeanUtils"/>
本文参考以下两篇文章:
http://penghuaiyi.iteye.com/blog/2042296
http://www.tuicool.com/articles/numyuq
作者:jiankunking 出处:http://blog.csdn.net/jiankunking
文章浏览阅读105次。php的安装见这里,写得不错。大致如下:sudo apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql当前的虚拟机是12.04的系统,默认的PHP只有5.3.x,因为想用Trait的功能,所以就想升级了。结果。。。死活升不上。于是找了点资料,发现只要三句话:(cyper笔记:这三句话有点问题见后文) su..._apt php5.4
文章浏览阅读348次。一.sql的分类DML:数据操纵语言 date manipulation language,语法包括:select、insert、delete、update记忆方法:使用sql操作数据的增删改查,对数据产生改变,对表不会产生改变DDL:数据定义语言 date definition language语法包括:alter,create、drop、rename truncate记忆方法:只会对表产生改变,对数据不会产生改变DCL:数据控制语言 date control language_dcl 中的reoke
文章浏览阅读379次。layui.define(['treeTable', 'layer', 'code', 'element', 'form', 'jquery', 'http','formUtils'], function (exports) { var o = layui.$, form = layui.form, layer = layui.layer, ..._conbogrid分页选择多条数据进行回填
文章浏览阅读6.9k次。Android的DHCP由两个独立的程序:dhcpcd和netcfg。1)dhcpcd位于external\dhcpcd目录下,在系统中以service的方式存在,负责通过dhcp协议为Android系统获取IP地址。上层应用通过对dhcpcd service的操作来实现DHCP功能。其调用流程大体如下:runDhcp(java) -- android_net_utils_runDhc_android init.rc dhcp
文章浏览阅读5.8k次,点赞5次,收藏15次。关于GOP中IDR帧的说明:1、在H.264中一个图像组的起始帧一定是IDR帧,因此在H.264中在处理一个GOP之前会把参考图像集清空,因此一个GOP中的帧一定不会参考另一个GOP的帧——这就是封闭式GOP。2、在HEVC中,一个GOP的第一帧默认不是IDR帧,而是普通的I帧,在处理一个GOP之前,编码器不会把参考集清空,因此在HEVC中一个GOP是会参考另一个GOP的数据,造成了GOP_h264 gop和i帧周期
文章浏览阅读530次。autodl使用记录_xshell ssh命令
文章浏览阅读1.1k次。最近在公司接手新项目,需要在mac电脑上全局安装gulp。正常来说全局安装gulp:npminstall-ggulp结果报错:EACCES:permissiondenied,mkdir'/usr/local/lib/node_modules/gulp/node_module...类似这种错误的原因是执行命令时没有获得管理员权限解决方法:在命令前面加上su..._mac 安装 gulp
文章浏览阅读2.5k次。在我们所使用的电子网络工具中,其中电脑是最受人们的喜爱的,而用过电脑的人都会了解一下电脑光驱的功能,并且会需要知道光驱启动的设置是如何做到的,小编今天的文章就跟电脑光驱相关联,既然很多用户都想知道电脑光驱如何设置启动,小编今天的文章就来教会大家,电脑如何设计光驱启动的方法,其实这些方法都非常的简单,如何简单呢?就看小编的文章来告诉大家吧。电脑要设置光驱启动,那么首先人们就要想到在blos中设置,小..._电脑如何设置光驱启动
文章浏览阅读1.1k次。提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档maptalks+VUE 开发(2)-- 绘制多边形一、效果图二、使用步骤1.引入2.创建VectorLayer(渲染层)一、效果图二、使用步骤1.引入参照上篇文章2.创建VectorLayer(渲染层)代码如下(示例):var setStyle = []; // 用来set对应的图形颜色var layer = new maptalks.VectorLayer("vector") .setStyle({_new maptalks.vectorlayer
文章浏览阅读1k次。/** * 找硬币问题。有1元、2元、5元三种面值的硬币,求找零N元,最少要找多少张零钱。 * @author zll * @version 1.0 * @date 2020/6/5 9:54 */public class test01 { public static void main(String[] args) { int[] arr = {1, 2, 5}; for(int i=-3;i<=30;i++){ int n_假设可使用的零钱面值分别为1元、2元、5元,从键盘输入找零总额n,编程找出所有
文章浏览阅读87次。[#1]neugeypc at cox dot net [2002-05-31 11:24:33]Thesybase_affected_rows()functionseemstoworknowwiththeSybaseDBLibrary.Notsurewhenthisimprovementwasmade,butI'mcurrentlyrunningP..._php sybase 中文乱码
文章浏览阅读43次。在linux系统下安装mysql详解,以及远程调用连接不上mysql的解决方法。_通过linux可以访问mysql但是部署的程序不能连接mysql