spring-将bean注入到enum
假设需要往YesNoEnum枚举类中注入User对象
public enum YesNoEnum {
YES(1,"yes"),
NO(0,"no");
private Integer code;
private String name;
// 注入user对象
private User user;
public void setUser(User user){
this.user = user;
}
@Component
public static class ReportTypeServiceInjector {
@Autowired
private User user;
@PostConstruct
public void postConstruct() {
for (YesNoEnum yesNoEnum : EnumSet.allOf(YesNoEnum.class)){
yesNoEnum.setUser(user);
}
}
}
YesNoEnum(Integer code, String name){
this.code = code;
this.name = name;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}