springcontext数组(详解springboot设计模式)

来源:国外服务器 在您之前已被浏览:1 次
导读:目前正在解读《springcontext数组(详解springboot设计模式)》的相关信息,《springcontext数组(详解springboot设计模式)》是由用户自行发布的知识型内容!下面请观看由(国外主机 - www.2bp.net)用户发布《springcontext数组(详解springboot设计模式)》的详细说明。
笨笨网美国主机,w ww.2 b p .n e t

在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所管理的,如果直接在所依赖的属性上使用@Autowired就会报无法注入的错误,或者是没报错,但是使用的时候会报空指针异常。总而言之由于其是不受Spring IoC容器所管理的,因而无法注入。

springcontext数组(详解springboot设计模式)

Spring的核心是ApplicationContext,它负责管理 beans 的完整生命周期。我们可以从applicationContext里通过bean名称获取注入的bean,然后进行操作。

springcontext数组(详解springboot设计模式)

SpringContextUtil工具类的代码如下所示:

package com.rickie.util;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

public class SpringContextUtil {

private static ApplicationContext applicationContext;

// 设置上下文

public static void setApplicationContext(ApplicationContext context) throws BeansException {

applicationContext = context;

}

// 获取上下文

public static ApplicationContext getApplicationContext() {

return applicationContext;

}

// 通过名字获取上下文中的bean

public static Object getBean(String name) {

if(name == null || name.length()==0) {

return null;

}

try {

String beanName = “”;

if(name.length() >1){

beanName = name.substring(0, 1).toLowerCase() + name.substring(1);

} else {

beanName = name.toLowerCase();

}

return applicationContext.getBean(beanName);

} catch (Exception ex){

ex.printStackTrace();

return null;

}

}

// 通过类型获取上下文中的bean

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

try {

return (T) applicationContext.getBean(clazz);

} catch(Exception ex) {

ex.printStackTrace();

return null;

}

}

}

由于该类并没有实现ApplicationContextAware接口,因此先设置好ApplicationContext的值。可以在Spring Boot的启动方法main中进行设置:

@SpringBootApplication

public class Application {

public static void main(String[] args) {

ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);

SpringContextUtil.setApplicationContext(ctx);

}

}

在Spring Boot的启动时调用的run方法会返回一个
ConfigurableApplicationContext,将其设置到SpringContextUtil的静态属性中,然后能够通过ApplicationContext对象获取到需要使用的bean,这样就可以使用了。

springcontext数组(详解springboot设计模式)

下面是一段使用SpringContextUtil工具类的示例代码:

/**

* 创建一个新的客户

* @param customer

* @return

*/

public int create(Customer customer) {

if(this.customerGateway == null) {

this.customerGateway = SpringContextUtil.getBean(CustomerGateway.class);

}

return customerGateway.insert(customer);

}

这样,无论是在静态方法中,还是在手动new的实例中,我们都能够成功获取IoC容器所管理的bean。如果我们想在静态属性中获取Spring Bean,其实也非常简单,直接对属性赋值即可,如下所示:

private static Customer customer = SpringBeanUtil.getBean(Customer.class);

springcontext数组(详解springboot设计模式)

Spring Cloud Alibaba微服务实战技术专栏,从项目实践出发,包括Spring Cloud Alibaba、Nacos、Gateway、Sentinel、Log日志、分布式全局唯一ID、DDD领域驱动设计等等技术内容,可帮助你对Spring Cloud 微服务技术栈有更加全面和直观的了解。相信你通过本专栏的练习和实践,能够学以致用,提升微服务应用的开发能力。

笨笨网美国主机,w ww.2 b p .n e t
提醒:《springcontext数组(详解springboot设计模式)》最后刷新时间 2025-03-21 11:17:48,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《springcontext数组(详解springboot设计模式)》该内容的真实性请自行鉴别。