Skip to content

Commit 68410f8

Browse files
committed
1 parent aa0baec commit 68410f8

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<maven.compiler.target>1.8</maven.compiler.target>
2222
<maven.compiler.source>1.8</maven.compiler.source>
2323
<java.version>1.8</java.version>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2425
</properties>
2526

2627

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package basic.ioc.wiringfinetune;
2+
3+
import org.springframework.beans.factory.annotation.Qualifier;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Primary;
8+
9+
@ComponentScan("basic.ioc.wiringfinetune")
10+
@Configuration
11+
public class ConfigWiring {
12+
13+
//bean name = insurance1
14+
@Bean
15+
@Primary
16+
public Insurance insurance1(){
17+
Insurance healthInsurance = new Insurance();
18+
healthInsurance.setType("Health Insurance");
19+
return healthInsurance;
20+
}
21+
22+
//bean name = insurance2
23+
@Bean
24+
public Insurance insurance2(){
25+
Insurance termInsurance = new Insurance();
26+
termInsurance.setType("Term Insurance");
27+
return termInsurance;
28+
}
29+
30+
//bean name = insurance3
31+
@Bean
32+
@Qualifier("generalInsurance")
33+
public Insurance insurance3() {
34+
Insurance generalInsurance = new Insurance();
35+
generalInsurance.setType("General Insurance");
36+
return generalInsurance;
37+
}
38+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package basic.ioc.wiringfinetune;
2+
3+
public interface FileReader {
4+
void print();
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package basic.ioc.wiringfinetune;
2+
3+
public class Insurance {
4+
private String type;
5+
6+
public String getType() {
7+
return type;
8+
}
9+
10+
public void setType(String type) {
11+
this.type = type;
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package basic.ioc.wiringfinetune;
2+
3+
import org.springframework.context.annotation.Primary;
4+
import org.springframework.stereotype.Component;
5+
6+
@Primary
7+
@Component
8+
public class PdfFileReader implements FileReader {
9+
10+
@Override
11+
public void print() {
12+
System.out.println("Inside PdfFileReader");
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package basic.ioc.wiringfinetune;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class WordFileReader implements FileReader {
7+
8+
@Override
9+
public void print() {
10+
System.out.println("Inside WordFileReader");
11+
}
12+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package basic.ioc.wiringfinetune;
2+
3+
import org.junit.Assert;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
7+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
8+
9+
@SpringJUnitConfig(ConfigWiring.class)
10+
public class FineTuneAutowiring {
11+
12+
@Autowired
13+
private FileReader fileReader;
14+
15+
@Test
16+
public void testFileReader() {
17+
Assert.assertNotNull(fileReader);
18+
Assert.assertEquals(fileReader.getClass(), PdfFileReader.class);
19+
}
20+
21+
@Autowired
22+
private Insurance insurance;
23+
24+
@Test
25+
public void testInsuranceBean(){
26+
Assert.assertNotNull(insurance);
27+
Assert.assertEquals(insurance.getType(), "Health Insurance");
28+
}
29+
30+
//With Qualifier
31+
@Autowired
32+
@Qualifier("wordFileReader")
33+
private FileReader fileReader2;
34+
35+
@Test
36+
public void testWordFileReader() {
37+
Assert.assertNotNull(fileReader2);
38+
Assert.assertEquals(fileReader2.getClass(), WordFileReader.class);
39+
}
40+
41+
@Autowired
42+
//@Qualifier("insurance3")
43+
@Qualifier("generalInsurance")
44+
private Insurance myInsurance;
45+
46+
@Test
47+
public void testInsuranceWithQualifier(){
48+
Assert.assertNotNull(myInsurance);
49+
Assert.assertEquals("General Insurance", myInsurance.getType());
50+
}
51+
}

0 commit comments

Comments
 (0)