Spring boot相关技巧

1. 注册多实例。

@Scope("prototype")

2. 手工方式获取注册的实例。

@Autowired
private ServletContext servletContext;
 
private <T> T resolve(Class<T> type) { WebApplicationContext webApplicationContext=(WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); return webApplicationContext.getBean(type); }

 

3. 多个同名类处理。
    注册加别名,如:@Service("abc")
    实现直接@Autowired即可(无须加别名)

4. 一个类型多个实现处理。
    注册加别名,如:@Service("abc")
    实现如下:

 @Autowired
 @Qualifier("abc")
 private IFooBarService service;

 

posted @ 2022-01-12 11:14  IT6889  阅读(23)  评论(0编辑  收藏  举报