Skip to content

Commit 325466a

Browse files
moguzozcanmaibin
authored andcommitted
Master (eugenp#1250)
* Bean Injection Project is added Different Types of Bean Injection article codes are added. * Java-based configuration added Java-based configuration and tests are added. Coding styles are fixed. * List of Lists Article Codes added. List of Lists Article Codes added.
1 parent d4576d9 commit 325466a

16 files changed

Lines changed: 311 additions & 6 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.list.listoflist;
2+
3+
public class Pen implements Stationery {
4+
5+
public String name;
6+
7+
public Pen(String name) {
8+
this.name = name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
15+
public String getName() {
16+
return this.name;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.list.listoflist;
2+
3+
public class Pencil implements Stationery{
4+
5+
public String name;
6+
7+
public Pencil(String name) {
8+
this.name = name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
15+
public String getName() {
16+
return this.name;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.list.listoflist;
2+
3+
public class Rubber implements Stationery {
4+
5+
public String name;
6+
7+
public Rubber(String name) {
8+
this.name = name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
15+
public String getName() {
16+
return this.name;
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.list.listoflist;
2+
3+
public interface Stationery {
4+
5+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.list.listoflist;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
public class ListOfListsTest {
11+
12+
private List<ArrayList<? extends Stationery>> listOfLists = new ArrayList<ArrayList<? extends Stationery>>();
13+
private ArrayList<Pen> penList = new ArrayList<>();
14+
private ArrayList<Pencil> pencilList = new ArrayList<>();
15+
private ArrayList<Rubber> rubberList = new ArrayList<>();
16+
17+
@SuppressWarnings("unchecked")
18+
@Before
19+
public void init() {
20+
listOfLists.add(penList);
21+
listOfLists.add(pencilList);
22+
listOfLists.add(rubberList);
23+
24+
((ArrayList<Pen>) listOfLists.get(0)).add(new Pen("Pen 1"));
25+
((ArrayList<Pencil>) listOfLists.get(1)).add(new Pencil("Pencil 1"));
26+
((ArrayList<Rubber>) listOfLists.get(2)).add(new Rubber("Rubber 1"));
27+
}
28+
29+
@Test
30+
public void givenListOfLists_thenCheckNames() {
31+
assertEquals("Pen 1", ((Pen) listOfLists.get(0)
32+
.get(0)).getName());
33+
assertEquals("Pencil 1", ((Pencil) listOfLists.get(1)
34+
.get(0)).getName());
35+
assertEquals("Rubber 1", ((Rubber) listOfLists.get(2)
36+
.get(0)).getName());
37+
}
38+
39+
@SuppressWarnings("unchecked")
40+
@Test
41+
public void givenListOfLists_whenRemovingElements_thenCheckNames() {
42+
((ArrayList<Pencil>) listOfLists.get(1)).remove(0);
43+
listOfLists.remove(1);
44+
assertEquals("Rubber 1", ((Rubber) listOfLists.get(1)
45+
.get(0)).getName());
46+
listOfLists.remove(0);
47+
assertEquals("Rubber 1", ((Rubber) listOfLists.get(0)
48+
.get(0)).getName());
49+
}
50+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.bean.config;
2+
3+
import org.baeldung.bean.injection.Helm;
4+
import org.baeldung.bean.injection.Ship;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class ConstructorBasedShipConfig {
10+
11+
@Bean
12+
public Ship ship() {
13+
return new Ship(helm());
14+
}
15+
16+
@Bean
17+
public Helm helm() {
18+
return new Helm();
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.bean.config;
2+
3+
import org.baeldung.bean.injection.Helm;
4+
import org.baeldung.bean.injection.Ship;
5+
import org.springframework.context.annotation.Bean;
6+
7+
public class SetterBasedShipConfig {
8+
9+
@Bean
10+
public Ship ship() {
11+
return new Ship();
12+
}
13+
14+
@Bean
15+
public Helm helm() {
16+
return new Helm();
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.bean.injection;
2+
3+
public class Helm {
4+
5+
private String brandOfHelm = "HelmBrand";
6+
7+
public String getBrandOfHelm() {
8+
return brandOfHelm;
9+
}
10+
11+
public void setBrandOfHelm(String brandOfHelm) {
12+
this.brandOfHelm = brandOfHelm;
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.bean.injection;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
5+
public class Ship {
6+
7+
@Autowired
8+
private Helm helm;
9+
10+
public Ship() {
11+
helm = new Helm();
12+
}
13+
14+
public Ship(Helm helm) {
15+
this.helm = helm;
16+
}
17+
18+
@Autowired
19+
public void setHelm(Helm helm) {
20+
this.helm = helm;
21+
}
22+
23+
public Helm getHelm() {
24+
return this.helm;
25+
}
26+
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
3-
xmlns:util="http://www.springframework.org/schema/util"
4-
xsi:schemaLocation="
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:util="http://www.springframework.org/schema/util"
5+
xsi:schemaLocation="
56
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
67
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
7-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
8-
>
8+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
99

10-
<context:property-placeholder location="classpath:bar.properties" order="2"/>
10+
<context:property-placeholder location="classpath:bar.properties"
11+
order="2" />
1112

1213
</beans>

0 commit comments

Comments
 (0)