java 获取spring配置文件中bean 对象

Posted Django_1987

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 获取spring配置文件中bean 对象相关的知识,希望对你有一定的参考价值。

编写工具类:

package com.thunisoft.webservice.service.util;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class getBean implements ApplicationContextAware

private static ApplicationContext  app;

public void setApplicationContext(ApplicationContext applicationContext)  throws BeanException

this.app = applicationContext;

public static ApplicationContext getYouBean()

return app;


applicationContext 配置文件:

<bean id = "applicationContext" class="com.thunisoft.webservice.service.util.getBean"> 


操作类:

可以写在方法的构造方法中。

public test()

try

ApplicationContext  app = getBean.getYouBean();

fydao=(IFydao)app.getBean("fydao");

catch(Exception e)

log.error("工具类获取application中bean对象失败!",e);


以上是关于java 获取spring配置文件中bean 对象的主要内容,如果未能解决你的问题,请参考以下文章