BAEL-2399: Migrate Spring DI example to spring-core module
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AccountService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AccountServiceImpl implements AccountService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AudioBookService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class AudioBookServiceImpl implements AudioBookService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AuthorService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class AuthorServiceImpl implements AuthorService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface BookService {
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class Foo {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class FooProcessor {
|
||||
|
||||
private Foo foo;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user