BAEL-2399: Migrate Spring DI example to spring-core module

This commit is contained in:
eric-martin
2019-02-05 22:14:51 -06:00
parent 874acddfcf
commit 01fa4cfaf8
26 changed files with 129 additions and 78 deletions

View File

@@ -0,0 +1,27 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class Account {
private String accountNumber;
private String type;
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface AccountService {
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class AccountServiceImpl implements AccountService {
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface AudioBookService {
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public class AudioBookServiceImpl implements AudioBookService {
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface AuthorService {
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public class AuthorServiceImpl implements AuthorService {
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface BookService {
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.di.spring;
import org.springframework.beans.factory.annotation.Autowired;
public class BookServiceImpl implements BookService {
@Autowired(required = false)
private AuthorService authorService;
}

View File

@@ -0,0 +1,4 @@
package com.baeldung.di.spring;
public class Foo {
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.di.spring;
public class FooProcessor {
private Foo foo;
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface PersonDao {
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class PersonDaoImpl implements PersonDao {
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.di.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringBeansConfig {
@Bean
public AudioBookService audioBookServiceGenerator() {
return new AudioBookServiceImpl();
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.di.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ SpringBeansConfig.class })
@ComponentScan("com.baeldung.di.spring")
public class SpringMainConfig {
@Bean
public BookService bookServiceGenerator() {
return new BookServiceImpl();
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.di.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SpringPersonService {
@Autowired
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.di.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserService {
@Autowired
private AccountService accountService;
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}

View File

@@ -0,0 +1,57 @@
package com.baeldung.di.spring;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { SpringMainConfig.class })
public class SpringUnitTest {
@Autowired
ApplicationContext context;
@Test
public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
UserService userService = context.getBean(UserService.class);
assertNotNull(userService.getAccountService());
}
@Test
public void givenBookServiceIsRegisteredAsBeanInContext_WhenBookServiceIsRetrievedFromContext_ThenReturnValueIsNotNull() {
BookService bookService = context.getBean(BookService.class);
assertNotNull(bookService);
}
@Test
public void givenBookServiceIsRegisteredAsBeanInContextByOverridingAudioBookService_WhenAudioBookServiceIsRetrievedFromContext_ThenNoSuchBeanDefinitionExceptionIsThrown() {
BookService bookService = context.getBean(BookService.class);
assertNotNull(bookService);
AudioBookService audioBookService = context.getBean(AudioBookService.class);
assertNotNull(audioBookService);
}
@Test
public void givenAuthorServiceAutowiredToBookServiceAsOptionalDependency_WhenBookServiceIsRetrievedFromContext_ThenNoSuchBeanDefinitionExceptionIsNotThrown() {
BookService bookService = context.getBean(BookService.class);
assertNotNull(bookService);
}
@Test
public void givenSpringPersonServiceConstructorAnnotatedByAutowired_WhenSpringPersonServiceIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() {
SpringPersonService personService = context.getBean(SpringPersonService.class);
assertNotNull(personService);
}
@Test
public void givenPersonDaoAutowiredToSpringPersonServiceBySetterInjection_WhenSpringPersonServiceRetrievedFromContext_ThenPersonDaoInitializedByTheSetter() {
SpringPersonService personService = context.getBean(SpringPersonService.class);
assertNotNull(personService);
assertNotNull(personService.getPersonDao());
}
}