- 非单例模式
- 需求:一个类的属性中要用到另外一个类的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 DependOnNoSingleton { @Autowired ConfigParam configParam; public DependOnNoSingleton(ConfigParam configParam) { System.out.println(configParam); } /** * 默认改造函数 */ public DependOnNoSingleton() { System.out.println("无参数构造器"); } @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;
- 创建无参数改造函数
/**
* 默认改造函数
*/
public DependOnNoSingleton() {
System.out.println("无参数构造器");
}
- 定义初始化话完成后需要进行的操作:
@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 AuthCodeManage authCodeManage() { System.out.println("创建AuthCodeManage"); AuthCodeManage authCodeManage = new AuthCodeManage(); return authCodeManage; } }
@Configuration
public class BeanConfig {
@Bean(name = "configParam")
public ConfigParam configParam() {
return new ConfigParam();
}
@Bean
@DependsOn(value = { "configParam" })
public DependOnNoSingleton dependOnNoSingleton() {
System.out.println("创建AuthCodeManage");
DependOnNoSingleton authCodeManage = new DependOnNoSingleton();
return authCodeManage;
}
}
- 使用依赖类,比如在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.DependOnNoSingleton; @RestController public class MyUserControl { @Autowired DependOnNoSingleton dependOnNoSingleton; @GetMapping("user") public String getUser() { boolean b = dependOnNoSingleton.isTimeout(150); if (b) { return "已经超时"; } else { return "未超时"; } } }
- 启动过程日志:
ConfigParam初始化
创建AuthCodeManage
无参数构造器
初始化完成后调用:ConfigParam [checkNum=5, checkTime=120]
- 调用:http://localhost:8080/user/
- 完整源代码: