* Fix a division method mistake

* What a Spring bean is

* Update

* 2nd update
This commit is contained in:
nguyennamthai 2018-09-23 17:46:26 +07:00 committed by Grzegorz Piwowarek
parent 27b53f070a
commit b9970c5e3a
4 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.baeldung.definition;
import com.baeldung.definition.domain.Address;
import com.baeldung.definition.domain.Company;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackageClasses = Company.class)
public class Config {
@Bean
public Address getAddress() {
return new Address("High Street", 1000);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.definition.domain;
import lombok.Data;
@Data
public class Address {
private String street;
private int number;
public Address(String street, int number) {
this.street = street;
this.number = number;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.definition.domain;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@Component
public class Company {
private Address address;
public Company(Address address) {
this.address = address;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.definition;
import com.baeldung.definition.domain.Company;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.assertEquals;
public class SpringBeanIntegrationTest {
@Test
public void whenUsingIoC_thenDependenciesAreInjected() {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Company company = context.getBean("company", Company.class);
assertEquals("High Street", company.getAddress().getStreet());
assertEquals(1000, company.getAddress().getNumber());
}
}