Merge pull request #5986 from codehunter34/BAEL-2399
BAEL-2399: Guice vs Spring - Dependency Injection
This commit is contained in:
commit
874acddfcf
|
@ -1,29 +1,45 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.examples.guice</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>guice</name>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.examples.guice</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>guice</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>${guice.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>${guice.version}</version>
|
||||
</dependency>
|
||||
|
||||
<properties>
|
||||
<guice.version>4.1.0</guice.version>
|
||||
</properties>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${springtest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<guice.version>4.2.2</guice.version>
|
||||
<spring.version>5.1.3.RELEASE</spring.version>
|
||||
<springtest.version>5.1.3.RELEASE</springtest.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
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.examples.common;
|
||||
|
||||
public interface AccountService {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AccountServiceImpl implements AccountService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
public interface AudioBookService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
public class AudioBookServiceImpl implements AudioBookService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
public interface AuthorService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
public class AuthorServiceImpl implements AuthorService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
public interface BookService {
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class BookServiceImpl implements BookService {
|
||||
|
||||
@Autowired(required = false)
|
||||
private AuthorService authorService;
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.examples.guice;
|
||||
|
||||
public class Foo {
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.examples.guice;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class FooProcessor {
|
||||
|
||||
@Inject
|
||||
@Nullable
|
||||
private Foo foo;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.examples.guice;
|
||||
|
||||
import com.baeldung.examples.common.PersonDao;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class GuicePersonService {
|
||||
|
||||
@Inject
|
||||
private PersonDao personDao;
|
||||
|
||||
public PersonDao getPersonDao() {
|
||||
return personDao;
|
||||
}
|
||||
|
||||
public void setPersonDao(PersonDao personDao) {
|
||||
this.personDao = personDao;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.examples.guice;
|
||||
|
||||
import com.baeldung.examples.common.AccountService;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class GuiceUserService {
|
||||
|
||||
@Inject
|
||||
private AccountService accountService;
|
||||
|
||||
public AccountService getAccountService() {
|
||||
return accountService;
|
||||
}
|
||||
|
||||
public void setAccountService(AccountService accountService) {
|
||||
this.accountService = accountService;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.examples.guice;
|
||||
|
||||
public class Person {
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.examples.guice.modules;
|
||||
|
||||
import com.baeldung.examples.common.AccountService;
|
||||
import com.baeldung.examples.common.AccountServiceImpl;
|
||||
import com.baeldung.examples.common.BookService;
|
||||
import com.baeldung.examples.common.BookServiceImpl;
|
||||
import com.baeldung.examples.common.PersonDao;
|
||||
import com.baeldung.examples.common.PersonDaoImpl;
|
||||
import com.baeldung.examples.guice.Foo;
|
||||
import com.baeldung.examples.guice.Person;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Provides;
|
||||
|
||||
public class GuiceModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
try {
|
||||
bind(AccountService.class).to(AccountServiceImpl.class);
|
||||
bind(Person.class).toConstructor(Person.class.getConstructor());
|
||||
// bind(Person.class).toProvider(new Provider<Person>() {
|
||||
// public Person get() {
|
||||
// Person p = new Person();
|
||||
// return p;
|
||||
// }
|
||||
// });
|
||||
bind(Foo.class).toProvider(new Provider<Foo>() {
|
||||
public Foo get() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
bind(PersonDao.class).to(PersonDaoImpl.class);
|
||||
|
||||
} catch (NoSuchMethodException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (SecurityException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Provides
|
||||
public BookService bookServiceGenerator() {
|
||||
return new BookServiceImpl();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.baeldung.examples;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.examples.common.BookService;
|
||||
import com.baeldung.examples.guice.FooProcessor;
|
||||
import com.baeldung.examples.guice.GuicePersonService;
|
||||
import com.baeldung.examples.guice.GuiceUserService;
|
||||
import com.baeldung.examples.guice.Person;
|
||||
import com.baeldung.examples.guice.modules.GuiceModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
public class GuiceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuiceUserService guiceUserService = injector.getInstance(GuiceUserService.class);
|
||||
assertNotNull(guiceUserService.getAccountService());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
BookService bookService = injector.getInstance(BookService.class);
|
||||
assertNotNull(bookService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
Person person = injector.getInstance(Person.class);
|
||||
assertNotNull(person);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFooInjectedToFooProcessorAsOptionalDependency_WhenFooProcessorIsRetrievedFromContext_ThenCreationExceptionIsNotThrown() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
FooProcessor fooProcessor = injector.getInstance(FooProcessor.class);
|
||||
assertNotNull(fooProcessor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuicePersonServiceConstructorAnnotatedByInject_WhenGuicePersonServiceIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||
assertNotNull(personService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonDaoInjectedToGuicePersonServiceBySetterInjection_WhenGuicePersonServiceIsInjected_ThenPersonDaoInitializedByTheSetter() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||
assertNotNull(personService);
|
||||
assertNotNull(personService.getPersonDao());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue