如何在线程中获取spring 管理的bean

如何在线程中获取spring 管理的bean,第1张

只要搜一下“获取 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;

    }

}

通用的方法来了,神器啊,前的 1、2两种方法并不通用,可以抛弃了。

在配置文件中加入:

Xml代码

<!-- 用于持有ApplicationContext,可以使用SpringContextHoldergetBean('xxxx')的静态方法得到spring bean对象 -->

<bean class="comxxxxxSpringContextHolder" lazy-init="false" />

Java代码

import orgspringframeworkcontextApplicationContext;

import orgspringframeworkcontextApplicationContextAware;

/

以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext

/

public class SpringContextHolder implements ApplicationContextAware {

private static ApplicationContext applicationContext;

/

实现ApplicationContextAware接口的context注入函数, 将其存入静态变量

/

public void setApplicationContext(ApplicationContext applicationContext) {

SpringContextHolderapplicationContext = applicationContext; // NOSONAR

}

/

取得存储在静态变量中的ApplicationContext

/

public static ApplicationContext getApplicationContext() {

checkApplicationContext();

return applicationContext;

}

/

从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型

/

@SuppressWarnings("unchecked")

public static <T> T getBean(String name) {

checkApplicationContext();

return (T) applicationContextgetBean(name);

}

/

从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型

/

@SuppressWarnings("unchecked")

public static <T> T getBean(Class<T> clazz) {

checkApplicationContext();

return (T) applicationContextgetBeansOfType(clazz);

}

/

清除applicationContext静态变量

/

public static void cleanApplicationContext() {

applicationContext = null;

}

private static void checkApplicationContext() {

if (applicationContext == null) {

throw new IllegalStateException("applicaitonContext未注入,请在applicationContextxml中定义SpringContextHolder");

}

}

}

你先找到标注了@SpringBootApplication的主类,注意看它的package。这个@SpringBootApplication会声明让Spring去扫描该package里以及所有子package里的类,如果扫到的类标注有@Component/@Controller/@Service/@Repository,那就把它加入Spring容器,这样你在其他任何地方使用@Autowired标注就能自动从Spring容器里把这个类找出来并注入进去直接使用。

package comdemo;

@SpringBootApplication

public class Application {

//

}

以上就是关于如何在线程中获取spring 管理的bean全部的内容,包括:如何在线程中获取spring 管理的bean、如何取得Spring管理的bean、SpringBoot里@autowired的Bean是从哪儿获得的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9455387.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存