只要搜一下“获取 spring bean”,获取spring bean的N中方法都出来了。线程中获取和普通类中获取方法是一样的。
下面是一种方法。这个类需要配置在Spring中。
使用的时候直接:Bean bean = SpringUtilgetBean("bean的id", Beanclass);//Beanclass是bean的类对象,比如获取 UserService的bean,就是:
UserService us = SpringUtilgetBean("userService", UserServiceclass);import orgspringframeworkbeansBeansException;import orgspringframeworkcontextApplicationContext;
import orgspringframeworkcontextApplicationContextAware;
/
Spring IOC上下文工具类
@ClassName: SpringUtil
@Description:
@author
@date 2014-6-21 下午02:06:48
/
public class SpringUtil implements ApplicationContextAware {
/
当前IOC
/
private static ApplicationContext applicationContext;
/
设置当前上下文环境,此方法由spring自动装配
/
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
applicationContext = arg0;
}
/
从当前IOC获取bean
@param id
bean的id
@return
/
public static <T> T getBean(String id, Class<T> requiredType) {
T t = applicationContextgetBean(id, requiredType);
return t;
}
}
第一种方式:FileSystemXmlApplicationContext通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例:ApplicationContextac=newFileSystemXmlApplicationContext(applicationContextxml)acgetBean(beanName);第二种方式:WebApplicationContextUtil在B/S系统中,通常在webxml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext例如:ApplicationContextctx=WebApplicationContextUtilsgetRequiredWebApplicationContext(ServletContextsc);ApplicationContextctx=WebApplicationContextUtilsgetWebApplicationContext(ServletContextsc);其中servletContext sc 可以具体 换成 servletgetServletContext()或者 thisgetServletContext()或者requestgetSession()getServletContext();另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 对象:WebApplicationContext webApplicationContext = (WebApplicationContext) servletContextgetAttribute(WebApplicationContextROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);下面几种方式没有用过,
1,,pronouncedpronounced2,realizedrealized3,,matteredmattered4,,impressedimpressed5,,dealtdealt6,,solvedsolved7,regardedregarded8influencedinfluenced9,,lostlost10,,facedfaced11,,terrifiedterrified12,chewedchewed13,causedcaused14wastedwasted15m,,concentratedconcentrated16,,designeddesigned17,,,volunteeredvolunteered~~~~~~18experiencedexperienced19,,,repliedreplied20,obeyedobeyed21,,achievedachieved22,,raced,,raced,23,succeededsucceeded24,botheredbothered25,,annoyedannoyed26,representedrepresented27,,coveredcovered28,,pressedpressed29,,burntburnt30,,hurthurt(原形)))))31,,offeredoffered32,,treatedtreated,33,,chasedchased34,,interviewedinterviewed35,,escaped,,escaped,
1添加spring获取上下文和bean方法的工具类(ApplicationContextUtil)
@Component
public class ApplicationContextUtil implements ApplicationContext{
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context){
ApplicationContextUtilcontext = context;
}
public static ApplicationContext getContext(){
return context;
}
}
2在静态方法类中通过工具类获取bean内容(获取的为bean内容,也可以实例化对象,但是获取不到bean内容)
Object obj = (Objectclass)ApplicationContextUtilgetContext()getBean("bean的名字");
以上就是关于如何在线程中获取spring 管理的bean全部的内容,包括:如何在线程中获取spring 管理的bean、Spring框架下获取Bean的几种方式、怎样获得spring中的ApplicationContext对象等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)