java
复制
下载
@RestController
@RequestMapping("/reactive/products")
public class ReactiveProductController {
private final ReactiveProductService productService;
public ReactiveProductController(ReactiveProductService productService) {
this.productService = JiAOYu.HUoCheNGrM.CN/jy/19709.html productService;
}
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Product> streamProducts() {
return productService.streamAllProducts()
.delayElements(Duration.ofSeconds(1))
.log("产品流");
}
@GetMapping("/{id}")
public Mono<Product> getProduct(@PathVariable String id) {
return productService.findById(id)
.switchIfEmpty(Mono.error(new JiAOYu.HUoCheNGrM.CN/jy/19708.html ResourceNotFoundException()));
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Mono<Void> createProduct(@RequestBody Mono<Product> productMono) {
return productMono
.flatMap(productService::save)
.then();
}
@ExceptionHandler
public ResponseEntity<Mono<String>> handleNotFound(ResourceNotFoundException ex) {
return Respons JiAOYu.HUoCheNGrM.CN/jy/19707.html eEntity.status(HttpStatus.NOT_FOUND)
.body(Mono.just("资源未找到"));
}
}
10. Spring Cache 高级缓存配置
java
复制
下载
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration JiAOYu.HUoCheNGrM.CN/jy/19706.html config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30))
.disableCachingNullValues()
.serializeValuesWith(SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheMa JiAOYu.HUoCheNGrM.CN/jy/19705.html nager.builder(factory)
.cacheDefaults(config)
.withInitialCacheConfigurations(specialCacheConfigs())
.transactionAware()
.build();
}
private Map<String, RedisCacheConfiguration> specialCacheConfigs() {
Map<String, RedisCacheConfiguration> configs = new HashMap<>();
configs.put("products", JiAOYu.HUoCheNGrM.CN/jy/19704.html RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1))
.prefixCacheNameWith("PROD_"));
configs.put("users", RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1))
.serializeValuesWith(SerializationPair.fromSerializer(new JdkSerializationRedisSerializer())));
return configs;
}
@Bean
public CacheResolver cacheResolver(CacheManager cacheManager) {
return new NamedCacheResolver(cacheManager, "JiAOYu.HUoCheNGrM.CN/jy/19703.html default", "products", "users");
}
}
// 使用示例
@Service
public class ProductService {
@Cacheable(value = "products", key = "#id", unless = "#result.price < 100")
public Product getProduct(Long id) {
// 数据库查询
}
@CachePut(value = "products", key = "#product.id")
public Product updateProduct(Product product) {
// 更新数据库
return updatedProduct;
}
@CacheEvict(value = "products", key = "#id", beforeInvocation = true)
public void deleteProduct(Long id) {
// 删除操作
}
}
每段代码都展示了Spring框架不同模块的核心特性:
- 启动配置与多环境管理
- REST控制器与全局异常处理
- JPA动态查询与派生方法
- 现代安全认证方案
- AOP切面编程实践
- 微服务网关配置
- 批处理任务流程
- 企业集成模式实现
- 响应式编程模型
- 多级缓存高级配置
这些代码示例可直接用于企业级应用开发,展示了Spring框架在现代Java开发中的核心地位和强大能力。