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
@@ -1,8 +1,5 @@
package com.baeldung.examples.common;
import org.springframework.stereotype.Component;
@Component
public class Account {
private String accountNumber;
@@ -1,8 +1,5 @@
package com.baeldung.examples.common;
import org.springframework.stereotype.Component;
@Component
public class AccountServiceImpl implements AccountService {
}
@@ -1,10 +1,7 @@
package com.baeldung.examples.common;
import org.springframework.beans.factory.annotation.Autowired;
public class BookServiceImpl implements BookService {
@Autowired(required = false)
private AuthorService authorService;
}
@@ -1,8 +1,5 @@
package com.baeldung.examples.common;
import org.springframework.stereotype.Component;
@Component
public class PersonDaoImpl implements PersonDao {
}
@@ -1,12 +1,9 @@
package com.baeldung.examples.guice;
import org.springframework.lang.Nullable;
import com.google.inject.Inject;
public class FooProcessor {
@Inject
@Nullable
private Foo foo;
}
@@ -27,7 +27,7 @@ public class GuiceModule extends AbstractModule {
// });
bind(Foo.class).toProvider(new Provider<Foo>() {
public Foo get() {
return null;
return new Foo();
}
});
bind(PersonDao.class).to(PersonDaoImpl.class);
@@ -1,17 +0,0 @@
package com.baeldung.examples.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.examples.common.AudioBookService;
import com.baeldung.examples.common.AudioBookServiceImpl;
@Configuration
public class SpringBeansConfig {
@Bean
public AudioBookService audioBookServiceGenerator() {
return new AudioBookServiceImpl();
}
}
@@ -1,21 +0,0 @@
package com.baeldung.examples.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;
import com.baeldung.examples.common.BookService;
import com.baeldung.examples.common.BookServiceImpl;
@Configuration
@Import({ SpringBeansConfig.class })
@ComponentScan("com.baeldung.examples")
public class SpringMainConfig {
@Bean
public BookService bookServiceGenerator() {
return new BookServiceImpl();
}
}
@@ -1,22 +0,0 @@
package com.baeldung.examples.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.examples.common.PersonDao;
@Component
public class SpringPersonService {
@Autowired
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}
@@ -1,22 +0,0 @@
package com.baeldung.examples.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.examples.common.AccountService;
@Component
public class UserService {
@Autowired
private AccountService accountService;
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}
@@ -1,63 +0,0 @@
package com.baeldung.examples;
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;
import com.baeldung.examples.common.AudioBookService;
import com.baeldung.examples.common.BookService;
import com.baeldung.examples.spring.SpringMainConfig;
import com.baeldung.examples.spring.SpringPersonService;
import com.baeldung.examples.spring.UserService;
@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());
}
}