11、spring的bean基础(3)

11、spring的bean基础(3)

在本文中,主要介绍的知识点有以下三个

  1. ListFactoryBean
  2. SetFactoryBean
  3. MapFactoryBean

现项目中存在一个实体类,将用于以下三个例子的演示
HelloWorld.java

public class HelloWorld{
    private List list;
    private Set set;
    private Map map;

    //.....
}

ListFactoryBean示例
在spring bean配置文件中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="list">
            <bean class="org.springframework.beans.factory.config.ListFactoryBean">
                <property name="targetListClass">
                    <value>java.util.ArrayList</value>
                </property>
                <property name="sourceList">
                    <list>
                        <value>hello</value>
                        <value>1</value>
                        <value>three</value>
                    </list>
                </property>
            </bean>
        </property>
    </bean>

</beans>

还可以使用下方式:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <bean id="CustomerBean" class="com.yiibai.common.Customer">
        <property name="list">
            <util:list list-class="java.util.ArrayList">
                <value>Hello</value>
                <value>1</value>
                <value>three</value>
            </util:list>
        </property>
    </bean>

</beans>

如果你不把util:list模式包含进配置文件中:或许会出现以下错误:

Caused by: org.xml.sax.SAXParseException: 
    The prefix "util" for element "util:list" is not bound.

SetFactoryBean实例

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="set">
            <bean class="org.springframework.beans.factory.config.SetFactoryBean">
                <property name="targetSetClass">
                    <value>java.util.HashSet</value>
                </property>
                <property name="sourceSet">
                    <list>
                        <value>one</value>
                        <value>2</value>
                        <value>three</value>
                    </list>
                </property>
            </bean>
        </property>
    </bean>
</beans>

也可以使用以下模式:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="set">
            <util:set set-class="java.util.HashSet">
                <value>one</value>
                <value>2</value>
                <value>three</value>
            </util:set>
        </property>
    </bean>
</beans>

MapFactoryBean实例

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="map">
            <bean class="org.springframework.beans.factory.config.MapFactoryBean">
                <property name="targetMapClass">
                    <value>java.util.HashMap</value>
                </property>
                <property name="sourceMap">
                    <map>
                        <entry key="Key1" value="one" />
                        <entry key="Key2" value="two" />
                        <entry key="Key3" value="three" />
                    </map>
                </property>
            </bean>
        </property>
    </bean>
</beans>

也可以使用以下方式:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="HelloWorld" class="com.main.HelloWorld">
        <property name="map">
            <util:map map-class="java.util.HashMap">
                <entry key="Key1" value="1" />
                <entry key="Key2" value="2" />
                <entry key="Key3" value="3" />
            </util:map>
        </property>
    </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring基础配置主要包括以下几个方面的内容:\[1\]\[2\]\[3\] 1. Spring的类包必须已经放在Spring的类容器下面。这意味着我们需要将Spring的类包放在项目的类路径下,以便Spring容器能够正确加载和管理这些类。 2. 应用程序应当为Spring提供完备的Bean的配置信息。这些配置信息可以通过XML文件或者注解的方式进行定义,用于描述Bean的属性、依赖关系、行为配置等。 3. Bean的类都已经放在Spring的类容器下面。这意味着我们需要将所有需要由Spring管理的Bean的类放在Spring容器能够扫描到的位置,以便Spring能够正确实例化和管理这些Bean。 4. Spring的配置文件是Spring容器对Bean进行生产以及关系注入的图纸。这个配置文件是一个或多个标准的XML文档,其中最常见的是ApplicationContext.xml,它是Spring的默认配置文件。在容器启动时,如果找不到其他的配置文件,Spring会尝试加载这个默认的配置文件。 5. Bean的配置信息由Bean的元数据信息组成,包括Bean的实现类、属性信息、依赖关系、行为配置以及创建方式定义等。这些信息用于告诉Spring容器如何实例化和装配Bean,以及如何为上层应用提供准备就绪的运行环境。 综上所述,Spring基础配置包括将Spring的类包放在类路径下、提供完备的Bean的配置信息、将Bean的类放在Spring容器能够扫描到的位置、配置Spring的配置文件以及定义Bean的元数据信息。这些配置将帮助Spring容器正确加载和管理Bean,为应用程序提供准备就绪的运行环境。 #### 引用[.reference_title] - *1* *2* *3* [Spring bean配置的六种方式](https://blog.csdn.net/echizao1839/article/details/88063013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值