当前位置:网站首页>ApplicationContext injection bean
ApplicationContext injection bean
2022-04-23 04:43:00 【Mount MONI】
Usually we inject... In the following ways
1、@Autowired It's through byType The way to inject , Use this annotation , The interface is required to have only one implementation class .
2、@Resource Can pass byName and byType Way of injection , By default, press first byName The method of matching , If it doesn't match , Press again byType The method of matching .
3、@Qualifier Annotations can be injected by name , But note that Class name .
In some cases, we need to be in the tool class or in new After a thread , Thread injection Service Layer or Dao layer , At this time, the above method can not be injected
package com.java.base.config;
import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
/***
*
* <p>Title: MyApplicationContext</p>
* <p>Description: ApplicationContextAware Through it Spring The container will automatically call the context object ApplicationContextAware Interface setApplicationContext Method .
* From this context object Spring In container Bean
* In the tool class we write, we can't get the thread directly through Spring Inject , This is the time to pass ApplicationContext obtain Bean
* </p>
* @author shy
*/
@Service
public class MyApplicationContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T getBean(final Class<T> requiredType) {
return context.getBean(requiredType);
}
public static <T> T getBean(final String beanName) {
@SuppressWarnings("unchecked")
final T bean = (T) context.getBean(beanName);
return bean;
}
public static <T> Map<String, T> getBeans(final Class<T> requiredType) {
return context.getBeansOfType(requiredType);
}
public static Map<String, Object> getBeansWithAnnotation(final Class<? extends Annotation> annotationType) {
return context.getBeansWithAnnotation(annotationType);
}
}
private XXXrService xxxService = MyApplicationContext.getBean(XXXService.class);
Can be injected
版权声明
本文为[Mount MONI]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220558291768.html
边栏推荐
- [paper reading] [3D object detection] voxel transformer for 3D object detection
- What is the thirty-six plan
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- What is the meaning of load balancing
- Installation and use of Apache bench (AB pressure test tool)
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- mysql table 中增加列的SQL语句
- Installation of zynq platform cross compiler
- 协程与多进程的完美结合
- Inverse system of RC low pass filter
猜你喜欢
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
PIP3 installation requests Library - the most complete pit sorting
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
383. Ransom letter
Mysql50 basic exercises
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
随机推荐
Apache Bench(ab 压力测试工具)的安装与使用
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
Case of using stream load to write data to Doris
A heavy sword without a blade is a great skill
Chapter 4 - understanding standard equipment documents, filters and pipelines
简单的拖拽物体到物品栏
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
Recommended scheme of national manufactured electronic components
FAQ of foreign lead and alliance Manager
zynq平臺交叉編譯器的安裝
Leetcode009 -- search the target value in the array with binary search
leetcode004--罗马数字转整数
Use recyclerview to realize left-right side-by-side classification selection
那些年我面试过的Android开发岗总结(附面试题+答案解析)
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Spark case - wordcount
Open the past and let's start over.
General enumeration constant class
Supplement: Annotation
Differences among electric drill, electric hammer and electric pick