当前位置:网站首页>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
- Druid -- JDBC tool class case
- IDE idea automatic compilation and configuration of on update action and on frame deactivation
- What is a data island? Why is there still a data island in 2022?
- The last day of 2021 is the year of harvest.
- Case of using stream load to write data to Doris
- IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
- AWS eks add cluster user or Iam role
- 基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
- 补充番外14:cmake实践项目笔记(未完待续4/22)
猜你喜欢

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

Coinbase: basic knowledge, facts and statistics about cross chain bridge

IDE idea automatic compilation and configuration of on update action and on frame deactivation

Spark FAQ sorting - must see before interview

Interaction of diet gut microbiota on cardiovascular disease

Installation and deployment of Flink and wordcount test

補:注解(Annotation)

Small volume Schottky diode compatible with nsr20f30nxt5g

test

Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
随机推荐
Migrate from MySQL database to AWS dynamodb
The programmer starts the required application with one click of window bat
Record the blind injection script
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
383. Ransom letter
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
协程与多进程的完美结合
Spark FAQ sorting - must see before interview
The last day of 2021 is the year of harvest.
test
leetcode003--判断一个整数是否为回文数
MySQL time function query
Brushless motor drive scheme based on Infineon MCU GTM module
补充番外14:cmake实践项目笔记(未完待续4/22)
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
Basic operation of sequence table
shell wc (统计字符数量)的基本使用
Mysql50 basic exercises
从MySQL数据库迁移到AWS DynamoDB
520.检测大写字母