当前位置:网站首页>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
边栏推荐
- 229. Find mode II
- How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
- getprop 属性
- [paper reading] [3D target detection] point transformer
- Bridge between ischemic stroke and intestinal flora: short chain fatty acids
- Alibaba tip: it is better to create threads manually
- Recommended scheme of national manufactured electronic components for intelligent electronic scales
- [paper reading] [3D object detection] voxel transformer for 3D object detection
- The programmer starts the required application with one click of window bat
- Improving 3D object detection with channel wise transformer
猜你喜欢

Supplement: Annotation
![[paper reading] [3D target detection] point transformer](/img/c5/b1fe5f206b5fe6e4dcd88dce11592d.png)
[paper reading] [3D target detection] point transformer

C language: spoof games

Use recyclerview to realize left-right side-by-side classification selection

zynq平台交叉编译器的安装

Flink's important basics

Spark FAQ sorting - must see before interview

Interaction of diet gut microbiota on cardiovascular disease

Supplement 14: cmake practice project notes (to be continued 4 / 22)

Key points of AWS eks deployment and differences between console and eksctl creation
随机推荐
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
520.检测大写字母
简单的拖拽物体到物品栏
Coinbase: basic knowledge, facts and statistics about cross chain bridge
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
shell wc (统计字符数量)的基本使用
test
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
Record the ThreadPoolExecutor main thread waiting for sub threads
补充番外14:cmake实践项目笔记(未完待续4/22)
RC低通滤波器的逆系统
针对NFT的网络钓鱼
Error occurs when thymeleaf th: value is null
Go反射法则
Teach you how to build the ruoyi system by Tencent cloud
New terminal play method: script guidance independent of technology stack
[pytoch foundation] torch Split() usage
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
The last day of 2021 is the year of harvest.
Flink case - Kafka, MySQL source