diff --git a/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/AccountDetails.java b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/AccountDetails.java new file mode 100644 index 0000000000..2e4eb3b894 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/AccountDetails.java @@ -0,0 +1,40 @@ +package com.baeldung.samplebeaninjectionypes; + +public class AccountDetails { + + private Long accountNumber; + + private String accountType; + + private String accountName; + + public AccountDetails(Long accountNumber, String accountType, String accountName) { + this.accountNumber = accountNumber; + this.accountType = accountType; + this.accountName = accountName; + } + + public Long getAccountNumber() { + return accountNumber; + } + + public void setAccountNumber(Long accountNumber) { + this.accountNumber = accountNumber; + } + + public String getAccountType() { + return accountType; + } + + public void setAccountType(String accountType) { + this.accountType = accountType; + } + + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } +} diff --git a/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountApplication.java b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountApplication.java new file mode 100644 index 0000000000..466255fd2e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountApplication.java @@ -0,0 +1,19 @@ +package com.baeldung.samplebeaninjectionypes; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class BankAccountApplication { + + + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("samplebeaninjectiontypes-context.xml"); + BankAccountWithSetterInjection bankAccountWithSetterInjection = (BankAccountWithSetterInjection) context.getBean("bankAccountWithSetterInjectionBean"); + + bankAccountWithSetterInjection.openAccount(12345L,"Savings","John Doe"); + + BankAccountWithConstructorInjection bankAccountWithConstructorInjection = (BankAccountWithConstructorInjection) context.getBean("bankAccountWithConstructorInjectionBean"); + bankAccountWithSetterInjection.openAccount(12345L,"Savings","John Doe"); + + } +} diff --git a/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountService.java b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountService.java new file mode 100644 index 0000000000..0f62db3655 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountService.java @@ -0,0 +1,9 @@ +package com.baeldung.samplebeaninjectionypes; + +public class BankAccountService { + + public AccountDetails openAccount(Long accountNumber, String accountType, String owner) { + AccountDetails accountDetails = new AccountDetails(accountNumber,accountType,owner); + return accountDetails; + } +} diff --git a/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountWithConstructorInjection.java b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountWithConstructorInjection.java new file mode 100644 index 0000000000..1bc75d15e0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountWithConstructorInjection.java @@ -0,0 +1,17 @@ +package com.baeldung.samplebeaninjectionypes; + +import org.springframework.beans.factory.annotation.Autowired; + +public class BankAccountWithConstructorInjection { + private BankAccountService bankAccountService; + + @Autowired + public BankAccountWithConstructorInjection(BankAccountService service) { + this.bankAccountService = service; + } + + public AccountDetails openAccount(Long accountNumber, String accountType, String owner) { + return bankAccountService.openAccount(accountNumber, accountType, owner); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountWithSetterInjection.java b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountWithSetterInjection.java new file mode 100644 index 0000000000..69c1fec4cc --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/samplebeaninjectionypes/BankAccountWithSetterInjection.java @@ -0,0 +1,22 @@ +package com.baeldung.samplebeaninjectionypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +public class BankAccountWithSetterInjection { + + private BankAccountService bankAccountService; + + public BankAccountWithSetterInjection(BankAccountService service) { + this.bankAccountService = service; + } + + @Autowired + public void setBankAccountService(BankAccountService bankAccountService) { + this.bankAccountService = bankAccountService; + } + + public AccountDetails openAccount(Long accountNumber, String accountType, String owner) { + return bankAccountService.openAccount(accountNumber, accountType, owner); + } +} diff --git a/spring-core/src/main/resources/samplebeaninjectiontypes-context.xml b/spring-core/src/main/resources/samplebeaninjectiontypes-context.xml new file mode 100644 index 0000000000..307abd508e --- /dev/null +++ b/spring-core/src/main/resources/samplebeaninjectiontypes-context.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/samplebeaninjectiontypes/SampleBeanInjectionTest.java b/spring-core/src/test/java/com/baeldung/samplebeaninjectiontypes/SampleBeanInjectionTest.java new file mode 100644 index 0000000000..f054a89a85 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/samplebeaninjectiontypes/SampleBeanInjectionTest.java @@ -0,0 +1,40 @@ +package com.baeldung.samplebeaninjectiontypes; + +import com.baeldung.dependencyinjectiontypes.ArticleWithSetterInjection; +import com.baeldung.samplebeaninjectionypes.AccountDetails; +import com.baeldung.samplebeaninjectionypes.BankAccountWithConstructorInjection; +import com.baeldung.samplebeaninjectionypes.BankAccountWithSetterInjection; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.junit.Assert.assertTrue; + +public class SampleBeanInjectionTest { + + @Test + public void testSetterInjectionValid() { + + ApplicationContext context = new ClassPathXmlApplicationContext("samplebeaninjectiontypes-context.xml"); + BankAccountWithSetterInjection bankAccountWithSetterInjection = (BankAccountWithSetterInjection) context.getBean("bankAccountWithSetterInjection"); + + String owner = "John Doe"; + AccountDetails accountDetails = bankAccountWithSetterInjection.openAccount(12345L,"Savings",owner); + + assertTrue(accountDetails.getAccountName().equals(owner)); + + } + + @Test + public void testConstructorInjectionValid() { + + ApplicationContext context = new ClassPathXmlApplicationContext("samplebeaninjectiontypes-context.xml"); + BankAccountWithConstructorInjection bankAccountWithConstructorInjection = (BankAccountWithConstructorInjection) context.getBean("bankAccountWithConstructorInjectionBean"); + + String owner = "John Doe"; + AccountDetails accountDetails = bankAccountWithConstructorInjection.openAccount(12345L,"Savings",owner); + + assertTrue(accountDetails.getAccountName().equals(owner)); + } +}