Skip to content

Instantly share code, notes, and snippets.

@ebongzzang
Last active May 29, 2018 06:31
Show Gist options
  • Save ebongzzang/a68a2cfeba89d4f0400bd8f30331f777 to your computer and use it in GitHub Desktop.
Save ebongzzang/a68a2cfeba89d4f0400bd8f30331f777 to your computer and use it in GitHub Desktop.
Spring Nested Property binding and configuration injection example
@Configuration
@ConfigurationProperties(prefix = "student")
public class AllStudentConfig {
@Setter @Getter
private StudentProperties younghee;
@Setter @Getter
private StudentProperties chulsoo;
public static final String CHULSOO_HOBBY = "sleeping";
public static final String YOUNGHEE_HOBBY = "eating";
@Primary
@Bean(name = "chulsoo")
public StudentProperties chulsooConfig() {
StudentProperties chulsooConfig = this.chulsoo;
chulsooConfig.setHobby(CULSOO_HOBBY)
return chulsooConfig;
}
@Bean(name = "younghee")
public StudentProperties youngheeConfig() {
StudentProperties youngheeConfig = this.telaria;
youngheeConfig.setHobby(YOUNGHEE_HOBBY)
return youngheeConfig;
}
@Data
@NoArgsConstructor(access = AccessLevel.PUBLIC)
public static class StudentProperties {
private int age;
private String grade;
private String school;
private String hobby;
}
}
student:
chulsoo:
age: 30
grade: 1
school: 'awesome school'
younghee:
age: 31
grade: 1
school: 'very cool school'
@Service
@Slf4j
class SchoolService {
private StudentProperties younghee;
private StudentProperties chulsoo;
@Autowired
public SchoolService(@Qualifier("younghee") StduentProperties youngheeProp, @Qualifier("chulsoo") StduentProperties chulsooProp) {
this.younghee = youngheeProp;
this.chulsoo = chulsooProp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment