Last active
May 29, 2018 06:31
-
-
Save ebongzzang/a68a2cfeba89d4f0400bd8f30331f777 to your computer and use it in GitHub Desktop.
Spring Nested Property binding and configuration injection example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
student: | |
chulsoo: | |
age: 30 | |
grade: 1 | |
school: 'awesome school' | |
younghee: | |
age: 31 | |
grade: 1 | |
school: 'very cool school' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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