Bean injection types

This commit is contained in:
Raksha Rao 2018-01-25 10:31:34 +05:30
parent 9f1429b067
commit dafe2f5553
7 changed files with 168 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,21 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<context:annotation-config/>
<bean id="bankAccountWithConstructorInjectionBean"
class="com.baeldung.samplebeaninjectionypes.BankAccountWithConstructorInjection">
<constructor-arg ref="bankAccountService" />
</bean>
<bean id="bankAccountWithSetterInjection"
class="com.baeldung.samplebeaninjectionypes.BankAccountWithSetterInjection">
<constructor-arg ref="bankAccountService" />
</bean>
<bean id="bankAccountService" class="com.baeldung.samplebeaninjectionypes.BankAccountService">
</bean>
</beans>

View File

@ -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));
}
}