BAEL-2399: Guice vs Spring - Dependency Injection

This commit is contained in:
codehunter34
2018-12-23 16:41:11 -05:00
parent 3df9e64050
commit 8880d96d3a
19 changed files with 531 additions and 0 deletions
@@ -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;
}
}