Skip to content

Commit dafe2f5

Browse files
Raksha RaoRaksha Rao
authored andcommitted
Bean injection types
1 parent 9f1429b commit dafe2f5

7 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.samplebeaninjectionypes;
2+
3+
public class AccountDetails {
4+
5+
private Long accountNumber;
6+
7+
private String accountType;
8+
9+
private String accountName;
10+
11+
public AccountDetails(Long accountNumber, String accountType, String accountName) {
12+
this.accountNumber = accountNumber;
13+
this.accountType = accountType;
14+
this.accountName = accountName;
15+
}
16+
17+
public Long getAccountNumber() {
18+
return accountNumber;
19+
}
20+
21+
public void setAccountNumber(Long accountNumber) {
22+
this.accountNumber = accountNumber;
23+
}
24+
25+
public String getAccountType() {
26+
return accountType;
27+
}
28+
29+
public void setAccountType(String accountType) {
30+
this.accountType = accountType;
31+
}
32+
33+
public String getAccountName() {
34+
return accountName;
35+
}
36+
37+
public void setAccountName(String accountName) {
38+
this.accountName = accountName;
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.samplebeaninjectionypes;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
public class BankAccountApplication {
7+
8+
9+
public static void main(String[] args) {
10+
ApplicationContext context = new ClassPathXmlApplicationContext("samplebeaninjectiontypes-context.xml");
11+
BankAccountWithSetterInjection bankAccountWithSetterInjection = (BankAccountWithSetterInjection) context.getBean("bankAccountWithSetterInjectionBean");
12+
13+
bankAccountWithSetterInjection.openAccount(12345L,"Savings","John Doe");
14+
15+
BankAccountWithConstructorInjection bankAccountWithConstructorInjection = (BankAccountWithConstructorInjection) context.getBean("bankAccountWithConstructorInjectionBean");
16+
bankAccountWithSetterInjection.openAccount(12345L,"Savings","John Doe");
17+
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.samplebeaninjectionypes;
2+
3+
public class BankAccountService {
4+
5+
public AccountDetails openAccount(Long accountNumber, String accountType, String owner) {
6+
AccountDetails accountDetails = new AccountDetails(accountNumber,accountType,owner);
7+
return accountDetails;
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.samplebeaninjectionypes;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
5+
public class BankAccountWithConstructorInjection {
6+
private BankAccountService bankAccountService;
7+
8+
@Autowired
9+
public BankAccountWithConstructorInjection(BankAccountService service) {
10+
this.bankAccountService = service;
11+
}
12+
13+
public AccountDetails openAccount(Long accountNumber, String accountType, String owner) {
14+
return bankAccountService.openAccount(accountNumber, accountType, owner);
15+
}
16+
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.samplebeaninjectionypes;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
6+
public class BankAccountWithSetterInjection {
7+
8+
private BankAccountService bankAccountService;
9+
10+
public BankAccountWithSetterInjection(BankAccountService service) {
11+
this.bankAccountService = service;
12+
}
13+
14+
@Autowired
15+
public void setBankAccountService(BankAccountService bankAccountService) {
16+
this.bankAccountService = bankAccountService;
17+
}
18+
19+
public AccountDetails openAccount(Long accountNumber, String accountType, String owner) {
20+
return bankAccountService.openAccount(accountNumber, accountType, owner);
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
3+
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
5+
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">
6+
7+
<context:annotation-config/>
8+
9+
<bean id="bankAccountWithConstructorInjectionBean"
10+
class="com.baeldung.samplebeaninjectionypes.BankAccountWithConstructorInjection">
11+
<constructor-arg ref="bankAccountService" />
12+
</bean>
13+
14+
<bean id="bankAccountWithSetterInjection"
15+
class="com.baeldung.samplebeaninjectionypes.BankAccountWithSetterInjection">
16+
<constructor-arg ref="bankAccountService" />
17+
</bean>
18+
19+
<bean id="bankAccountService" class="com.baeldung.samplebeaninjectionypes.BankAccountService">
20+
</bean>
21+
</beans>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.samplebeaninjectiontypes;
2+
3+
import com.baeldung.dependencyinjectiontypes.ArticleWithSetterInjection;
4+
import com.baeldung.samplebeaninjectionypes.AccountDetails;
5+
import com.baeldung.samplebeaninjectionypes.BankAccountWithConstructorInjection;
6+
import com.baeldung.samplebeaninjectionypes.BankAccountWithSetterInjection;
7+
import org.junit.Test;
8+
import org.springframework.context.ApplicationContext;
9+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
10+
import org.springframework.context.support.ClassPathXmlApplicationContext;
11+
12+
import static org.junit.Assert.assertTrue;
13+
14+
public class SampleBeanInjectionTest {
15+
16+
@Test
17+
public void testSetterInjectionValid() {
18+
19+
ApplicationContext context = new ClassPathXmlApplicationContext("samplebeaninjectiontypes-context.xml");
20+
BankAccountWithSetterInjection bankAccountWithSetterInjection = (BankAccountWithSetterInjection) context.getBean("bankAccountWithSetterInjection");
21+
22+
String owner = "John Doe";
23+
AccountDetails accountDetails = bankAccountWithSetterInjection.openAccount(12345L,"Savings",owner);
24+
25+
assertTrue(accountDetails.getAccountName().equals(owner));
26+
27+
}
28+
29+
@Test
30+
public void testConstructorInjectionValid() {
31+
32+
ApplicationContext context = new ClassPathXmlApplicationContext("samplebeaninjectiontypes-context.xml");
33+
BankAccountWithConstructorInjection bankAccountWithConstructorInjection = (BankAccountWithConstructorInjection) context.getBean("bankAccountWithConstructorInjectionBean");
34+
35+
String owner = "John Doe";
36+
AccountDetails accountDetails = bankAccountWithConstructorInjection.openAccount(12345L,"Savings",owner);
37+
38+
assertTrue(accountDetails.getAccountName().equals(owner));
39+
}
40+
}

0 commit comments

Comments
 (0)