如何注册/添加一个自定义关闭例程,该例程将在我的Spring Boot应用程序关闭时触发?
场景:我将Spring Boot应用程序部署到Jetty servlet容器(即没有嵌入式Jetty).我的应用程序使用Logback进行日志记录,我想使用Logback的MBean JMX配置程序在运行时更改日志记录级别.其文档指出,为避免内存泄漏,在关闭时必须调用特定的LoggerContext关闭方法.
听取Spring Boot关闭事件的好方法是什么?
我试过了:
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext cac = SpringApplication.run(Example.class, args);
cac.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
logger.info("Do something");
}
});
}
Run Code Online (Sandbox Code Playgroud)
但是当应用程序关闭时,不会调用此注册的侦听器.
我使用的是spring-boot,我有一个像这样定义的实体类
import org.joda.time.LocalDateTime;
@Entity
public class Project {
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime start_date;
...
...
}
Run Code Online (Sandbox Code Playgroud)
当此类转换为JSON时,该字段将转换为以下字符串表示形式
{"start_date":[2014,11,15,0,0,0,0],...., ...}
Run Code Online (Sandbox Code Playgroud)
我想让json响应为yyyy-MM-dd.
我尝试了@DateTimeFormat(iso = ISO.DATE)注释,但也没有帮助.
是否有一种简单的方法可以将此转换为正确的json格式?
我正在使用带有maven的spring-boot,这是我的配置类:
package hello;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序启动时在控制台中显示此行:
2014-11-06 17:00:55.102 INFO 4669 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
Run Code Online (Sandbox Code Playgroud)
我想将TomcatEmbedded端口更改为8081.感谢:D
如何更改Spring Boot依赖项的默认版本?例如,Spring Boot 1.4使用Thymeleaf 2.1版本,但最新版本的Thymeleaf是3.0.那么如何将2.1版更改为3.0版?
我有一些问题试图让我的建议执行.我尝试了几个不同的切入点无济于事."@EnableAspectJProxy"似乎正在工作并检测我的方面.任何建议表示赞赏.
我正在使用spring-boot-aop-starter.
@Aspect
@Component
public class ExecutionTimeLogger {
private Logger logger;
public ExecutionTimeLogger() {
logger = LoggerFactory.getLogger(getClass());
logger.info("HEY");
}
@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() {}
@Pointcut("execution(* edu.x.y.z.server.web.controller.*.*(*))")
public void methodPointcut() {}
@Pointcut("within(@org.springframework.web.bind.annotation.RequestMapping *)")
public void requestMapping() {}
@Around("controller() && methodPointcut() && requestMapping()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
StopWatch sw = new StopWatch();
String name = pjp.getSignature().getName();
try {
sw.start();
return pjp.proceed();
} finally {
sw.stop();
logger.info("STOPWATCH: " + sw.getTime() + " - " + name);
}
}
} …Run Code Online (Sandbox Code Playgroud)