//      ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","spring-xxxxx.xml"});
// BeanFactory factory = (BeanFactory)context;
// MonitorServer ce = (MonitorServer) factory.getBean("monitorServer");

类似于上面的这种,通过种种方式得到测试中都遇到资源耗尽的严重bug,

于是找高手请教,得到如下解决方法:而且效率很高,很易用。下面是代码L

package org.company.xxx;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
*
* 获取spring容器,以访问容器中定义的其他bean
* @author lyltiger
* @since MOSTsView 3.0 2009-11-16
*/
public class SpringContextUtil implements ApplicationContextAware { // Spring应用上下文环境
private static ApplicationContext applicationContext; /**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*
* @param applicationContext
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext;
} /**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} /**
* 获取对象
* 这里重写了bean方法,起主要作用
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
} }

z只要将其类放在工程中的合适位置,直接用类名使用,无论何时何地……

使用:

java类中:

MonitorServer md = (MonitorServer)SpringContextUtil.getBean("monitorServer");//bean的名称

spring配置文件中:

<bean id="monitorServer"
class="com.company.xxxxx.MonitorServer">
<property name="dataSource" ref="dataSource" />
</bean>

这样就可以得到我们想要的bean了,里面的方法随意调用,得到我们想要的数据。
OK!

sping获取bean方法 解决资源耗尽的更多相关文章

  1. JAVA中SpringMVC获取bean方法,在工具类(utils)注入service

    有时候我们会出现无法用注解 @Autowired 注入bean的情况,这个时候可以 通过contextLoader获取 WebApplicationContext ctx = ContextLoade ...

  2. JSF页面中使用js函数回调后台bean方法并获取返回值的方法

    由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的, ...

  3. [转]Android 如何根据网络地址获取网络图片方法

    http://blog.csdn.net/xiazdong/article/details/7724103 目录(?)[-] h2pre namecode classhtml stylefont-we ...

  4. Spring获取bean的几种方式

    工作中需要对一个原本加载属性文件的工具类修改成对数据库的操作当然,ado层已经写好,但是需要从Spring中获取bean,然而,工具类并没有交给Spring来管理,所以需要通过方法获取所需要的bean ...

  5. VMware桥接模式无法自动化获取IP的解决方法

    虚拟机桥接无法自动获取IP的解决方法 在虚拟机VM里面装了centos系统,网卡选用桥接方式. 刚开始的时候还能自动获取到IP地址,突然有一天IP消失了,再怎么重启都无法获取IP地址.因为之前是可以获 ...

  6. Spring自动扫描无法扫描jar包中bean的解决方法(转)

    转载自:http://www.jb51.net/article/116357.htm 在日常开发中往往会对公共的模块打包发布,然后调用公共包的内容.然而,最近对公司的公共模块进行整理发布后.sprin ...

  7. SpringBoot拦截器中无法注入bean的解决方法

    SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...

  8. 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值

    7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...

  9. 【我的Android进阶之旅】Android使用getIdentifier()方法根据资源名来获取资源id

    有时候我们想动态的根据一个资源名获得到对应的资源id,就可以使用getResources().getIdentifier()方法来获取该id.然后再使用该id进行相关的操作. 1.Demo示例 下面用 ...

随机推荐

  1. WCF 消息压缩性能问题及解决方法

    最近使用WCF作为通迅框架开发一套信息系统,系统使用传统C/S框架,系统有可能会部署在互联网上,因此决定对传输的数据进行GZIP压缩,原来在使用.NET Remoting时,可以使用插入自定义的Cha ...

  2. routes.IgnoreRoute("{resource}.axd/{*pathInfo}")作用

    {resource}.axd 表示后缀名为.axd所有资源 如webresource.axd{*pathInfo} 表示所有路径 作用:使路由系统忽略处理ASP.NET的Web资源文件(WebReso ...

  3. [topcoder]TallPeople

    水题.http://community.topcoder.com/stat?c=problem_statement&pm=2923&rd=5854 一开始错了是因为理解错题意.还有就是 ...

  4. 【HDOJ】1857 Word Puzzle

    trie树.以puzzle做trie树内存不够,从puzzle中直接找串应该会TLE.其实可以将查询组成trie树,离线做.扫描puzzle时注意仅三个方向即可. /* 1857 */ #includ ...

  5. 【HDOJ】2065 "红色病毒"问题

    刚开始看这道题目的时候,完全没看出来是递推.看了网上大牛的分析.立刻就明白了.其实无论字符串长度为多少,都可以将该长度下的组合分成四种情况S1(A偶数C偶数).S2(A偶数C奇数).S3(A奇数C偶数 ...

  6. Item Import: What Does "Sync" Items Do? (Doc ID 417887.1)

    In this Document Goal Solution APPLIES TO: Oracle Item Master - Version 11.5.10.0 to 11.5.10.0 [Rele ...

  7. HDU4739Zhuge Liang's Mines(状压)

    链接 预处理出只有四个1的情况存入数组中 然后状压下 #include <iostream> #include<cstdio> #include<cstring> ...

  8. 函数lock_mode_stronger_or_eq 锁权限等级

    row代表lock HashTable的权限 column代表预加锁的权限    ulint lock_mode_stronger_or_eq( /*=====================*/ e ...

  9. How to Set Word Document Properties with C#

    Word properties shows a brief description about one document. Through properties, we can learn gener ...

  10. selenium2.0的初步封装(java版本)

    我们都知道, 在本地创建java项目后,引入selenium-java-2.35.0.jar   selenium-support-2.35.0.jar junit-4.8.1.jar等等jar包之后 ...