File tree Expand file tree Collapse file tree
main/java/com/baeldung/definition
test/java/com/baeldung/definition Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .definition ;
2+
3+ import com .baeldung .definition .domain .Address ;
4+ import com .baeldung .definition .domain .Company ;
5+ import org .springframework .context .annotation .Bean ;
6+ import org .springframework .context .annotation .ComponentScan ;
7+ import org .springframework .context .annotation .Configuration ;
8+
9+ @ Configuration
10+ @ ComponentScan (basePackageClasses = Company .class )
11+ public class Config {
12+ @ Bean
13+ public Address getAddress () {
14+ return new Address ("High Street" , 1000 );
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .definition .domain ;
2+
3+ import lombok .Data ;
4+
5+ @ Data
6+ public class Address {
7+ private String street ;
8+ private int number ;
9+
10+ public Address (String street , int number ) {
11+ this .street = street ;
12+ this .number = number ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .definition .domain ;
2+
3+ import lombok .Data ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Data
7+ @ Component
8+ public class Company {
9+ private Address address ;
10+
11+ public Company (Address address ) {
12+ this .address = address ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .definition ;
2+
3+ import com .baeldung .definition .domain .Company ;
4+ import org .junit .Test ;
5+ import org .springframework .context .ApplicationContext ;
6+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
7+
8+ import static org .junit .Assert .assertEquals ;
9+
10+ public class SpringBeanIntegrationTest {
11+ @ Test
12+ public void whenUsingIoC_thenDependenciesAreInjected () {
13+ ApplicationContext context = new AnnotationConfigApplicationContext (Config .class );
14+ Company company = context .getBean ("company" , Company .class );
15+ assertEquals ("High Street" , company .getAddress ().getStreet ());
16+ assertEquals (1000 , company .getAddress ().getNumber ());
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments