spring中beans注入

本文解析了Spring XML配置文件中如何定义并注入不同类型的bean,包括User、UserDao及其实现,展示了属性值的注入方式,如字符串、集合和引用。同时讨论了load-time-weaver的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml文件格式:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

<!-- a service object; we will be profiling its methods -->
<bean id="user" class="com.stone.cn.pojo.User"></bean>
<bean id="userDaoImpl" class="com.stone.cn.dao.impl.UserDaoImpl"></bean>
<bean id="userDaoMysql" class="com.stone.cn.dao.impl.UserDaoMysql"></bean>
<bean id="userDaoOracle" class="com.stone.cn.dao.impl.UserDaoOracle"></bean>

<bean id="userServiceImpl" class="com.stone.cn.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDaoMysql"></property>
</bean>

<!-- this switches on the load-time weaving -->
<!--<context:load-time-weaver/>-->
</beans>

//不同类型的对象值注入格式,单个string name value 对象 name ref 数组array value

<bean id="moreComplexObject" class="example.ComplexObject">
    <!-- results in a setAdminEmails(java.util.Properties) call -->
    <property name="adminEmails">
        <props>
            <prop key="administrator">administrator@example.org</prop>
            <prop key="support">support@example.org</prop>
            <prop key="development">development@example.org</prop>
        </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="someList">
        <list>
            <value>a list element followed by a reference</value>
            <ref bean="myDataSource" />
        </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="someMap">
        <map>
            <entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
    <property name="someSet">
        <set>
            <value>just some string</value>
            <ref bean="myDataSource" />
        </set>
    </property>
</bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值