1. 程式人生 > 其它 >Spring之三種依賴注入,給各種集合型別的屬性注入值

Spring之三種依賴注入,給各種集合型別的屬性注入值

技術標籤:java

、第一種注入(set注入):

建立實體類,Teacher:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

package org.ruangong.entity;

public class Teacher {

private String name;

private int age;

public String getName() {

    return name;

}

public void setName(String name) {

    this.name = name;

}

public int getAge() {

    return age;

}

public void setAge(int age) {

    this.age = age;

}

}

建立實體類,Course:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

package org.ruangong.entity;

public class Course {

private String cname;

private int chour;

private Teacher teacher;

public String getCname() {

    return cname;

}

public void setCname(String cname) {

    this.cname = cname;

}

public int getChour() {

    return chour;

}

public void setChour(int chour) {

    this.chour = chour;

}

public Teacher getTeacher() {

    return teacher;

}

public void setTeacher(Teacher teacher) {

    this.teacher = teacher;

}

@Override

public String toString() {

    return "Course [cname=" + cname + ", chour=" + chour + ", teacher=" + teacher.getName() + teacher.getAge()+"]";

}

}

在applicationContext.xml檔案中新增bean標籤。

1

2

3

4

5

6

7

8

9

10

    <property name="name" value="王建民"></property>

    <property name="age" value="50"></property>    

</bean>

<bean id="course" class="org.ruangong.entity.Course">

    <property name="cname" value="java"></property>

    <property name="chour" value="2"></property>

     

    <property name="teacher" ref="teacher"></property>

</bean>

其中的ref=“”,對應物件值。將teacher物件注入到course物件中。

二、第二中注入(構造器注入):

在Teacher實體中新增構造方法。

1

2

3

4

5

public Teacher(String name, int age) {

    super();

    this.name = name;

    this.age = age;

}

在Course實體中新增構造方法:

1

2

3

4

5

6

public Course(String cname, int chour, Teacher teacher) {

    super();

    this.cname = cname;

    this.chour = chour;

    this.teacher = teacher;

}

在之前的applicationContext.xml檔案的id為teacher和course標籤重新新增構造方法。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<!-- 通過set注入 -->

    <!-- <property name="name" value="王建民"></property>

    <property name="age" value="50"></property>  -->

<!-- 通過構造器注入 -->

    <constructor-arg value="劉丹"></constructor-arg>

    <constructor-arg value="48"></constructor-arg>

     

</bean>

<bean id="course" class="org.ruangong.entity.Course">

<!-- 通過set注入 -->

    <!-- <property name="cname" value="java"></property>

    <property name="chour" value="2"></property>       

    <property name="teacher" ref="teacher"></property> -->

<!-- 通過構造器注入 -->

    <constructor-arg value="PHP"></constructor-arg>

    <constructor-arg value="5"></constructor-arg>

    <constructor-arg ref="teacher"></constructor-arg>

         

</bean>

或者可以在constructor標籤的後面新增index=“key”,key值來控制屬性順序。

或者新增name=“key”,key來控制屬性名。

三、第三種注入(P值注入)

生成P的標籤。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<!-- 通過set注入 -->

    <!-- <property name="name" value="王建民"></property>

    <property name="age" value="50"></property>  -->

<!-- 通過構造器注入 -->

    <!-- <constructor-arg value="劉丹"></constructor-arg>

    <constructor-arg value="48"></constructor-arg> -->

     

</bean>

<bean id="course" class="org.ruangong.entity.Course" p:cname="統一建模" p:chour="6" p:teacher-ref="teacher">

<!-- 通過set注入 -->

    <!-- <property name="cname" value="java"></property>

    <property name="chour" value="2"></property>       

    <property name="teacher" ref="teacher"></property> -->

<!-- 通過構造器注入 -->

    <!-- <constructor-arg value="PHP"></constructor-arg>

    <constructor-arg value="5"></constructor-arg>

    <constructor-arg ref="teacher"></constructor-arg> -->

         

</bean>

四、集合型別值注入:

建立集合實體,AllCollectionType:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

package org.ruangong.entity;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

public class AllCollectionType {

private List<String> list;

private String[] array;

private Map<String,String> map;

private Set<String> set;

private Properties props;

public List<String> getList() {

    return list;

}

public void setList(List<String> list) {

    this.list = list;

}

public String[] getArray() {

    return array;

}

public void setArray(String[] array) {

    this.array = array;

}

public Map<String, String> getMap() {

    return map;

}

public void setMap(Map<String, String> map) {

    this.map = map;

}

public Set<String> getSet() {

    return set;

}

public void setSet(Set<String> set) {

    this.set = set;

}

public Properties getProps() {

    return props;

}

public void setProps(Properties props) {

    this.props = props;

}

@Override

public String toString() {

    return "AllCollectionType [list=" + list + ", array=" + Arrays.toString(array) + ", map=" + map + ", set=" + set

            + ", props=" + props + "]";

}

}

在applicationContext.xml檔案中:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

<!-- 通過set方式賦值 -->

<property name="list">

    <list>

        <value>[體育新聞](http://www.snmky.org/tiyu/)</value>

        <value>足球</value>

        <value>乒乓球</value>

    </list>

</property>

<property name="array">

    <array>

        <value>籃球_array</value>

        <value>[足球比分](http://www.snmky.org/zqbf/)_array</value>

        <value>乒乓球_array</value>   

    </array>

</property>

<property name="map">

        <map>

            <entry>

                <key>

                    <value>

                    foot

                    </value>

                </key>

                <value>足球</value>

                </entry>

                <entry>

                <key>

                    <value>

                    basket

                    </value>

                </key>

                <value>**[nba直播吧](http://www.snmky.org/gov/)**</value>

                </entry>

                <entry>

                <key>

                    <value>

                    pingpang

                    </value>

                </key>

                <value>足球</value>

            </entry>

        </map>

</property>

<property name="set">

    <set>

        <value>籃球_set</value>

        <value>足球_set</value>  

        <value>乒乓球_set</value> 

    </set>

</property>

<property name="props">

    <props>

        <prop key="foot4">足球</prop>

        <prop key="basket4">籃球</prop>      

        <prop key="pp4">乒乓球</prop>

    </props>

</property>

</bean>

test中進行測試:

1

2

3

4

5

public static void collectionDemo(){

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    AllCollectionType type = (AllCollectionType)context.getBean("collection");

    System.out.println(type);

}