第1章:spring注入/1.6 一个Bean依赖另外一个Bean/1.6.1 @DependOn方式/1.6.1.2 单例模式
作者:互联网
- 非单例模式
- 需求:一个类的属性中要用到另外一个类的Bean,而且要求属性Bean要先初始化,并且要求是单例的
- 配置文件,它集成了很多配置项,比如:
authcode:
check:
num: 5
time: 120
- 被依赖的类(比如配置类),很多类都会用到这个配置项,比如:
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初始化"); } }
- 依赖类,它有一个属性依赖配置类,比如:
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; } }
- 定义要依赖的属性
@Autowired
ConfigParam configParam;
- 创建无参数改造函数
/**
* 默认改造函数
*/
private DependOnSingleton() {
System.out.println("无参数私有构造器");
}
- 创建单例
// 静态类方式:利用类在加载时线程互相排斥的属性进行唯一初始化对象
private static class SingletonFactory {
private static DependOnSingleton singleton = new DependOnSingleton();
}
// 利用静态类方式
public static DependOnSingleton getSignleton() {
return SingletonFactory.singleton;
}
- 定义初始化话完成后需要进行的操作:
@PostConstruct 第2章:spring中的Bean/2.5 Bean的周期回调/2.5.4 注解方式方式定义回调方法
public void init() throws Exception {
System.out.println("初始化完成后调用:" + configParam);
}
- 定义要依赖的属性
- 创建一个配置类,然后在配置类中使用@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;
}
}
- 使用依赖类,比如在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 "未超时"; } } }
- 启动过程日志:
ConfigParam初始化
获取单例构造器:AuthCodeManage
无参数私有构造器
初始化完成后调用:ConfigParam [checkNum=5, checkTime=120]
- 调用:http://localhost:8080/user/
- 完整源代码:
标签:1.6,return,spring,public,Bean,configParam,import,DependOnSingleton,ConfigParam 来源: https://blog.csdn.net/u011830122/article/details/92088774