参考文章:这里
出现原因:
@Bean
public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
LifecycleBeanPostProcessor用于在实现了Initializable接口的Shiro bean初始化时调用Initializable接口回调,在实现了Destroyable接口的Shiro bean销毁时调用 Destroyable接口回调。在配置类中,该Bean提前调用了未初始化的资源导致了null异常。参考如下解释:
解决方案两种:
- 将LifecycleBeanPostProcessor 这个bean放到单独的Config文件中去
- 在该方法前增加static,如下:
@Bean
public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
后记:
若在yml文件中配置port,host,timeout等出现timeout和max-active报错,这是因为这两个类型不是int 而是duration,可以改为如下:
spring
redis:
host: localhost
port: 6379
jedis:
pool:
max-idle: 8
min-idle: 0
max-active: 8
max-wait: -1ms
timeout: 2000ms
此时需注意:在shiroConfig中@value取timeout的时候类型不可以是int,因为timeout实际上是2000ms是String类型,否则会报错格式转换异常。
2018-09-27 15:54:34.771 WARN 13064 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shiroConfig': Unsatisfied dependency expressed through field 'timeout'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "2000ms"
2018-09-27 15:54:34.771 INFO 13064 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-09-27 15:54:34.787 ERROR 13064 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shiroConfig': Unsatisfied dependency expressed through field 'timeout'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "2000ms"