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

笔者生产中,遇到

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;
	}

 

猜你喜欢

转载自jdkleo.iteye.com/blog/2374595