美文网首页
Springboot @Component 使用@Autowir

Springboot @Component 使用@Autowir

作者: 莫夏_b560 | 来源:发表于2020-06-10 13:23 被阅读0次

解决方案
1、被引用的bean,部分可以转换成静态方法,如果工具类里面还需要@Autowired引入其他的bean,可以

    private static RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisUtil(RedisTemplate<String, Object> redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

2、在引用类下,使用 @PostConstruct

3、@Confuguation结合#ConfigurationScan(basePackages="xxxxx")

4、@Component标注的类下使用@Value读取配置的值失败。修改如下:

    @Value("${shiroCacheExpireTime}")
    private String shiroCacheExpireTimeS;

    private static String shiroCacheExpireTime;

    @PostConstruct
    public void setShiroCacheExpireTime(){
        shiroCacheExpireTime = this.shiroCacheExpireTimeS;
    }

相关文章

网友评论

      本文标题:Springboot @Component 使用@Autowir

      本文链接:https://www.haomeiwen.com/subject/msoutktx.html