第1章:spring注入/1.6 一个Bean依赖另外一个Bean/1.6.1 @DependOn方式/1.6.1.2 单例模式

  • 非单例模式
  1. 需求:一个类的属性中要用到另外一个类的Bean,而且要求属性Bean要先初始化,并且要求是单例的
  2. 配置文件,它集成了很多配置项,比如:

    authcode:

       check:

          num: 5

          time: 120

  3. 被依赖的类(比如配置类),很多类都会用到这个配置项,比如:
    package com.yxbj.config;
    
    import org.springframework.beans.factory.annotation.Value;
    
    public class ConfigParam {
    
    	@Value("${authcode.check.num}")
    	int checkNum;
    
    	@Value("${authcode.check.time}")
    	int checkTime;
    
    	public int getCheckNum() {
    		return checkNum;
    	}
    
    	public void setCheckNum(int checkNum) {
    		this.checkNum = checkNum;
    	}
    
    	public int getCheckTime() {
    		return checkTime;
    	}
    
    	public void setCheckTime(int checkTime) {
    		this.checkTime = checkTime;
    	}
    
    	@Override
    	public String toString() {
    		return "ConfigParam [checkNum=" + checkNum + ", checkTime=" + checkTime + "]";
    	}
    
    	public ConfigParam() {
    		System.out.println("ConfigParam初始化");
    	}
    
    }
    
  4. 依赖类,它有一个属性依赖配置类,比如:
    package com.yxbj.authcode;
    
    import javax.annotation.PostConstruct;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.yxbj.config.ConfigParam;
    
    public class DependOnSingleton {
    
    	@Autowired
    	ConfigParam configParam;
    
    	public DependOnSingleton(ConfigParam configParam) {
    		System.out.println(configParam);
    	}
    
    	/**
    	 * 默认改造函数
    	 */
    	private DependOnSingleton() {
    		System.out.println("无参数私有构造器");
    	}
    
    	// 静态类方式:利用类在加载时线程互相排斥的属性进行唯一初始化对象
    	private static class SingletonFactory {
    		private static DependOnSingleton singleton = new DependOnSingleton();
    	}
    
    	// 利用静态类方式
    	public static DependOnSingleton getSignleton() {
    		return SingletonFactory.singleton;
    	}
    
    	@PostConstruct
    	public void init() throws Exception {
    		System.out.println("初始化完成后调用:" + configParam);
    	}
    
    	public boolean isTimeout(int time) {
    		System.out.println(configParam);
    		if (time > configParam.getCheckTime()) {
    			return true;
    		}
    		return false;
    	}
    }
    
    1. 定义要依赖的属性

      @Autowired

      ConfigParam configParam;

    2. 创建无参数改造函数

      /**

            * 默认改造函数

            */

           private DependOnSingleton() {

                 System.out.println("无参数私有构造器");

           }

    3. 创建单例

      // 静态类方式:利用类在加载时线程互相排斥的属性进行唯一初始化对象

           private static class SingletonFactory {

                 private static DependOnSingleton singleton = new DependOnSingleton();

           }

           // 利用静态类方式

           public static DependOnSingleton getSignleton() {

                 return SingletonFactory.singleton;

           }

    4. 定义初始化话完成后需要进行的操作:

      @PostConstruct  第2章:spring中的Bean/2.5 Bean的周期回调/2.5.4 注解方式方式定义回调方法

           public void init() throws Exception {

                 System.out.println("初始化完成后调用:" + configParam);

           }

  5. 创建一个配置类,然后在配置类中使用@DependOn确认初始化顺序,比如:
    package com.yxbj.authcode;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.DependsOn;
    
    import com.yxbj.config.ConfigParam;
    
    @Configuration
    public class BeanConfig {
    
    	@Bean(name = "configParam")
    	public ConfigParam configParam() {
    		return new ConfigParam();
    	}
    
    	@Bean
    	@DependsOn(value = { "configParam" })
    	public DependOnSingleton dependOnNoSingleton() {
    		System.out.println("获取单例构造器:AuthCodeManage");
    		DependOnSingleton dependOnSingleton = DependOnSingleton.getSignleton();
    		return dependOnSingleton;
    	}
    
    }
    

    @Configuration

    public class BeanConfig {

         @Bean(name = "configParam")

         public ConfigParam configParam() {

               return new ConfigParam();

         }

         @Bean

         @DependsOn(value = { "configParam" })

         public DependOnSingleton dependOnNoSingleton() {

               System.out.println("获取单例构造器:AuthCodeManage");

               DependOnSingleton dependOnSingleton = DependOnSingleton.getSignleton();

               return dependOnSingleton;

         }

    }

  6. 使用依赖类,比如在Control中使用:
    package com.yxbj.control;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.yxbj.authcode.DependOnSingleton;
    
    @RestController
    public class MyUserControl {
    
    	@Autowired
    	DependOnSingleton dependOnSingleton;
    
    	@GetMapping("user")
    	public String getUser() {
    		boolean b = dependOnSingleton.isTimeout(150);
    		if (b) {
    			return "已经超时";
    		} else {
    			return "未超时";
    		}
    	}
    }
    
  7. 启动过程日志:

    ConfigParam初始化

    获取单例构造器:AuthCodeManage

    无参数私有构造器

    初始化完成后调用:ConfigParam [checkNum=5, checkTime=120]

  8. 调用:http://localhost:8080/user/
  9. 完整源代码:
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易学笔记(qq:1776565180)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值