Spring Boot系列之使用问题总结
  TEZNKK3IfmPf 2023年11月13日 32 0

在使用springboot集成RabbitMQ的时候,选择使用 springboot 提供的starter,spring-boot-starter-amqp,参考官网示例,以及网络上面的一大堆资源写一些demo程序,但是在一个小小的server-client程序中,竟然报错:

at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1414) [spring-rabbit-1.7.1.RELEASE.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
Caused by: org.springframework.amqp.AmqpException: No method found for class java.lang.Integer

百度以及Google,但是网上找到的一般都是报错 ​​AmqpException: No method found for class B​​​,显然不是一样的错误,且把死马当作活马医,解决方案网上一大堆,比如​​这里​​,但是真的和我遇到的情况一样么?彩笔级别的程序猿如我,还达不到看源码去debug调试的级别,只能暂且试一试类似上面这篇博文的解决方案。
但是加上两个@Bean,反而和网上的报错一样: ​​​AmqpException: No method found for class B​​​。
于是继续Google/百度,发现​​这篇​​,意思是把注解@RabbitListener从类级别移到方法级别的。试一试,果然如此。至于为什么,有待日后深入研究吧。

spring boot 集成 log4j2

没什么难度,在pom文件里面引入dependency,同时exclude原来的默认logger:spring-boot-starter-logging,同时在resources目录下面新增一个配置文件log4j2.xml文件即可。

spring boot使用https

默认情况下,spring boot 是 http 协议;https 协议的普及,以及企业应用安全的考虑,使得我们思考如何使得我们的spring boot 应用使用 https,并将 Http 请求自动重定向到 Https。
首先得有一个 https 根证书。可以向证书机构申请证书,也可以自己制作根证书。本小节讲述后者的实现。
命令行生成后缀为 *.jks 的文件:
​​​keytool -genkey -alias test -keyalg RSA -keysize 1024 -keystore test.jks -validity 365​​​ 查看JKS中生成的证书的详细信息:
​keytool -list -v -keystore test.jks​​ 导出证书为cer格式,可直接双击安装到浏览器(本机):
​keytool -alias test -exportcert -keystore test.jks -file test.cer​

新建一个配置类:

@Configuration
public class WebConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return container -> {
Ssl ssl = new Ssl();
// test.jks中包含服务器私钥和证书
ssl.setKeyStore("test.jks");
ssl.setKeyStorePassword("123456");
container.setSsl(ssl);
container.setPort(8443);
};
}

/**
* Embedded默认只有一个Connector,要在提供Https服务的同时支持Http,需要添加一个Connector。
*/
@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
TomcatEmbeddedServletContainerFactory factory =
new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
// SecurityConstraint必须存在,可以通过其为不同的URL设置不同的重定向策略。
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
factory.addAdditionalTomcatConnectors(createHttpConnector());
return factory;
}

private Connector createHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setSecure(false);
connector.setPort(8080);
connector.setRedirectPort(8443);
return connector;
}
}

效果:使用默认端口http://localhost:8080打开应用,会自动跳转到https://localhost:8443,Chrome 浏览器访问时,会显示一个红色的警告,那是因为本地安装的根证书没有得到认证中心的认证。
参考代码​​​GitHub​​

spring boot 启动失败 Unregistering JMX-exposed beans on shutdown

编写的spring boot demo case小程序,启动失败,没有任何报错信息,没有ERROR甚至WARNING级别的log产生,很是莫名其妙,只能从log入手:

2017-10-04 18:02:44.670  WARN 12076 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2017-10-04 18:06:12.665 INFO 19220 --- [ main] batch.BatchApplication : Started BatchApplication in 2.744 seconds (JVM running for 3.384)
2017-10-04 18:06:12.665 INFO 19220 --- [ Thread-5] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@543e710e: startup date [Wed Oct 04 18:06:10 GMT+08:00 2017]; root of context hierarchy
2017-10-04 18:06:12.667 INFO 19220 --- [ Thread-5] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

从stackoverflow查看得知​​这里​​​ 大致意思是,这个demo case不是一个web应用,在classpath上面没有内嵌的服务器如Tomcat,故而应用启动成功,但是由于没有web server,不能处于running状态,加入​​spring-boot-starter-web​​即可。

spring boot工程跑UT失败

一个spring boot demo project,集成有 swagger,MyBatis,druid等组件,然后现在在加入spring-boot-starter-data-jpa之后,编写测试用例Unit Test,启动报错:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/C:/Users/johnn/.m2/repository/io/springfox/springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/C:/Users/johnn/.m2/repository/io/springfox/springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

A child container failed during start

spring boot 应用启动失败,报错信息:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardServer[-1]]
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Tomcat]]
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat]]
Caused by: org.apache.catalina.LifecycleException: A child container failed during start

一开始仅仅只是去关注下面的错误堆栈信息:​​Failed to start component​​​here
知道错误的原因是:

Caused by: java.lang.ClassNotFoundException: org.apache.catalina.util.StandardSessionIdGenerator

于是又是各种百度 Google。包括查看 dependency 的包结构信息。
通过查看 spring-boot-starter-tomcat 的 pom 文件,知道这个类应该是

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>

里面的类,但是并没有找到。持续懵逼。
各种摸索才知道,一开始把 springboot 的 version 从 1.3.3(别人的运行成功的案例) 改成 1.5.9,默认支持的tomcat 版本从 8.0.X 升级成 8.5.X,
如果此时还想要通过定义 properties 标签,使用 8.0.X 版本的 tomcat,则会启动失败,抛出上面的错误堆栈。

<properties>
<start-class>com.johnny.validator.Application</start-class>
<!--<tomcat.version>8.0.8</tomcat.version>-->
<!--添加 spring-boot-starter-tomcat 依赖之后,spring boot 会自动解析这个 property-->
<tomcat.version>8.5.3</tomcat.version>
</properties>

另外还要注意一个问题,groupId 为​​org.apache.tomcat.embed​​​,下面的 artifactId,即 jar 包有6个( 看这里​​maven​​​),各个 jar 包的发布版本不太一致。如​​tomcat-embed-core​​​有大部分 8.5.X 版本,但是​​tomcat-embed-logging-juli​​​8.5.X 只提供两个版本,8.5.0 和 8.5.2,​​tomcat-embed-logging-juli​​​。
这也就是意味着并没有统一配置版本信息。

ClassNotFoundException: org.apache.juli.logging.LogFactory

spring boot 启动报错:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
Caused by: java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
at org.apache.catalina.util.LifecycleBase.<clinit>(LifecycleBase.java:37) ~[tomcat-embed-core-8.0.8.jar:8.0.8]
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory

这个问题好解决。
但是上面的版本信息​​​[tomcat-embed-core-8.0.8.jar:8.0.8]​​​值得注意,这个版本号是可以自己配置的。怎么自定义配置,参考我的另外一篇博客。
在 maven 的 pom 文件里面添加:

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>8.5.2</version>
</dependency>

但是需要去 maven 仓库查看是不是真的有这个版本的artifactId,以及注意与 spring boot 的版本相匹配。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月17日   46   0   0 JSpspring
TEZNKK3IfmPf