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

本文解析了Spring框架中关于@Configuration Bean定义的警告信息,并提供了如何避免这些警告的具体代码示例,强调了@Bean方法声明为static的重要性。

笔者生产中,遇到

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值