当前位置:网站首页>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
边栏推荐
- Leetcode - > 1 sum of two numbers
- New terminal play method: script guidance independent of technology stack
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- 补:注解(Annotation)
- Key points of AWS eks deployment and differences between console and eksctl creation
- 重剑无锋,大巧不工
- 协程与多进程的完美结合
- 2021数学建模国赛一等奖经验总结与分享
- getprop 属性
- Installation and deployment of Flink and wordcount test
猜你喜欢
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
2021数学建模国赛一等奖经验总结与分享
Practice and exploration of knowledge map visualization technology in meituan
Coinbase:关于跨链桥的基础知识、事实和统计数据
Key points of AWS eks deployment and differences between console and eksctl creation
The perfect combination of collaborative process and multi process
那些年我面试过的Android开发岗总结(附面试题+答案解析)
Detailed explanation of life cycle component of jetpack
递归调用--排列的穷举
Druid -- JDBC tool class case
随机推荐
Installation and use of Apache bench (AB pressure test tool)
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
Spark small case - RDD, spark SQL
Apache Bench(ab 压力测试工具)的安装与使用
Eksctl deploying AWS eks
Improving 3D object detection with channel wise transformer
Create VPC in AWS console (no plate)
Last day of 2017
New terminal play method: script guidance independent of technology stack
Unity攝像頭跟隨鼠標旋轉
leetcode001--返回和为target的数组元素的下标
Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
Case of using stream load to write data to Doris
Inverse system of RC low pass filter
Coinbase: basic knowledge, facts and statistics about cross chain bridge
mysql ,binlog 日志查询
简单的拖拽物体到物品栏
Practice and exploration of knowledge map visualization technology in meituan
Unity3d practical skills - theoretical knowledge base (I)