• Spring 之工具类中注入bin


    /**
     * The type Redis utils.
     *
     */
    @Component
    public  class RedisUtils {
        /**
         * 注入redisTemplate bean
         */
        private static RedisTemplate<String,Object> redisTemplate;
    
        @Autowired
        public void setRedisService(RedisTemplate<String,Object> redisTemplate){
            RedisUtils.redisTemplate = redisTemplate;
        }
    }
    
    注意:
    RedisUtils 添加 @Component 声明其为bean组件,放到set方法中,使用 @Autowired注入启动类。
    
    
    
    @Component
    public  class RedisUtils {
        //声明一个静态的属性(加上注解@Autowired)
        @Autowired
        private RedisTemplate<String,Object> template;
    
        //声明一个非静态的属性
        private static RedisTemplate<String,Object> redisTemplate;
    
        @PostConstruct
        public void init(){
            redisTemplate = template;
        }
    }
    注意:
    使用@PostConstruct注解,被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的init()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前执行。
    无为而治
  • 相关阅读:
    Educational Codeforces Round 81 (Rated for Div. 2) A-E
    SEERC 2018 I
    manjaro linux java环境配置
    Pangu and Stones HihoCoder
    Linux下 vim 的配置
    C++内存管理技术
    Interview_C++_day27
    Interview_C++_day26
    Interview_C++_day25
    Interview_数据库_day24
  • 原文地址:https://www.cnblogs.com/wangchuanfu/p/15783398.html
Copyright © 2020-2023  润新知