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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

笔者生产中,遇到

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 配置文件 Spring Boot 的配置文件主要用于定义应用程序的行为和环境变量。以下是关于如何设置和初始化 `application.properties` 或 `application.yml` 文件的详细介绍。 #### 1. 创建配置文件 在 Spring Boot 项目中,默认情况下,框架会在以下路径查找配置文件: - **classpath:/config/application.properties** - **classpath:/application.properties** 因此,在 Maven 或 Gradle 构建工具管理的项目中,通常将配置文件放置于 `src/main/resources/` 目录下[^1]。 对于 YAML 格式的配置文件(即 `application.yml`),其功能与 `.properties` 文件相同,但支持更复杂的嵌套结构[^3]。 #### 2. 基础配置项 无论是使用 `.properties` 还是 `.yml` 文件,都可以通过键值对的形式指定配置项。例如: ##### 使用 `application.properties` ```properties server.port=8081 spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=root spring.datasource.password=password logging.level.root=WARN ``` ##### 使用 `application.yml` ```yaml server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3306/testdb username: root password: password logging: level: root: WARN ``` 上述示例展示了如何更改服务器端口、数据库连接信息以及调整日志级别。 #### 3. 加载顺序 Spring Boot 按照一定的优级加载多个可能存在的配置文件。默认加载顺序如下: 1. **命令行参数**:最高优级。 2. **SPRING_APPLICATION_JSON 中的属性**:JSON 字符串形式的内联配置。 3. **ServletConfig 初始化参数** 和 **ServletContext 初始化参数**。 4. **Java System 属性 (System.getProperties())**。 5. **操作系统环境变量**。 6. **random.* 属性 (仅限 random)**。 7. **打包外部配置文件** (`application.properties`)。 8. **类路径中的配置文件** (`application.properties`)。 此加载顺序确保高优级的配置能够覆盖低优级的配置。 #### 4. 动态刷新配置 某些场景下,可能希望在不重启应用的情况下更新配置。这可以通过引入 Actuator 组件并启用动态刷新实现。具体操作包括: - 添加依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` - 启用配置刷新: ```java @Configuration public class RefreshConfiguration { @RefreshScope @Bean public MyService myService() { return new MyService(); } } ``` 当修改远程配置源(如 Config Server)时,发送 POST 请求至 `/actuator/refresh` 即可触发重新加载[^2]。 #### 5. 日志调试 为了验证配置是否生效,可以在开发环境中将日志等级设为 DEBUG 并观察控制台输出。例如: ```properties logging.level.org.springframework=DEBUG ``` --- ### 示例代码 下面是一个完整的 Spring Boot 应用程序示例,展示如何读取配置文件的内容: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${my.custom.message}") private String customMessage; // 自定义消息 @GetMapping("/hello") public String sayHello() { return "Custom Message from properties file: " + customMessage; } } ``` 对应的 `application.properties` 文件内容: ```properties my.custom.message=Welcome to the world of Spring Boot! ``` 访问 URL `/hello` 将返回自定义的消息字符串。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值