From 8880d96d3a0c6c5dcdc0f188756d89543fec7481 Mon Sep 17 00:00:00 2001 From: codehunter34 Date: Sun, 23 Dec 2018 16:41:11 -0500 Subject: [PATCH 1/6] BAEL-2399: Guice vs Spring - Dependency Injection --- .../com/baeldung/examples/common/Account.java | 27 +++++++ .../examples/common/AccountService.java | 8 +++ .../examples/common/AccountServiceImpl.java | 15 ++++ .../com/baeldung/examples/common/Address.java | 35 +++++++++ .../baeldung/examples/common/BookService.java | 9 +++ .../examples/common/BookServiceImpl.java | 12 ++++ .../com/baeldung/examples/guice/Employee.java | 32 +++++++++ .../java/com/baeldung/examples/guice/Foo.java | 5 ++ .../baeldung/examples/guice/FooGenerator.java | 11 +++ .../baeldung/examples/guice/GuiceUser.java | 32 +++++++++ .../examples/guice/GuiceUserService.java | 19 +++++ .../com/baeldung/examples/guice/Person.java | 39 ++++++++++ .../examples/guice/modules/GuiceModule.java | 47 ++++++++++++ .../baeldung/examples/spring/AppConfig.java | 19 +++++ .../baeldung/examples/spring/SpringUser.java | 35 +++++++++ .../com/baeldung/examples/spring/Student.java | 34 +++++++++ .../baeldung/examples/spring/UserService.java | 22 ++++++ .../com/baeldung/examples/GuiceTests.java | 72 +++++++++++++++++++ .../com/baeldung/examples/SpringTests.java | 58 +++++++++++++++ 19 files changed, 531 insertions(+) create mode 100644 guice/src/main/java/com/baeldung/examples/common/Account.java create mode 100644 guice/src/main/java/com/baeldung/examples/common/AccountService.java create mode 100644 guice/src/main/java/com/baeldung/examples/common/AccountServiceImpl.java create mode 100644 guice/src/main/java/com/baeldung/examples/common/Address.java create mode 100644 guice/src/main/java/com/baeldung/examples/common/BookService.java create mode 100644 guice/src/main/java/com/baeldung/examples/common/BookServiceImpl.java create mode 100644 guice/src/main/java/com/baeldung/examples/guice/Employee.java create mode 100644 guice/src/main/java/com/baeldung/examples/guice/Foo.java create mode 100644 guice/src/main/java/com/baeldung/examples/guice/FooGenerator.java create mode 100644 guice/src/main/java/com/baeldung/examples/guice/GuiceUser.java create mode 100644 guice/src/main/java/com/baeldung/examples/guice/GuiceUserService.java create mode 100644 guice/src/main/java/com/baeldung/examples/guice/Person.java create mode 100644 guice/src/main/java/com/baeldung/examples/guice/modules/GuiceModule.java create mode 100644 guice/src/main/java/com/baeldung/examples/spring/AppConfig.java create mode 100644 guice/src/main/java/com/baeldung/examples/spring/SpringUser.java create mode 100644 guice/src/main/java/com/baeldung/examples/spring/Student.java create mode 100644 guice/src/main/java/com/baeldung/examples/spring/UserService.java create mode 100644 guice/src/test/java/com/baeldung/examples/GuiceTests.java create mode 100644 guice/src/test/java/com/baeldung/examples/SpringTests.java diff --git a/guice/src/main/java/com/baeldung/examples/common/Account.java b/guice/src/main/java/com/baeldung/examples/common/Account.java new file mode 100644 index 0000000000..09a9d80b6a --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/Account.java @@ -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; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/common/AccountService.java b/guice/src/main/java/com/baeldung/examples/common/AccountService.java new file mode 100644 index 0000000000..d6a7fe1d11 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/AccountService.java @@ -0,0 +1,8 @@ +package com.baeldung.examples.common; + +import java.util.List; + +public interface AccountService { + public List listAccountTypes(); + +} diff --git a/guice/src/main/java/com/baeldung/examples/common/AccountServiceImpl.java b/guice/src/main/java/com/baeldung/examples/common/AccountServiceImpl.java new file mode 100644 index 0000000000..9cecf021fe --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/AccountServiceImpl.java @@ -0,0 +1,15 @@ +package com.baeldung.examples.common; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.stereotype.Component; + +@Component +public class AccountServiceImpl implements AccountService { + + public List listAccountTypes() { + return Arrays.asList("Checking", "Saving"); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/common/Address.java b/guice/src/main/java/com/baeldung/examples/common/Address.java new file mode 100644 index 0000000000..3b07c47011 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/Address.java @@ -0,0 +1,35 @@ +package com.baeldung.examples.common; + +import org.springframework.stereotype.Component; + +@Component +public class Address { + private String city; + private String state; + private String county; + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getCounty() { + return county; + } + + public void setCounty(String county) { + this.county = county; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/common/BookService.java b/guice/src/main/java/com/baeldung/examples/common/BookService.java new file mode 100644 index 0000000000..3aca9d90d7 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/BookService.java @@ -0,0 +1,9 @@ +package com.baeldung.examples.common; + +import java.util.List; + +public interface BookService { + + public List findBestSellerBooks(); + +} diff --git a/guice/src/main/java/com/baeldung/examples/common/BookServiceImpl.java b/guice/src/main/java/com/baeldung/examples/common/BookServiceImpl.java new file mode 100644 index 0000000000..b5499c678d --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/BookServiceImpl.java @@ -0,0 +1,12 @@ +package com.baeldung.examples.common; + +import java.util.Arrays; +import java.util.List; + +public class BookServiceImpl implements BookService { + + public List findBestSellerBooks() { + return Arrays.asList("Harry Potter", "Lord of The Rings"); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/Employee.java b/guice/src/main/java/com/baeldung/examples/guice/Employee.java new file mode 100644 index 0000000000..b85d251a05 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/guice/Employee.java @@ -0,0 +1,32 @@ +package com.baeldung.examples.guice; + +import com.google.inject.Inject; + +public class Employee { + + private String firstName; + private String lastName; + + @Inject + public Employee(String firstName) { + this.firstName = firstName; + this.lastName = "Default"; + } + + 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; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/Foo.java b/guice/src/main/java/com/baeldung/examples/guice/Foo.java new file mode 100644 index 0000000000..a4f22a6f69 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/guice/Foo.java @@ -0,0 +1,5 @@ +package com.baeldung.examples.guice; + + +public class Foo { +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/FooGenerator.java b/guice/src/main/java/com/baeldung/examples/guice/FooGenerator.java new file mode 100644 index 0000000000..b2d3309d0e --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/guice/FooGenerator.java @@ -0,0 +1,11 @@ +package com.baeldung.examples.guice; + +import org.springframework.lang.Nullable; + +import com.google.inject.Inject; + +public class FooGenerator { + @Inject + public FooGenerator(@Nullable Foo foo) { + } +} \ No newline at end of file diff --git a/guice/src/main/java/com/baeldung/examples/guice/GuiceUser.java b/guice/src/main/java/com/baeldung/examples/guice/GuiceUser.java new file mode 100644 index 0000000000..df438add5b --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/guice/GuiceUser.java @@ -0,0 +1,32 @@ +package com.baeldung.examples.guice; + +import com.baeldung.examples.common.Account; +import com.baeldung.examples.common.Address; +import com.google.inject.Inject; + +public class GuiceUser { + + @Inject + private Account account; + + private Address address; + + public Account getAccount() { + return account; + } + + public void setAccount(Account account) { + this.account = account; + } + + public Address getAddress() { + return address; + } + + @Inject + public void setAddress(Address address) { + this.address = address; + address.setCity("Default"); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/GuiceUserService.java b/guice/src/main/java/com/baeldung/examples/guice/GuiceUserService.java new file mode 100644 index 0000000000..18cb076661 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/guice/GuiceUserService.java @@ -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; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/Person.java b/guice/src/main/java/com/baeldung/examples/guice/Person.java new file mode 100644 index 0000000000..45ee5f4b89 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/guice/Person.java @@ -0,0 +1,39 @@ +package com.baeldung.examples.guice; + +import com.baeldung.examples.common.Address; +import com.google.inject.Inject; + +public class Person { + private String firstName; + + private String lastName; + + private Address address; + + 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; + } + + public Address getAddress() { + return address; + } + + @Inject + public void setAddress(Address address) { + this.address = address; + address.setCity("Default"); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/guice/modules/GuiceModule.java b/guice/src/main/java/com/baeldung/examples/guice/modules/GuiceModule.java new file mode 100644 index 0000000000..44b566240e --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/guice/modules/GuiceModule.java @@ -0,0 +1,47 @@ +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.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(Foo.class).toProvider(new Provider() { + public Foo get() { + return null; + } + }); + + bind(Person.class).toConstructor(Person.class.getConstructor()); + // bind(Person.class).toProvider(new Provider() { + // public Person get() { + // Person p = new Person(); + // return p; + // } + // }); + } 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(); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/spring/AppConfig.java b/guice/src/main/java/com/baeldung/examples/spring/AppConfig.java new file mode 100644 index 0000000000..e5b6648e0d --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/spring/AppConfig.java @@ -0,0 +1,19 @@ +package com.baeldung.examples.spring; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.examples.common.BookService; +import com.baeldung.examples.common.BookServiceImpl; + +@Configuration +@ComponentScan("com.baeldung.examples") +public class AppConfig { + + @Bean + public BookService bookServiceGenerator() { + return new BookServiceImpl(); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/spring/SpringUser.java b/guice/src/main/java/com/baeldung/examples/spring/SpringUser.java new file mode 100644 index 0000000000..ba6aaab09b --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/spring/SpringUser.java @@ -0,0 +1,35 @@ +package com.baeldung.examples.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.baeldung.examples.common.Account; +import com.baeldung.examples.common.Address; + +@Component +public class SpringUser { + + @Autowired + private Account account; + + private Address address; + + public Account getAccount() { + return account; + } + + public void setAccount(Account account) { + this.account = account; + } + + public Address getAddress() { + return address; + } + + @Autowired + public void setAddress(Address address) { + this.address = address; + address.setCity("Default"); + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/spring/Student.java b/guice/src/main/java/com/baeldung/examples/spring/Student.java new file mode 100644 index 0000000000..d8b61a91ef --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/spring/Student.java @@ -0,0 +1,34 @@ +package com.baeldung.examples.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class Student { + private String firstName; + private String lastName; + + @Autowired + public Student(@Value("Default") String firstName, @Value("Default") String lastName) { + this.firstName = firstName; + this.lastName = 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; + } + +} diff --git a/guice/src/main/java/com/baeldung/examples/spring/UserService.java b/guice/src/main/java/com/baeldung/examples/spring/UserService.java new file mode 100644 index 0000000000..91b95c100d --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/spring/UserService.java @@ -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; + } + +} diff --git a/guice/src/test/java/com/baeldung/examples/GuiceTests.java b/guice/src/test/java/com/baeldung/examples/GuiceTests.java new file mode 100644 index 0000000000..b87289a1a4 --- /dev/null +++ b/guice/src/test/java/com/baeldung/examples/GuiceTests.java @@ -0,0 +1,72 @@ +package com.baeldung.examples; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +import com.baeldung.examples.common.BookService; +import com.baeldung.examples.guice.Employee; +import com.baeldung.examples.guice.FooGenerator; +import com.baeldung.examples.guice.GuiceUser; +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 GuiceTests { + + @Test + public void givenAccountFieldInjectedInGuiceUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() { + Injector injector = Guice.createInjector(new GuiceModule()); + GuiceUser guiceUser = injector.getInstance(GuiceUser.class); + assertNotNull(guiceUser.getAccount()); + } + + @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 givenFooGeneratorConstructorParameterIsNotNullable_WhenFooGeneratorIsInjected_ThenTestFailsByProvisionException() { + Injector injector = Guice.createInjector(new GuiceModule()); + FooGenerator fooGenerator = injector.getInstance(FooGenerator.class); + assertNotNull(fooGenerator); + } + + @Test + public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() { + Injector injector = Guice.createInjector(new GuiceModule()); + Person person = injector.getInstance(Person.class); + assertNotNull(person); + } + + @Test + public void givenEmployeeConstructorAnnotatedByInject_WhenEmployeeIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() { + Injector injector = Guice.createInjector(new GuiceModule()); + Employee employee = injector.getInstance(Employee.class); + assertNotNull(employee); + assertEquals("Default", employee.getLastName()); + } + + @Test + public void givenAddressAutowiredToGuiceUserBySetterInjection_WhenGuiceUserIsInjected_ThenAddressInitializedByTheSetter() { + Injector injector = Guice.createInjector(new GuiceModule()); + GuiceUser guiceUser = injector.getInstance(GuiceUser.class); + assertNotNull(guiceUser); + assertNotNull(guiceUser.getAddress()); + assertEquals("Default", guiceUser.getAddress().getCity()); + } + +} diff --git a/guice/src/test/java/com/baeldung/examples/SpringTests.java b/guice/src/test/java/com/baeldung/examples/SpringTests.java new file mode 100644 index 0000000000..327e409cc1 --- /dev/null +++ b/guice/src/test/java/com/baeldung/examples/SpringTests.java @@ -0,0 +1,58 @@ +package com.baeldung.examples; + +import static org.junit.Assert.assertEquals; +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.BookService; +import com.baeldung.examples.spring.AppConfig; +import com.baeldung.examples.spring.SpringUser; +import com.baeldung.examples.spring.Student; +import com.baeldung.examples.spring.UserService; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = { AppConfig.class }) +public class SpringTests { + @Autowired + ApplicationContext context; + + @Test + public void givenAccountFieldAutowiredToSpringUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() { + SpringUser springUser = context.getBean(SpringUser.class); + assertNotNull(springUser.getAccount()); + } + + @Test + public void givenAccountServiceFieldAutowiredToUserService_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 givenStudentConstructorAnnotatedByAutowired_WhenStudentIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() { + Student student = context.getBean(Student.class); + assertNotNull(student); + assertEquals("Default", student.getFirstName()); + assertEquals("Default", student.getLastName()); + } + + @Test + public void givenAddressAutowiredToSpringUserBySetterInjection_WhenSpringUserRetrievedFromContext_ThenAddressInitializedByTheSetter() { + SpringUser springUser = context.getBean(SpringUser.class); + assertNotNull(springUser.getAddress()); + assertEquals("Default", springUser.getAddress().getCity()); + } + +} From acf8034d9352feded9a4e442e654ae517c182e10 Mon Sep 17 00:00:00 2001 From: codehunter34 Date: Wed, 26 Dec 2018 23:58:04 -0500 Subject: [PATCH 2/6] BAEL-2399: Guice vs Spring - Dependency Injection --- guice/pom.xml | 62 ++++++++++++------- .../{GuiceTests.java => GuiceUnitTest.java} | 2 +- .../{SpringTests.java => SpringUnitTest.java} | 2 +- 3 files changed, 41 insertions(+), 25 deletions(-) rename guice/src/test/java/com/baeldung/examples/{GuiceTests.java => GuiceUnitTest.java} (96%) rename guice/src/test/java/com/baeldung/examples/{SpringTests.java => SpringUnitTest.java} (96%) diff --git a/guice/pom.xml b/guice/pom.xml index f3e7873245..54532bc992 100644 --- a/guice/pom.xml +++ b/guice/pom.xml @@ -1,29 +1,45 @@ - - 4.0.0 - com.baeldung.examples.guice - guice - 1.0-SNAPSHOT - jar - guice + + 4.0.0 + com.baeldung.examples.guice + guice + 1.0-SNAPSHOT + jar + guice - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - com.google.inject - guice - ${guice.version} - - + + + com.google.inject + guice + ${guice.version} + - - 4.1.0 - + + org.springframework + spring-context + ${spring.version} + + + + org.springframework + spring-test + ${springtest.version} + test + + + + + 4.1.0 + 5.1.3.RELEASE + 5.1.3.RELEASE + diff --git a/guice/src/test/java/com/baeldung/examples/GuiceTests.java b/guice/src/test/java/com/baeldung/examples/GuiceUnitTest.java similarity index 96% rename from guice/src/test/java/com/baeldung/examples/GuiceTests.java rename to guice/src/test/java/com/baeldung/examples/GuiceUnitTest.java index b87289a1a4..ebc35de405 100644 --- a/guice/src/test/java/com/baeldung/examples/GuiceTests.java +++ b/guice/src/test/java/com/baeldung/examples/GuiceUnitTest.java @@ -15,7 +15,7 @@ import com.baeldung.examples.guice.modules.GuiceModule; import com.google.inject.Guice; import com.google.inject.Injector; -public class GuiceTests { +public class GuiceUnitTest { @Test public void givenAccountFieldInjectedInGuiceUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() { diff --git a/guice/src/test/java/com/baeldung/examples/SpringTests.java b/guice/src/test/java/com/baeldung/examples/SpringUnitTest.java similarity index 96% rename from guice/src/test/java/com/baeldung/examples/SpringTests.java rename to guice/src/test/java/com/baeldung/examples/SpringUnitTest.java index 327e409cc1..0bd6770909 100644 --- a/guice/src/test/java/com/baeldung/examples/SpringTests.java +++ b/guice/src/test/java/com/baeldung/examples/SpringUnitTest.java @@ -18,7 +18,7 @@ import com.baeldung.examples.spring.UserService; @RunWith(SpringRunner.class) @ContextConfiguration(classes = { AppConfig.class }) -public class SpringTests { +public class SpringUnitTest { @Autowired ApplicationContext context; From aeba601c1f5bd14ad061a18c69babe92db97735d Mon Sep 17 00:00:00 2001 From: codehunter34 Date: Wed, 26 Dec 2018 23:58:04 -0500 Subject: [PATCH 3/6] BAEL-2399: Guice vs Spring - Dependency Injection --- guice/pom.xml | 62 ++++++++++++------- .../{GuiceTests.java => GuiceUnitTest.java} | 2 +- .../{SpringTests.java => SpringUnitTest.java} | 2 +- 3 files changed, 41 insertions(+), 25 deletions(-) rename guice/src/test/java/com/baeldung/examples/{GuiceTests.java => GuiceUnitTest.java} (96%) rename guice/src/test/java/com/baeldung/examples/{SpringTests.java => SpringUnitTest.java} (96%) diff --git a/guice/pom.xml b/guice/pom.xml index f3e7873245..54532bc992 100644 --- a/guice/pom.xml +++ b/guice/pom.xml @@ -1,29 +1,45 @@ - - 4.0.0 - com.baeldung.examples.guice - guice - 1.0-SNAPSHOT - jar - guice + + 4.0.0 + com.baeldung.examples.guice + guice + 1.0-SNAPSHOT + jar + guice - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - com.google.inject - guice - ${guice.version} - - + + + com.google.inject + guice + ${guice.version} + - - 4.1.0 - + + org.springframework + spring-context + ${spring.version} + + + + org.springframework + spring-test + ${springtest.version} + test + + + + + 4.1.0 + 5.1.3.RELEASE + 5.1.3.RELEASE + diff --git a/guice/src/test/java/com/baeldung/examples/GuiceTests.java b/guice/src/test/java/com/baeldung/examples/GuiceUnitTest.java similarity index 96% rename from guice/src/test/java/com/baeldung/examples/GuiceTests.java rename to guice/src/test/java/com/baeldung/examples/GuiceUnitTest.java index b87289a1a4..ebc35de405 100644 --- a/guice/src/test/java/com/baeldung/examples/GuiceTests.java +++ b/guice/src/test/java/com/baeldung/examples/GuiceUnitTest.java @@ -15,7 +15,7 @@ import com.baeldung.examples.guice.modules.GuiceModule; import com.google.inject.Guice; import com.google.inject.Injector; -public class GuiceTests { +public class GuiceUnitTest { @Test public void givenAccountFieldInjectedInGuiceUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() { diff --git a/guice/src/test/java/com/baeldung/examples/SpringTests.java b/guice/src/test/java/com/baeldung/examples/SpringUnitTest.java similarity index 96% rename from guice/src/test/java/com/baeldung/examples/SpringTests.java rename to guice/src/test/java/com/baeldung/examples/SpringUnitTest.java index 327e409cc1..0bd6770909 100644 --- a/guice/src/test/java/com/baeldung/examples/SpringTests.java +++ b/guice/src/test/java/com/baeldung/examples/SpringUnitTest.java @@ -18,7 +18,7 @@ import com.baeldung.examples.spring.UserService; @RunWith(SpringRunner.class) @ContextConfiguration(classes = { AppConfig.class }) -public class SpringTests { +public class SpringUnitTest { @Autowired ApplicationContext context; From bd63df8caf05acee9307f84f94c83a304cbc401b Mon Sep 17 00:00:00 2001 From: codehunter34 Date: Tue, 1 Jan 2019 02:46:06 -0500 Subject: [PATCH 4/6] BAEL-2399: Guice vs Spring - Dependency Injection --- .../main/java/com/baeldung/examples/common/PersonDao.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 guice/src/main/java/com/baeldung/examples/common/PersonDao.java diff --git a/guice/src/main/java/com/baeldung/examples/common/PersonDao.java b/guice/src/main/java/com/baeldung/examples/common/PersonDao.java new file mode 100644 index 0000000000..980fee0252 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/PersonDao.java @@ -0,0 +1,5 @@ +package com.baeldung.examples.common; + +public interface PersonDao { + +} From d740cb2da09e0dfeef3ae166473785aa92f3a443 Mon Sep 17 00:00:00 2001 From: codehunter34 Date: Tue, 1 Jan 2019 02:46:06 -0500 Subject: [PATCH 5/6] BAEL-2399: Guice vs Spring - Dependency Injection --- .../java/com/baeldung/examples/common/PersonDaoImpl.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 guice/src/main/java/com/baeldung/examples/common/PersonDaoImpl.java diff --git a/guice/src/main/java/com/baeldung/examples/common/PersonDaoImpl.java b/guice/src/main/java/com/baeldung/examples/common/PersonDaoImpl.java new file mode 100644 index 0000000000..971db5aa87 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/PersonDaoImpl.java @@ -0,0 +1,8 @@ +package com.baeldung.examples.common; + +import org.springframework.stereotype.Component; + +@Component +public class PersonDaoImpl implements PersonDao { + +} \ No newline at end of file From 3ac96662fc8d9f83936f21fc688a13ffd648af40 Mon Sep 17 00:00:00 2001 From: codehunter34 Date: Tue, 1 Jan 2019 02:46:06 -0500 Subject: [PATCH 6/6] BAEL-2399: Guice vs Spring - Dependency Injection --- .../examples/common/PersonDaoImpl.java | 8 ++++++++ .../examples/guice/GuicePersonService.java | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 guice/src/main/java/com/baeldung/examples/common/PersonDaoImpl.java create mode 100644 guice/src/main/java/com/baeldung/examples/guice/GuicePersonService.java diff --git a/guice/src/main/java/com/baeldung/examples/common/PersonDaoImpl.java b/guice/src/main/java/com/baeldung/examples/common/PersonDaoImpl.java new file mode 100644 index 0000000000..971db5aa87 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/common/PersonDaoImpl.java @@ -0,0 +1,8 @@ +package com.baeldung.examples.common; + +import org.springframework.stereotype.Component; + +@Component +public class PersonDaoImpl implements PersonDao { + +} \ No newline at end of file diff --git a/guice/src/main/java/com/baeldung/examples/guice/GuicePersonService.java b/guice/src/main/java/com/baeldung/examples/guice/GuicePersonService.java new file mode 100644 index 0000000000..ce12e3e528 --- /dev/null +++ b/guice/src/main/java/com/baeldung/examples/guice/GuicePersonService.java @@ -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; + } + +}