spring bean生命周期:http://www.cnblogs.com/zrtqsk/p/3735273.html
session bean创建:
/** * Created by dongsilin on 2017/3/7. * RestTemplate bean,生命周期为session */ @Configuration public class RestTemplateBean { private static final SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); private static final HttpMessageConverter httpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8")); static { requestFactory.setReadTimeout(1000 * 30); //30s requestFactory.setConnectTimeout(1000 * 30); //30s } @Bean @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) public RestTemplate initRestTemplate(){ RestTemplate restTemplate = new RestTemplate(); restTemplate.setRequestFactory(requestFactory); restTemplate.getMessageConverters().set(1, httpMessageConverter); return restTemplate; } }
session bean使用:
@Autowired private RestTemplate restTemplate;
时间: 2024-11-07 01:10:36