Bael 2110 (#5286)
* Fix a division method mistake * What a Spring bean is * Update * 2nd update
This commit is contained in:
parent
27b53f070a
commit
b9970c5e3a
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue