BAEL-2399: Guice vs Spring - Dependency Injection
This commit is contained in:
parent
3df9e64050
commit
8880d96d3a
|
@ -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,8 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface AccountService {
|
||||
public List<String> listAccountTypes();
|
||||
|
||||
}
|
|
@ -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<String> listAccountTypes() {
|
||||
return Arrays.asList("Checking", "Saving");
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BookService {
|
||||
|
||||
public List<String> findBestSellerBooks();
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class BookServiceImpl implements BookService {
|
||||
|
||||
public List<String> findBestSellerBooks() {
|
||||
return Arrays.asList("Harry Potter", "Lord of The Rings");
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.examples.guice;
|
||||
|
||||
|
||||
public class Foo {
|
||||
}
|
|
@ -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) {
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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,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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Foo>() {
|
||||
public Foo get() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
bind(Person.class).toConstructor(Person.class.getConstructor());
|
||||
// bind(Person.class).toProvider(new Provider<Person>() {
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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,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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue