SpringBoot普通类调用Bean对象 SpringBoot 普通类调用Bean对象的一种方式推荐

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot普通类调用Bean对象 SpringBoot 普通类调用Bean对象的一种方式推荐

morro_c137   2021-11-16 我要评论
想了解SpringBoot 普通类调用Bean对象的一种方式推荐的相关内容吗,morro_c137在本文为您仔细讲解SpringBoot普通类调用Bean对象的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot普通类,普通类调用Bean,调用Bean对象,下面大家一起来学习吧。

SpringBoot 普通类调用Bean对象

有时我们有一些特殊的需要,可能要在一个不被Spring管理的普通类中去调用Spring管理的bean对象的一些方法,比如一般SpringMVC工程在controller中通过

@Autowired
private TestService testService;

注入TestService 接口就可以调用此接口实现类的实现的方法。

但在一般类中显然不可以这么做,注入的 TestService 将会报空指针异常,你无法拿到这个bean,在一般的ssm工程中我们可以通过xml配置把普通类设置成一个bean对象,那么 TestService 就有效了, 或者使用 ApplicationContext 直接读取xml配置中的bean也可以拿到 TestService。`

Spring Boot 已经摒弃了各种繁琐的xml配置,当然就不再使用xml配置的方式,之前在网上看到一种很简便的方式,但现在又找不到链接了,这里做下记录。

在普通类中定义 ApplicationContext 静态变量和set方法

    private static ApplicationContext applicationContext;//启动类set入,调用下面set方法
    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

在启动类中,启动时事实已经生成 ConfigurableApplicationContext 对象, ConfigurableApplicationContext 是 ApplicationContext 接口的实现,这里直接传到普通类的 setApplicationContext 方法就行了

@SpringBootApplication
@ServletComponentScan
public class WxApplication implements EmbeddedServletContainerCustomizer{
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(WxApplication.class, args);
        TestClass.setApplicationContext(applicationContext);
    }
}

由于是静态变量,类加载时 applicationContext 已经存在,就可获取到 TestService 了,唯一不好就是静态变量在服务器启动后将一直存在

public class TestClass {
    private static ApplicationContext applicationContext;//启动类set入,调用下面set方法
    public static void setApplicationContext(ApplicationContext context) {
        // TODO Auto-generated method stub
        applicationContext = context;
    }
    public void getBeanTest(){
        TestService testService  = (TestService)applicationContext.getBean(TestService.class);
    }
}

补充:

在普通 Spring 工程在启动的时候都会通过 org.springframework.web.context.ContextLoaderListener 监听器从加载系统资源并管理bean, Spring 提供的 WebApplicationContextUtils 工具类能在请求时获取到运行时工程的bean,如果看源码就可以知道监听器执行时与 WebApplicationContextUtils 类的关联

//封装一下,类的class和请求request为必要参数
public static <T> T getBean(Class<? extends Object> cla,HttpServletRequest request){
        if(request == null){
            return null;
        }
        return (T)WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean(cla);//getBean参数可为bean类的.class或直接是bean的Id
    }
//这样获取bean
TestService testService= (TestService)getBean(TestService.class, request);

SpringBoot 中bean的使用

通常定义bean的方式有三种,注解、xml文件中定义等

但是在采用注解形式定义bean的时候,如果我们没有为bean指定名字,那么spring本身也会为bean指定一个默认的名字,名字命名规则如下:

1、如果类的前两个字母都是大写的话,那么bean的名称就是类的名称。比如类的名称是BEan,那么bean的名称就是BEan.

2、如果类名只是首字母大写,那么bean的名称,就会成为首字母小写的。比如类的名称是Bean,那么bean的名称就是bean

Springboot中bean的使用

很多时候,我们在开发spring boot等相关的spring的web项目时候,使用bean的方式有几种:

1、通过autowired字段来引入bean,这样可以使用bean

2、在程序中,通过spring的ApplicationContext获取某一个bean来做操作

  • @Autowired
  • Bean bean;

针对于第一种,我们形式比较常见,通常autowired这种方式引用bean后,直接通过对象名bean就可以调用对应的方法了

针对于第二种,一般就是直接通过bean名称调用了

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们