好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring依赖注入多种类型数据的示例代码

Student实体类

?

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

60

61

62

63

64

65

66

67

68

package entity;

import java.util.*;

/**

  * @author LeDao

  * @company

  * @create 2022-02-13 21:26

  */

public class Student {

     private int id;

     private String name;

     private StudentClass studentClass;

     private String[] books;

     private List<String> hobbies;

     private Map<String, String> cards;

     private Set<String> games;

     private String wife;

     private Properties info;

     public int getId() {

         return id;

     }

     public void setId( int id) {

         this .id = id;

     public String getName() {

         return name;

     public void setName(String name) {

         this .name = name;

     public StudentClass getStudentClass() {

         return studentClass;

     public void setStudentClass(StudentClass studentClass) {

         this .studentClass = studentClass;

     public String[] getBooks() {

         return books;

     public void setBooks(String[] books) {

         this .books = books;

     public List<String> getHobbies() {

         return hobbies;

     public void setHobbies(List<String> hobbies) {

         this .hobbies = hobbies;

     public Map<String, String> getCards() {

         return cards;

     public void setCards(Map<String, String> cards) {

         this .cards = cards;

     public Set<String> getGames() {

         return games;

     public void setGames(Set<String> games) {

         this .games = games;

     public String getWife() {

         return wife;

     public void setWife(String wife) {

         this .wife = wife;

     public Properties getInfo() {

         return info;

     public void setInfo(Properties info) {

         this .info = info;

     @Override

     public String toString() {

         return "Student{" +

                 "id=" + id +

                 ", name='" + name + '\ '' +

                 ", studentClass=" + studentClass +

                 ", books=" + Arrays.toString(books) +

                 ", hobbies=" + hobbies +

                 ", cards=" + cards +

                 ", games=" + games +

                 ", wife='" + wife + '\ '' +

                 ", info=" + info +

                 '}' ;

}

StudentsClass实体类

?

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

package entity;

/**

  * @author LeDao

  * @company

  * @create 2022-02-14 14:11

  */

public class StudentClass {

     private int id;

     private String name;

     public int getId() {

         return id;

     }

     public void setId( int id) {

         this .id = id;

     }

     public String getName() {

         return name;

     }

     public void setName(String name) {

         this .name = name;

     }

     @Override

     public String toString() {

         return "Class{" +

                 "id=" + id +

                 ", name='" + name + '\ '' +

                 '}' ;

     }

}

beans.xml

下面展示的数据类型有:一般类型、对象、数组、List、Map、Set、空值、Properties

?

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

<?xml version= "1.0" encoding= "UTF-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

        xmlns:xsi= "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

        xsi:schemaLocation= "http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd" >

     <bean id= "studentClass1" class = "entity.StudentClass" >

         <property name= "id" value= "1" />

         <property name= "name" value= "软件工程3班" />

     </bean>

     <bean id= "user1" class = "entity.Student" >

         <!--一般类型-->

         <property name= "id" value= "1" />

         <property name= "name" value= "tom" />

         <!--对象-->

         <property name= "studentClass" ref= "studentClass1" />

         <!--数组-->

         <property name= "books" >

             <array>

                 <value>Java编程思想</value>

                 <value>MySQL必知必会</value>

                 <value>平凡的世界</value>

             </array>

         </property>

         <!--List-->

         <property name= "hobbies" >

             <list>

                 <value>唱</value>

                 <value>跳</value>

                 <value>rap</value>

                 <value>打篮球</value>

             </list>

         </property>

         <!--Map-->

         <property name= "cards" >

             <map>

                 <entry key= "身份证" value= "123" />

                 <entry key= "校园卡" value= "321" />

             </map>

         </property>

         <!--Set-->

         <property name= "games" >

             <set>

                 <value>LOL</value>

                 <value>DNF</value>

                 <value>COC</value>

             </set>

         </property>

         <!--空值-->

         <property name= "wife" >

             < null />

         </property>

         <!--Properties-->

         <property name= "info" >

             <props>

                 <prop key= "userName" >root</prop>

                 <prop key= "password" > 123456 </prop>

             </props>

         </property>

     </bean>

</beans>

测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

import config.MyConfig;

import entity.Student;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

  * @author LeDao

  * @company

  * @create 2022-02-12 15:56

  */

public class MyTest {

     public static void main(String[] args) {

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

         Student student = (Student) context.getBean( "user1" );

         System.out.println(student);

     }

}

到此这篇关于Spring依赖注入多种类型数据的文章就介绍到这了,更多相关Spring依赖注入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/zou666zou/article/details/123836594

查看更多关于Spring依赖注入多种类型数据的示例代码的详细内容...

  阅读:14次