spring 相关问题

Spring Framework是Java平台上应用程序开发的开源框架。其核心是对基于组件的体系结构的丰富支持,目前它拥有20多个高度集成的模块。


Spring 批处理运行处理器/编写器的次数作为块大小

嗨,我想了解 Spring Batch 的设计。 给定以下作业配置: @豆 公共步骤 jobStep() { 返回新的 StepBuilder("stepName", repo) . 嗨,我想了解 Spring Batch 的设计。 给定以下作业配置: @Bean public Step jobStep() { return new StepBuilder("stepName", repo) .<List<Entity>, List<Entity>> chunk(10, txnManager) .reader(reader) .processor(processor) .writer(writer) .build(); } 以及以下示例处理器: public class JobProcessor implements ItemProcessor<List<Entity>, List<Entity>> { @Override public List<Entity> process(List<Entity> items) { for (Entity record: items) { //do some logic return items; } 我的观察是,读取器/处理器/写入器运行的次数与块大小相同。例如,在上面的例子中,处理器将运行 10 次,作者也将运行 10 次。如果我的阅读器只提取 2 条记录,则它小于块大小。 ItemProcessor中List的大小只有2——如果传入整个记录集,为什么还要额外运行8次?避免重复处理的唯一方法似乎是跟踪某个项目是否被处理(和写入)。 这并不难,但我想了解这是正常行为还是我做错了什么。 总而言之 - 如果 Spring Batch 记录集小于块大小,处理器/编写器不应该只运行一次,因为第一个块包含整个记录集? 编辑:这是我删除列表的尝试,但它仍然循环块次数,即使项目的大小只有 2。 @Bean public Step jobStep() { return new StepBuilder("stepName", repo) .<Entity, Entity> chunk(10, platformTransactionManager) .reader(reader) .processor(processor) .writer(writer) .build(); } public class JobReader implements ItemReader<Entity> { @Override public Entity read() { List<Entity> items = repo.getData(); return items.isEmpty() ? items.get(0) : null; } public class JobProcessor implements ItemProcessor<Entity, Entity> { @Override public Entity process(Entity entity) throws Exception { //do stuff return entity; } 这里看起来错误的是您正在操作实体列表。正确的代码应该是这样的: public Step jobStep() { return new StepBuilder("stepName", repo) .<Entity, Entity> chunk(10, txnManager) .reader(reader) .processor(processor) .writer(writer) .build(); } public class JobProcessor implements ItemProcessor<Entity, Entity> { @Override public Entity process(Entity item) { //do some logic on one item return item; } } 与您的问题中未显示的读者和作者类类似。

回答 1 投票 0

设置上下文 |获取 TransactionSynconizationMananger.isCurrentTransactionReadOnly() 的值

我正在尝试将我的事务动态路由到 READ_WRITE 或 READ_ONLY 数据源。 但在我的类中 TransactionRoutingDataSource 扩展了 AbstractRoutingDataSource 当我试图获得 va 时...

回答 1 投票 0

JpaTransactionManager 支持 REQUIRES_NEW 传播吗?

在我的 springboot 应用程序中,我有一个带有嵌套事务的方法,如下所示。 //消息处理程序 @Transactional(传播=传播.REQUIRES_NEW) @覆盖 公共无效过程消息(

回答 1 投票 0

如何告诉 Springdata 存储库的删除方法在实体不存在时不抛出异常?

我正在使用SpringData的存储库。如果我尝试通过不存在或从未存在过的 ID 删除实体,则会引发异常。因为我不想检查该实体之前是否存在...

回答 10 投票 0

Spring HTTP 接口 - 逗号分隔的参数值列表

我在Spring中有以下@HttpExchange: 导入 org.springframework.web.bind.annotation.RequestParam; 导入 org.springframework.web.service.annotation.GetExchange; ... @GetExchange(&quo...

回答 1 投票 0

Spring 4 计算注释的占位符

我有一个注释,带有一个 String 属性,我希望它在运行时计算。 在我所说的具体情况下 @Scheduled(initialDelayString = "10000" ) 根据o...

回答 1 投票 0

Spring JPA - 如何在将父实体及其子实体保存在同一事务中后重新加载它们

在我的服务类中,我试图更新一个实体(及其许多子实体),并希望在同一事务中使用更新的记录。表中的数据正在更新,但上下文未更新...

回答 1 投票 0

@GetMapping 和 @GetExchange 有什么区别?

从Spring 6开始,引入了@GetExchange、@PostExchange、@PutExchange、@DeleteExchange、@PatchExchange。 据我所知,我们已经有了相同的注释,可以做同样的事情@GetMa...

回答 1 投票 0

如何在 MockWebServer 中使用 @BeforeEach 和 @AfterEach 注解?

在进行集成测试时,我试图用一个或多个 MockWebServer 来模拟控制器的行为,但没有找到解决方案。 我看到这里的帖子说 MockWebServer 应该......

回答 1 投票 0

Spring Cloud Gateway + Spring Cloud Security + Keycloak 如何连接

他这几天一直在尝试用 Keycloak 连接 s-c-gateway 和 s-c-security。我希望位于网关后面的模块没有 keycloak 配置。 春天来了吗...

回答 4 投票 0

Spring boot:无法打破bean之间的依赖循环

我正在使用 Spring Boot 创建一个航空公司网站。我有一个 Seat 类和一个 Flight 类,它们代表数据库中的表。这个想法是,每当添加航班时,座位......

回答 1 投票 0

Spring参数验证注解不验证

我有一个像这样的控制器方法: 导入 javax.validation.Valid; 导入 javax.validation.constraints.Min; 导入 javax.validation.constraints.NotNull; @控制器 @RequestMapping(值 = "/fo...

回答 3 投票 0

spring 4 计算注释的占位符

我有一个注释,带有一个 String 属性,我希望它在运行时计算。 在我所说的具体情况下 @Scheduled(initialDelayString = "10000" ) 根据o...

回答 1 投票 0

原因:org.messaging.handler.annotation.support.MethodArgumentNotValidException:无法解析 public void 中索引 1 处的方法参数

我正在开发 Kafka Consumer App,在手动尝试确认和确认时出现以下错误。可能是什么原因导致了这个问题? 错误详情: org.springframework.kafka.listener.

回答 1 投票 0

如何访问springs中的另一个字段@Value

就像标题所示,我想使用 springs @Value 注释访问另一个字段值。 我想这样做是为了通过另一个字段 id 解析字段值。例如:我有一个字段 id 和一个字段

回答 1 投票 0

Spring Boot 提供被安全阻止的静态内容

我启动了 Spring Boot + Angular 应用程序,现在我想将整个应用程序部署为 jar。所以我创建了 Maven 配置,其中构建了 Angular 应用程序,然后将其复制到 /target/classes/resource...

回答 7 投票 0

百里香叶+春天。提交响应后无法创建会话

我有一个网络应用程序使用带有 thymeleaf 的引导程序形式: 我有一个网络应用程序使用带有百里香的引导程序形式: <form th:method="POST" th:action="@{/main}" th:object="${order}"> <div class="mb-3"> <label for="companyNameInput" class="form-label">Company Name</label> <input type="text" th:field="*{companyName}" class="form-control" id="companyNameInput" aria-describedby="companyHelp"> <div id="companyHelp" class="form-text">What is the name of your company?</div> <div style="color:red" th:if="${#fields.hasErrors('companyName')}" th:errors="*{companyName}">IF::ERROR</div> </div> <div class="mb-3"> <label for="contactPersonInput" class="form-label">Contact Person</label> <input type="text" th:field="*{contactName}" class="form-control" id="contactPersonInput" aria-describedby="nameHelp"> <div id="nameHelp" class="form-text">What is the name of the person we are going to talk to?</div> <div style="color:red" th:if="${#fields.hasErrors('contactName')}" th:errors="*{contactName}">IF::ERROR</div> </div> <div class="mb-3"> <label for="contactNumberInput" class="form-label">Number</label> <input type="text" th:field="*{number}" class="form-control" id="contactnumberInput" aria-describedby="numberHelp"> <div id="numberHelp" class="form-text">Leave us your number so we can contact with you to negotiate next steps!</div> <div style="color:red" th:if="${#fields.hasErrors('number')}" th:errors="*{number}">IF::ERROR</div> </div> <div class="mb-3"> <label for="contactEmailInput" class="form-label">Email</label> <input type="email" th:field="*{email}" class="form-control" id="contactEmailInput" aria-describedby="emailHelp"> <div id="emailHelp" class="form-text">We'll sent you an email to recall about your order when it's ready to go. We'll never share your email with anyone else.</div> <div style="color:red" th:if="${#fields.hasErrors('email')}" th:errors="*{email}">IF::ERROR</div> </div> <div class="mb-3"> <label for="contactAdressInput" class="form-label">Address</label> <input type="text" th:field="*{address}" class="form-control" id="contactAdressInput" aria-describedby="addressHelp"> <div id="addressHelp" class="form-text">Where would you like to get your order?</div> <div style="color:red" th:if="${#fields.hasErrors('address')}" th:errors="*{address}">IF::ERROR</div> </div> <div class="mb-3"> <label for="categorySelect" class="form-label">Product Category</label> <select class="form-select" th:field="*{category}" aria-label="categorySelect" id="categorySelect" aria-describedby="categoryHelp"> <option th:value="Cigarettes" selected>Cigarettes</option> <option th:value="Vapes">Vapes</option> <option th:value="Devices">Smoking Devices</option> <option th:value="Drinks">Drinks</option> <option th:value="Food">Food</option> </select> <div id="categoryHelp" class="form-text">Where would you like to get your order?</div> <div style="color:red" th:if="${#fields.hasErrors('category')}" th:errors="*{category}">IF::ERROR</div> </div> <div class="mb-3"> <label for="additionalInformationTextarea" class="form-label">Additional Information</label> <textarea class="form-control" th:field="*{additionalInformation}" id="additionalInformationTextarea" rows="5"></textarea> <div style="color:red" th:if="${#fields.hasErrors('additionalInformation')}" th:errors="*{additionalInformation}">IF::ERROR</div> </div> <div class="mb-3 form-check"> <input type="checkbox" class="form-check-input js-checkbox" id="privacyCheck"> <label class="form-check-label" for="privacyCheck">I agree to all the company's terms and conditions and the data processing rule.</label> </div> <button type="submit" class="btn btn-primary js-button" disabled>Place an order</button> </form> 这个控制器可以处理这个: @GetMapping("/main") public String mainPage(@ModelAttribute("order") Order order) { return "main"; } @PostMapping("/main") public String createOrder(@ModelAttribute("order") @Valid Order order, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "main"; } order.setStatus(String.valueOf(OrderStatus.Pending)); orderDetailsService.saveOrder(order); return "redirect:/main"; } 但是当我进入我的网站时,我遇到了这个异常,看起来我的网站有点冻结(就像手风琴或轮播不起作用)。但如果我从 html 代码中删除“表单”,网站将再次运行。如果我多次重新加载页面,则可能会显示表单并且网站会停止冻结。 2024-05-13T19:34:49.173+03:00 INFO 13472 --- [retailShop] [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2024-05-13T19:34:49.173+03:00 INFO 13472 --- [retailShop] [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2024-05-13T19:34:49.175+03:00 INFO 13472 --- [retailShop] [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms 2024-05-13T19:34:49.432+03:00 ERROR 13472 --- [retailShop] [nio-8080-exec-2] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-2] Exception processing template "main": Error during execution of processor 'org.thymeleaf.spring6.processor.SpringActionTagProcessor' (template: "main" - line 383, col 44) org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringActionTagProcessor' (template: "main" - line 383, col 44) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 2024-05-13T19:34:49.438+03:00 ERROR 13472 --- [retailShop] [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringActionTagProcessor' (template: "main" - line 383, col 44)] with root cause java.lang.IllegalStateException: Cannot create a session after the response has been committed 2024-05-13T19:34:49.453+03:00 ERROR 13472 --- [retailShop] [nio-8080-exec-2] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/main] as the response has already been committed. As a result, the response may have the wrong status code. 如何解决这个问题? 看来我已经解决了! https://github.com/thymeleaf/thymeleaf-spring/issues/222 我将此代码添加到我的 SecurityConfig.class 方法中: public SecurityFilterChain filterChain(HttpSecurity http) { http.sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)); }

回答 1 投票 0

如何使用Spring data JPA保存双向关联而不进行级联?

假设我在父子之间有双向一对多关联,映射如下: 父级.java: @实体 公开课家长{ @ID 私有整数 ID; @OneToMany(mappedBy...

回答 2 投票 0

在 Spring Kafka 中为批量监听器实现自定义错误处理程序

我将向我的容器工厂添加自定义错误处理程序 这是我的要求: 我有一个批处理侦听器,即使某个特定记录由于

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.