`

SpringBoot对于一些必须要先初始化Bean给出WARN的解决办法

    博客分类:
  • java
 
阅读更多

笔者生产中,遇到

2017-05-16 08:47:22.020  WARN 1910 --- [localhost-startStop-1] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'myBatisMapperScannerConfig' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2017-05-16 08:47:22.487  WARN 1910 --- [localhost-startStop-1] o.s.c.a.ConfigurationClassEnhancer       : @Bean method Application.initOcc 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.

 

MapperScannerConfigurerPropertyPlaceholderConfigurer

之类的Bean必须要标记为static方法,以示优先加载。否则会给出警告。

 

 

代码:

 

import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 * @author liuzh
 * @since 2015-12-19 14:46
 */
@Configuration
//TODO 注意,由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class MyBatisMapperScannerConfig {

	@Bean(name = "mapperScannerConfigurer")
	public static MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactorys");
		mapperScannerConfigurer.setBasePackage("com.odianyun.swift.chae.alarm.mapper");
		return mapperScannerConfigurer;
	}

}

 

 

@Bean(name="occConfigure")
	public static OccPropertyPlaceholderConfigurer initOcc(){
		OccPropertyPlaceholderConfigurer opc = new OccPropertyPlaceholderConfigurer();
		opc.setPool("chae");
		return opc;
	}

 

分享到:
评论

相关推荐

    Spring Boot 全局懒加载机制.docx

    1. **第一次HTTP请求处理时间变长**:由于bean在首次使用时才初始化,首次请求可能会因为初始化bean而延迟。后续请求则不会受到影响,因为bean已经被创建。 2. **延迟错误暴露**:由于bean在运行时才初始化,一些...

    SpringBoot面试专题及答案.zip

    3. 加载所有带有@Configuration的类,进行Bean的定义和初始化。 4. 执行SpringApplicationRunListeners的各个方法,如应用程序的初始化、环境准备等。 5. 执行ApplicationRunner或CommandLineRunner接口的run方法,...

    SpringBoot(入门篇)资料.rar

    SpringBoot是Java开发中的一个流行框架,主要用于简化Spring应用程序的初始搭建以及开发过程。这个"SpringBoot(入门篇)资料.rar"压缩包包含了几个关键的学习资源,帮助初学者快速掌握SpringBoot的基础知识。 1. *...

    SpringBoot+AOP日志服务

    首先,Spring Boot简化了Spring框架的配置和初始化,使得开发者可以快速搭建一个完整的Web应用。它内置了Tomcat服务器,并提供了自动配置功能,能够根据项目依赖自动配置相应的Bean。 1. **Spring AOP基础**: AOP...

    Java_提高弹簧启动测试效率.zip

    Spring Boot作为Spring框架的一部分,极大地简化了初始化和配置过程,使得开发人员能够快速地启动项目。然而,随着项目的复杂性增加,Spring Boot应用的启动时间和测试效率可能成为开发流程中的瓶颈。本文将围绕...

    MQForSpring.zip

    SpringBoot是一个轻量级的框架,用于简化Spring应用的初始化和配置。将RabbitMQ与SpringBoot整合,可以充分利用它们的优势,实现高效的消息处理。下面将详细讲解如何在SpringBoot项目中整合RabbitMQ,以及Work Queue...

    Swagger 接口文档 接入springboot 的 教程及 logback-spring.xml输出不同级别的日志信息(附件).rar

    Swagger接口文档是软件开发中用于构建RESTful API的重要工具,它提供了一种规范化的、易于理解和使用的API描述语言,使得开发者能够清晰地了解服务提供的功能和调用方式。本教程将探讨如何在Spring Boot项目中集成...

Global site tag (gtag.js) - Google Analytics