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")
实现直接
4. 一个类型多个实现处理。
注册加别名,如:@Service("abc")
实现如下:
@Autowired @Qualifier("abc") private IFooBarService service;