Skip to content

Spring相关

Kervin edited this page May 9, 2023 · 14 revisions

一、Spring系列版本对应关系

Spring Spring-Boot Spring-Cloud Spring-Cloud-Netflix
4.3.x 1.5.21.RELEASE Edgware.SR6 1.4.7.RELEASE
5.2.x 2.2.x.RELEASE Hoxton.SR1~SR2 2.2.1.RELEASE
5.2.x 2.3.x.RELEASE Hoxton.SR3~SR6 2.2.2.RELEASE

二、Spring上下文

  1. Spring的Schema相关参数查询
  • 包含jar:spring-context-x.x.x.RELEASE.jar
  • 位置:org.springframework.context.config
  • 文件:spring-context-x.x.xsd

三、Spring EL

1.Spring的填充普通类

 @Value("#{${thread.count}>0?${thread.count}:T(java.lang.Runtime).getRuntime().availableProcessors()*2}")
private int thread_count;

2.Spring的填充Bean

AckMode MANUAL = org.springframework.kafka.listener.AbstractMessageListenerContainer.AckMode.MANUAL
AckMode BATCH = org.springframework.kafka.listener.AbstractMessageListenerContainer.AckMode.BATCH
@Value("#{${enable.auto.commit}?@BATCH:@MANUAL}")
private AckMode ackMode;

3.Spring的填充Class

AckMode MANUAL = org.springframework.kafka.listener.AbstractMessageListenerContainer.AckMode.MANUAL
AckMode BATCH = org.springframework.kafka.listener.AbstractMessageListenerContainer.AckMode.BATCH
@Value("#{${enable.auto.commit}?B(BATCH):B(MANUAL)}")
private AckMode ackMode;

4.Spring的填充方法处理

@Value("#{'${tagschema.cache.name:}'?.split(',')}")
private String[] names;

配置

tagschema.cache.name=unit05,unit06

四、Spring xml配置

1.Spring的枚举

<bean id="BATCH" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
		<property name="staticField" value="org.springframework.kafka.listener.AbstractMessageListenerContainer.AckMode.BATCH"/>
</bean>

2.Spring方法构造bean

<bean id="kafkaProducerListener" class="com.kafka.spring.KafkaProducerService" autowire-candidate="false"/>
<bean id="producerListener" factory-bean="kafkaProducerListener" factory-method="callback"/>

autowire-candidate="false" 不产生bean,防止同一类产生多个bean

五、Spring开发

  1. SpringMVC下载
Path file = Paths.get("/home/test.docx");
UrlResource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable())
{
	BodyBuilder bulder = ResponseEntity.ok();
	bulder.cacheControl(CacheControl.empty().mustRevalidate());
	bulder.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;fileName="+ URLEncoder.encode(resource.getFilename().trim().replaceAll(" ", ""),StandardCharsets.UTF_8.name()));
	bulder.header(HttpHeaders.PRAGMA, CacheControl.noCache().getHeaderValue());
	bulder.header(HttpHeaders.EXPIRES, "0");
	bulder.contentLength(resource.contentLength());
	bulder.contentType(MediaType.valueOf(MediaType.APPLICATION_OCTET_STREAM_VALUE));
	ResponseEntity<InputStreamResource> resp = bulder.body(new InputStreamResource(resource.getInputStream()));
	return resp;
}else {
	return error("报告不存在!");
}

六、SpringBoot开发

  1. 服务器配置
org.springframework.boot.autoconfigure.web.ServerProperties
  1. Spring获取请求
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();