好得很程序员自学网

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

Spring Xml装配Bean的思路详解

1,概述

在Spring中提供了三种方式来对Bean进行配置:

在xml文件中配置在Java的接口和实现类中配置隐式Bean的发现机制和自动装配原则
这三种方式都经常用到,而且常常会混合使用。这篇先写xml装配Bean。

2,分析bean标签

?

1

2

3

4

5

<bean id= "pserson" class = "com.xl.spring.xlIoc.beans.Person" >

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

     <property name= "name" value= "宋智孝" />

     <property name= "nickName" value= "懵懵" />

</bean>

1.id:为bean取一个全局唯一的名字。该属性可选,如果没有声明该属性,那么Spring会采用"全限定名#{number}"的方式自动生成一个编号,number从0开始计数。
比如声明了两个Person对象,如下:

?

1

2

3

4

5

6

7

8

9

10

<bean class = "com.xl.spring.xlIoc.beans.Person" >

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

     <property name= "name" value= "宋智孝" />

     <property name= "nickName" value= "懵懵" />

</bean>

<bean class = "com.xl.spring.xlIoc.beans.Person" >

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

     <property name= "name" value= "周杰伦" />

     <property name= "nickName" value= "Jack" />

</bean>

这时想要获取对象:

?

1

2

3

ApplicationContext ac = new ClassPathXmlApplicationContext( "springioc.xml" );

Person person = (Person) ac.getBean(Person. class .getName() + "#1" );

System.out.println(person.toString());

2.class:注入的对象的类型,对应的是类的全限定名。
3.property:定义类的属性,其中name表示属性名,value表示属性值。

3,装配集合

以上实现注入,都是对于一些基本数据类型和String类型。如果数据类型是集合的话,那么用如下做法:
1.首先定义一个新的类 --> Coder:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.xl.spring.xlIoc.beans;

 

import lombok.Data;

 

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

 

@Data

public class Coder {

     private String name;

     private List<String> languages;

     private Set<String> friends;

     private Map<String, Integer> houses;

     private Properties properties;

}

2.在Spring的xml文件中,注入Coder:

?

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

<bean id= "coder" class = "com.xl.spring.xlIoc.beans.Coder" >

     <property name= "name" value= "宋智孝" />

     <!--配置list集合-->

     <property name= "languages" >

         <list>

             <value>java</value>

             <value>JavaScript</value>

             <value>SQL</value>

         </list>

     </property>

     <!--配置set集合-->

     <property name= "friends" >

         <set>

             <value>金钟国</value>

             <value>河东勋</value>

             <value>刘在石</value>

             <value>池石镇</value>

             <value>全邵旻</value>

         </set>

     </property>

     <!--配置map集合-->

     <property name= "houses" >

         <map>

             <entry key= "麻浦区" value= "240000000" />

         </map>

     </property>

     <!--配置properties集合-->

     <property name= "properties" >

         <props>

             <prop key= "身高" > 168 </prop>

             <prop key= "体重" > 52.3 </prop>

             <prop key= "年龄" > 30 </prop>

         </props>

     </property>

</bean>

3.调用:

?

1

2

3

4

5

6

public static void testCoder() {

     //1.通过配置文件获取到spring的上下文

     ApplicationContext ac = new ClassPathXmlApplicationContext( "springioc.xml" );

     Coder coder = (Coder) ac.getBean( "coder" );

     coder.getFriends().forEach(s -> System.out.println( "friend ---> " + s));

}

4,复杂Bean装配

如果属性是一个复杂集合对象,比如属性是List或Map,而这个List的泛型是一个对象,或者说Map的key和value都是一个对象。
这个时候怎么办?
解决思路:先把泛型对应的Bean注入好,然后在注入属性的时候引入过去就好。
1.首先定义三个类:其中UserRole中需要使用User对象和Role对象

?

1

2

3

4

5

6

7

8

9

package com.xl.spring.xlIoc.beans;

 

import lombok.Data;

 

@Data

public class User {

     private Integer id;

     private String name;

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

package com.xl.spring.xlIoc.beans;

 

import lombok.Data;

 

@Data

public class Role {

     private Integer id;

     private String name;

}

 

 

package com.xl.spring.xlIoc.beans;

 

import lombok.Data;

 

import java.util.List;

import java.util.Map;

 

@Data

public class UserRole {

     private Integer id;

     private List<User> users;

     private Map<Role, User> map;

}

2.先注入对应的User对象和Role对象,然后再注入UserRole对象:

?

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

<bean id= "user1" class = "com.xl.spring.xlIoc.beans.User" >

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

     <property name= "name" value= "张生" />

</bean>

<bean id= "user2" class = "com.xl.spring.xlIoc.beans.User" >

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

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

</bean>

<bean id= "role1" class = "com.xl.spring.xlIoc.beans.Role" >

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

     <property name= "name" value= "管理员" />

</bean>

<bean id= "role2" class = "com.xl.spring.xlIoc.beans.Role" >

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

     <property name= "name" value= "普通用户" />

</bean>

<bean id= "userRole" class = "com.xl.spring.xlIoc.beans.UserRole" >

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

     <property name= "users" >

         <list>

             <ref bean= "user1" />

             <ref bean= "user2" />

         </list>

     </property>

     <property name= "map" >

         <map>

             <entry key-ref= "role1" value-ref= "user1" />

             <entry key-ref= "role2" value-ref= "user2" />

         </map>

     </property>

</bean>

3.创建对象并使用:

?

1

2

3

4

5

public static void testComplexBean() {

     ApplicationContext ac = new ClassPathXmlApplicationContext( "springioc.xml" );

     UserRole userRole = (UserRole) ac.getBean( "userRole" );

     System.out.println(userRole);

}

到此这篇关于Spring Xml装配Bean的文章就介绍到这了,更多相关Spring Xml装配Bean内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/qmcl-biu/p/15488361.html

查看更多关于Spring Xml装配Bean的思路详解的详细内容...

  阅读:15次