1.在类上直接加注解@Component,那么这个类就直接注入到Spring容器中了 ,像@Contrloller,@Service这些本质上都是@Component, 2.@Configuration(或者@SpringBootConfiguration)放到类上,然后在类中的方法上加注解@Bean @SpringBootConfigurationpublic class UseConditionalOnBean { @Bean public User createBean(){ retur
在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务.而通过Spring注入来管理和使用服务是较为合理的方式.但是若直接在Thread子类中通过注解方式注入Bean是无效的. 因为Spring本身默认Bean为单例模式构建,同时是非线程安全的,因此禁止了在Thread子类中的注入行为,因此在Thread中直接注入的bean是null的,会发生空指针错误. 以下分别列举错误的注入方法和两种解决方式. 错误的注入方法 @Controller public class
最近,在项目开发过程中使用了RedisTemplate,进行单元测试时提示“Field redisTemplate in com.example.demo1.dao.RedisDao required a bean of type ‘org.springframework.data.redis.core.RedisTemplate’ that could not be found”,翻译过来就是“找不到类型为RedisTemplate的bean”.当然,仅看这句话我们无法确定为什么会出现这个问题
https://www.dazhuanlan.com/2019/10/22/5daebc5d16429/ 最近在做传统Spring项目到SpringBoot项目迁移过程中,遇到了一些bean加载顺序的问题:比如一个config中的bean依赖于另一个config中的bean进行初始化,于是查了一些资料,出现了一些新的概念: @Order @AutoConfigureAfter @DependsOn @Order注解 Before Spring 4.0, the @Order annotation