@Configuration
public class AppConfig {
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public RequestScopedBean requestScopedBean() {
return new RequestScopedBean();
}
}
@Controller
public class MyController {
@Autowired
private WebApplicationContext webApplicationContext;
@RequestMapping("/my-url")
public String myUrl() {
RequestScopedBean requestScopedBean =
webApplicationContext.getBean("requestScopedBean", RequestScopedBean.class);
//use requestScopedBean
return "my-view";
}
}
在上面的示例中,getBean方法中第一个参数是bean的名称,第二个参数是bean的类型。需要注意的是,当使用代理模式时,需要使用ScopedProxyMode.TARGET_CLASS。
上一篇:ApplicationConfigurationMetadataResolver : 无法获取Docker资源的属性。
下一篇:ApplicationContext.getBeansOfType返回的Map发生了变化-WebFluxTest SpringBoot