是否可以运行带有加载弹簧上下文的测试套件,就像这样
@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class })
@ContextConfiguration(locations = { "classpath:context.xml" }) <------
public class SuiteTest {
}
Run Code Online (Sandbox Code Playgroud)
上面的代码显然不会工作,但有没有办法完成这样的行为?
目前这是我的测试套件中使用spring上下文的方式:
@BeforeClass
public static void setUp() {
final ConfigurableApplicationContext context =
loadContext(new String[] { "context.xml" });
jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
// initialization of other beans...
}
Run Code Online (Sandbox Code Playgroud) 我正在Windows项目的行尾设置为LF的计算机上工作。jgitflow:release-start运行命令后,将使用新版本更新poms,但poms中的行尾也将更改为CRLF。仅提及*.xml text eol=lf.gitattributes中有一行。
有谁知道如何防止这种情况?
有没有办法从jsp文件中获取响应作为servlet内的String.就像是
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String section1 = getResponseFromJSP();
// do something else ...
sendMailToUser(section1);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Servlet API 2.4和java 1.4
有没有办法在spring webflow中处理未发生的eventId或没有eventId参数?
例如,对于此webflow
<flow xmlns="http://www.springframework.org/schema/webflow" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<view-state id="welcome">
<transition on="goForward" to="nextStep"/>
</view-state>
<view-state id="nextStep">
<transition on="endFlow" to="finishStep" />
</view-state>
<end-state id="finishStep"/>
<global-transitions>
<transition on="cancel" to="finishStep"/>
</global-transitions>
</flow>
Run Code Online (Sandbox Code Playgroud)
如何使用param like处理请求_eventId=unexistingAction或没有_eventIdparam的请求?这通常会产生一个带有堆栈跟踪的页面......
no transition found on occurence of event in state of flow...
Run Code Online (Sandbox Code Playgroud) 我遇到了一个非常简单的任务:如何在Spring MVC 3.2配置中设置ServletContext属性?
我发现类似的东西可以用ServletContextPropertyPlaceholderConfigurer来完成,但是从春季3.1这被视为过时:
" 已过时.在Spring 3.1赞成PropertySourcesPlaceholderConfigurer的与StandardServletEnvironment结合. "
这并没有告诉我多少,因为我不知道如何使用StandardServletEnvironment.
有什么建议吗?
有没有办法使用 Spring Spring AMQP 拦截每个传入和传出的消息?类似于 servlet 上下文中的过滤器。
我有一个使用 micrometer Elasticsearch 注册表(使用 Elasticsearch 7.2.0)的简单 spring boot 2.1.7.RELEASE项目。该项目可在这里GitHub上。它只有两个类,看起来像这样
pom.xml具有以下依赖项:
<dependencies>
<dependency>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
<dependency>
<artifactId>spring-boot-starter-actuator</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
<dependency>
<artifactId>micrometer-registry-elastic</artifactId>
<groupId>io.micrometer</groupId>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
和两个类: MicrometerElasticApplication:
@SpringBootApplication
public class MicrometerElasticApplication {
public static void main(final String[] args) {
SpringApplication.run(MicrometerElasticApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
和测试控制器
@RestController
public class TestController {
@ResponseStatus(HttpStatus.OK)
@GetMapping("/test")
public void sendMessage() {
System.out.println("Received a test message");
}
}
Run Code Online (Sandbox Code Playgroud)
启动应用程序后,我可以在日志中看到
i.m.elastic.ElasticMeterRegistry : publishing metrics to elastic every 1m
Run Code Online (Sandbox Code Playgroud)
这意味着发送了指标,但是当我检查 Elasticsearch 中索引的内容时,我只能看到这样的文档
{
"_index": …Run Code Online (Sandbox Code Playgroud) 有没有办法用EL访问EL中的HashMap元素,键是String和int值的串联.像这样的东西:
<c:forEach begin="1" end="5" var="current">
<c:out value="${myHashMap['elem-' + current]}"/>
</c:forEach>
Run Code Online (Sandbox Code Playgroud) 我需要它启动,并以字母结尾和中间包含4-8个字符的字符串相匹配的正则表达式:A-z,-,_,和.
字符串不能包含两个或多个连续-,.或_ 字符.
没有连续限制的正则表达式是^[A-z][A-z_\\.\\-]{4,8}[A-z]$如何在这里添加限制或者可能不可能?
我知道在正则表达式中对特定单词进行否定的首选方法是否定前瞻,但我不知道如何在此处实现它.
我将spring boot版本从1.4.2升级到1.5.1.我的应用程序取决于RabbitMQ.
在使用版本1.4.2时,健康端点的输出是
{
"status": "UP",
"diskSpace": {
"status": "UP",
"total": 249779191808,
"free": 160644202496,
"threshold": 10485760
},
"rabbit": {
"status": "UP",
"version": "3.6.5"
}
}
Run Code Online (Sandbox Code Playgroud)
升级后,使用1.5.1版输出
{
"status": "UP"
}
Run Code Online (Sandbox Code Playgroud)
因此,磁盘空间和兔子指标不再存在.奇怪的是在日志中,我可以找到:
Registering bean definition for @Bean method org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$DiskSpaceHealthIndicatorConfiguration.diskSpaceHealthIndicatorProperties()
Registering bean definition for @Bean method org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration$RabbitHealthIndicatorConfiguration.rabbitHealthIndicator()
Run Code Online (Sandbox Code Playgroud)
甚至当我请求/健康端点时,我可以在日志中看到调用RabbitMq
o.s.amqp.rabbit.core.RabbitTemplate - Executing callback on RabbitMQ Channel: Cached Rabbit Channel: AMQChannel(amqp://guest@0:0:0:0:0:0:0:1:5672/,2), conn: Proxy@5292883 Shared Rabbit Connection: SimpleConnection@1aa1a795 [delegate=amqp://guest@0:0:0:0:0:0:0:1:5672/, localPort= 59527]
Run Code Online (Sandbox Code Playgroud)
预计,如果我关闭RabbitMQ实例的响应是
{
"status": "DOWN"
}
Run Code Online (Sandbox Code Playgroud)
在我的application.ymlI中没有任何健康配置,因此使用默认配置.
如何获取健康端点的旧输出?
health-monitoring spring-boot spring-rabbitmq spring-boot-actuator
我得到例外
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
Run Code Online (Sandbox Code Playgroud)
对于代码行
when(messageSource.getMessage(eq(SUCCESS_MESSAGE_KEY), any(Object[].class), any(Locale.class))).thenReturn(anyString());
Run Code Online (Sandbox Code Playgroud)
messageSource的类型为org.springframework.context.MessageSource.Mockito版本是1.9.5.谁能猜出问题是什么?
Spring Web应用程序配置包含ObjectMapper像这样配置的Jackson
objectMapper.disable(ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
objectMapper.registerModule(new JavaTimeModule())
Run Code Online (Sandbox Code Playgroud)
JavaTimeModule添加来处理 的反序列化ZonedDateTime。有两个端点处理包含ZonedDateTime. POJO是这样的:
class MyRequest {
ZonedDateTime from
ZonedDateTime to
}
Run Code Online (Sandbox Code Playgroud)
具有端点的控制器是:
@Slf4j
@RestController
class MyController {
@GetMapping('/pojo')
void getPojo(MyRequest myRequest) {
log.debug("Request received: $myRequest")
}
@PostMapping('/pojo')
void postPojo(@RequestBody MyRequest myRequest) {
log.debug("Request received: $myRequest")
}
}
Run Code Online (Sandbox Code Playgroud)
当我发送带有正文的 POST /pojo 时
{"from": "2017-03-15T00:00:00Z", "to": "2017-03-16T00:00:00Z"}
Run Code Online (Sandbox Code Playgroud)
响应为200,反序列化成功。
相反,当我发送
GET /pojo?from=2017-03-15T00:00:00Z&to=2017-03-15T00:00:00Z
Run Code Online (Sandbox Code Playgroud)
收到 400 Bad Request 错误
Failed to convert from type [java.lang.String] to type [java.time.ZonedDateTime] for value '2017-03-15T00:00:00Z'
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为在 GET …
自定义线程池的建议大小为number_of_cores + 1(请参见此处和此处).因此,假设在具有2个内核的系统上有一个Spring应用程序,配置就像这样
<task:executor id="taskExecutor"
pool-size="#{T(java.lang.Runtime).getRuntime().availableProcessors() + 1}" />
<task:annotation-driven executor="taskExecutor" />
Run Code Online (Sandbox Code Playgroud)
在这种情况下,将在多个请求之间共享一个ExecutorService.因此,如果10个请求到达服务器,则只能在ExecutorService中同时执行其中3个请求.这可能会产生瓶颈,并且结果会随着请求数量的增加而变得更糟(请记住:默认情况下,tomcat最多可以处理200个并发请求= 200个线程).该应用程序将执行得更好,没有任何池.
通常,一个核心可以同时处理多个线程.例如,我创建了一个服务,它调用两次https://httpbin.org/delay/2.每次调用都需要2秒才能执行.因此,如果没有使用线程池,则服务平均响应4.5秒(使用20个同时请求进行测试).如果使用线程池,则响应因池大小和硬件而异.我在具有不同池大小的4核心机器上运行测试.以下是最小,最大和平均响应时间(以毫秒为单位)的测试结果
从结果可以得出结论,最佳平均时间是最大池大小.池中5(4个核心+ 1)线程的平均时间比没有池的结果更糟糕.因此,在我看来,如果请求不占用大量CPU时间,那么将线程池限制为Web应用程序中的核心数+ 1是没有意义的.
对于非CPU要求的Web服务,是否有人发现在2或4核心机器上将池大小设置为20(甚至更多)有什么问题?
java ×5
spring ×4
spring-boot ×3
spring-mvc ×3
servlets ×2
amqp ×1
el ×1
event-id ×1
jackson2 ×1
java1.4 ×1
jgit ×1
jsp ×1
jstl ×1
junit ×1
line-endings ×1
maven ×1
micrometer ×1
mockito ×1
pom.xml ×1
rabbitmq ×1
regex ×1
spring-amqp ×1
spring-test ×1
test-suite ×1
threadpool ×1
tomcat ×1
unit-testing ×1