当前位置:网站首页>上下文对象实例
上下文对象实例
2022-07-15 22:45:00 【斯普润布特】
上下文对象实例
package com.common.support;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
/**
* 上下文对象实例 获取对象
*/
@Component
public class ApplicationContextProvider
implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextProvider.applicationContext = applicationContext;
}
/**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
/**
* 描述 : <获得多语言的资源内容>. <br>
* <p>
* <使用方法说明>
* </p>
*
* @param code
* @param args
* @return
*/
public static String getMessage(String code, Object[] args) {
return getApplicationContext().getMessage(code, args, LocaleContextHolder.getLocale());
}
/**
* 描述 : <获得多语言的资源内容>. <br>
* <p>
* <使用方法说明>
* </p>
*
* @param code
* @param args
* @param defaultMessage
* @return
*/
public static String getMessage(String code, Object[] args,
String defaultMessage) {
return getApplicationContext().getMessage(code, args, defaultMessage,
LocaleContextHolder.getLocale());
}
}
引用方式
// server name 与 service 上的注解保持一致 @Service(value="demoService")
final ICountryService countryService = (DemoService) ApplicationContextProvider.getBean("demoService");边栏推荐
- 超好用的截图软件Snipaste(包含安装包)、如何设置Snipaste开机自启
- 力扣练习——17 任务调度器
- 五种常见的加密方式及常用的加解密工具
- 力扣练习——23 救生艇
- How to judge the quality of an ERP management system
- Record the troubleshooting experience of a pit father memory leak
- P1085 [NOIP2004 普及组第一题] 不高兴的津津 ← 模拟题
- 力扣练习——20 接雨水 II
- Seven uncovered industrial 4.0 misunderstandings
- Why is everyone keen on taking soft exams? Here's the reason
猜你喜欢

关于线程切换问题的一些思考总结

带你刷(牛客网)C语言百题(第二天)
![[figure 1 understand RTC] comprehensive upgrade of the underlying framework of audio and video](/img/69/eebbd5dbac4d8d4e045664a31641a0.png)
[figure 1 understand RTC] comprehensive upgrade of the underlying framework of audio and video

Kbpc2510w-asemi welding machine special rectifier bridge kbpc2510w
Zabbix+mysql fpmmm (MPM) monitoring

Heartless sword Chinese translation of Michael's definition of algebra

Flume 数据采集

Ethernet development and testing, have you done this step right (3)

超好用的截图软件Snipaste(包含安装包)、如何设置Snipaste开机自启

(手工)【sqli-labs42、43】POST注入、堆叠注入、错误回显、字符型注入
随机推荐
移动布局 -适配
Internet worm
漫谈软件缺陷管理的价值
LeetCode_112_路径总和
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Can SQL also do AI? you 're right! Mlops meetup V3 review openmlbd+sqlflow+byzer
LeetCode_ 112_ Path sum
想学硬件,该学什么啊?
Advanced process: the independence of programmers to do projects
Ethernet development and testing, have you done this step right (1)
开发人员使用Helix QAC实现静态代码测试合规性的五大原因
Tagger: Deep Unsupervised Perceptual Grouping
[2023 approval in advance] Haier
ACL access control list case (7.15)
codeforces每日5题(均1500)-第十六天
五种常见的加密方式及常用的加解密工具
问题 V: hannnnah_j’s Biological Test
时间模块之datatime模块、os模块、sys模块、json模块、json模块实操
Sqlserver remote update timeout through linked server
IDEA - Could not autowire. No beans of ‘XXXMapper‘ type found.