主方法中获取Bean实例:

1

Person person=(Person) ctx.getBean(\"person\");

运行后输出:

1

Person [name=Tom, age=23, car=Car [brand=Audi, corp=上海, price=500000.0, maxSpeed=0]]

5.2 内部Bean

当 Bean 实例仅仅给一个特定的属性使用时, 可以将其声明为内部 Bean。 内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里,不需要设置任何 id 或 name 属性。内部 Bean 不能使用在任何其他地方。

同实现上面的功能,在 中进行配置:

1

2

3

4

5

6

7

8

9

10

11

<bean id=\"person1\" class=\"com.java.spring.Person\">

  <property name=\"name\" value=\"Tom\"></property>

  <property name=\"age\" value=\"23\"></property>

  <property name=\"car\">

    <bean class=\"com.java.spring.Car\">

      <constructor-arg value=\"Audi\" index=\"0\"></constructor-arg>

      <constructor-arg value=\"长春一汽\" index=\"1\"></constructor-arg>

      <constructor-arg value=\"500000.0000\" index=\"2\"></constructor-arg>

    </bean>

  </property>

</bean>

6.级联属性赋值

和 Struts、Hiberante 等框架一样,Spring 支持级联属性的配置。

1

2

3

4

5

6

7

8

9

10

11

<bean id=\"car\" class=\"com.java.spring.Car\">

  <constructor-arg value=\"Audi\" index=\"0\"></constructor-arg>

  <constructor-arg value=\"上海\" index=\"1\"></constructor-arg>

  <constructor-arg value=\"500000.0000\" index=\"2\"></constructor-arg>

</bean>

<bean id=\"person\" class=\"com.java.spring.Person\">

  <property name=\"name\" value=\"Tom\"></property>

  <property name=\"age\" value=\"23\"></property>

  <property name=\"car\" ref=\"car\"></property>

  <property name=\"car.price\" value=\"300000.0000\"></property>

</bean>

使用级联属性赋值的前提是car中的price必须有setPrice()方法。

运行后输出:

1

[name=Tom, age=23, car=Car [brand=Audi, corp=上海, price=300000.0, maxSpeed=0]]

7.集合属性的配置

在 Spring中可以通过一组内置的 标签(例如: <list>, <set> 或 <map>) 来配置集合属性。

7.1 配置 java.util.List 类型的属性, 需要指定 <list>  标签, 在标签里包含一些元素。这些标签可以通过 <value> 指定简单的常量值,通过 <ref> 指定对其他 Bean 的引用。 通过<bean> 指定内置 Bean 定义。通过 <null/> 指定空元素。 甚至可以内嵌其他集合。数组的定义和 List 一样, 都使用 <list>。

示例代码:

Person.java

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

package com.java.spring;

 

import java.util.List;

 

public class Person {

    private String name;

    private int age;

    private List<Car> cars;

    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;

    }

    public List<Car> getCars() {

        return cars;

    }

    public void setCars(List<Car> cars) {

        this.cars = cars;

    }

    @Override

    public String toString() {

        return \"Person [name=\" + name + \", age=\" + age + \", cars=\" + cars + \"]\";

    }

}

在 中进行配置:

1

2

3

4

5

6

7

8

9

10

<bean id=\"person\" class=\"com.java.spring.Person\">

  <property name=\"name\" value=\"Tom\"></property>

  <property name=\"age\" value=\"23\"></property>

  <property name=\"cars\">

    <list>

      <ref bean=\"car\"/>

      <ref bean=\"car2\"/>

    </list>

  </property>

</bean>

在主方法中获取Bean实例:

1

Person person=(Person) ctx.getBean(\"person\");

运行后输出:

1

Person [name=Tom, age=23, cars=[Car [brand=Audi, corp=上海, price=500000.0, maxSpeed=0], <br>Car [brand=Audi, corp=长春一汽, price=0.0, maxSpeed=240]]]

7.2 配置 java.util.Set 需要使用 <set> 标签,定义元素的方法与 List 一样。

7.3 Java.util.Map 通过 <map> 标签定义,<map> 标签里可以使用多个 <entry> 作为子标签。每个条目包含一个键和一个值,必须在 <key> 标签里定义键。因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素。可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义。

Person.java

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

package com.java.spring;

 

import java.util.Map;

 

public class Person {

    private String name;

    private int age;

    private Map<String,Car> cars;

    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;

    }

    public Map<String,Car> getCars() {

        return cars;

    }

    public void setCars(Map<String,Car> cars) {

        this.cars = cars;

    }

    @Override

    public String toString() {

        return \"Person [name=\" + name + \", age=\" + age + \", cars=\" + cars + \"]\";

    }

}

在 中进行配置:

1

2

3

4

5

6

7

8

9

10

<bean id=\"person\" class=\"com.java.spring.Person\">

  <property name=\"name\" value=\"Tom\"></property>

  <property name=\"age\" value=\"23\"></property>

  <property name=\"cars\">

    <map>

      <entry key=\"A\" value-ref=\"car\"></entry>

      <entry key=\"B\" value-ref=\"car2\"></entry>

    </map>

  </property>

</bean>

在主方法中获取Bean实例:

1

Person person=(Person) ctx.getBean(\"person\");

运行后输出:

1

Person [name=Tom, age=23, cars={A=Car [brand=Audi, corp=上海, price=500000.0, maxSpeed=0],<br>B=Car [brand=Audi, corp=长春一汽, price=0.0, maxSpeed=240]}]

7.4 使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签。每个 <prop> 标签必须定义 key 属性。

DataSource.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.java.spring;

 

import java.util.Properties;

 

public class DataSource {

    private Properties properties;

    public Properties getProperties() {

        return properties;

    }

    public void setProperties(Properties properties) {

        this.properties = properties;

    }

    @Override

    public String toString() {

        return \"DataSource [properties=\" + properties + \"]\";

    }

}

在 中进行配置:

1

2

3

4

5

6

7

8

9

10

<bean id=\"datasource\" class=\"com.java.spring.DataSource\">

  <property name=\"properties\">

    <props>

      <prop key=\"user\">root</prop>

      <prop key=\"password\">1234</prop>

      <prop key=\"jdbcUrl\">jdbc:mysql:///test</prop>

      <prop key=\"driverClass\">com.mysql.jdbc.Driver</prop>

    </props>

  </property>

</bean>

在主方法中获取Bean实例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.java.spring;

import java.util.Properties;

import org.spring work.context.ApplicationContext;

import org.spring work.context.support.ClassPath ApplicationContext;

 

public class Main {

    public static void main(String[] args){

        //1.创建spring的IOC对象

        ApplicationContext ctx=new ClassPath ApplicationContext(\"applicationContext. \");

        //2.从IOC容器中获取bean实例

        DataSource datasource= (DataSource) ctx.getBean(DataSource.class);

        System.out.println(datasource);

    }

}

运行后输出:

1

DataSource [properties={driverClass=com.mysql.jdbc.Driver, user=root, password=1234, jdbcUrl=jdbc:mysql:///test}]

8.使用 utility scheme 定义集合

使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合。解决这个问题的方法是使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义。使用前需要先导入util命名空间

收藏 打印