前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java-Spring框架-基于xml方式注入属性

Java-Spring框架-基于xml方式注入属性

作者头像
吃猫的鱼Code
发布2023-03-12 14:35:14
3300
发布2023-03-12 14:35:14
举报

基于xml方式注入属性

DI依赖注入,对象的属性注入值;(spring实现)

第一种实现方式:基于对象属性set方法实现

(基于set方法实现,要求类中有set方法)

    <bean id="userEntity" class="cn.fish9.spring.UserEntity">
        <property name="key1" value="1"></property>
        <property name="key2" value="1"></property>
        <!-可以使用ref来传递对象--->
        <property name="key3" ref="menDao"></property>
    </bean>

Name:类中的属性名称

Value:需要注入属性值

第二种实现方式:基于有参数构造函数实现

    <bean id="userEntity" class="cn.fish9.spring.UserEntity">
        <constructor-arg name="key1" value="value1"></constructor-arg>
        <constructor-arg name="key2" value="value2"></constructor-arg>
    </bean>

//或者使用索引来替代name
    <bean id="userEntity" class="cn.fish9.spring.UserEntity">
        <constructor-arg index="0" value="value1"></constructor-arg>
        <constructor-arg index="1" value="value2"></constructor-arg>
    </bean>

其中UserEntity类中需要有有参构造方法,如下:

    public UserEntity(String key1,String key2){
    }

使用p标签为属性注入值

(基于set方法实现,要求类中有set方法)

头部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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    ......
</beans>

区别在于添加了

xmlns:p="http://www.springframework.org/schema/p"

使用p标签注入值

<bean id="userEntity" class="cn.fish9.spring.UserEntity" p:key1="123" p:key2="1234">
    </bean>

bean属性注入空值与特殊字符

在bean属性中注入空值,可以在<property>标签中添加<null>标签,来表示当前的值为null

    <bean id="userEntity" class="cn.fish9.spring.UserEntity">
        <property name="key1" >
            <null></null>
        </property>
    </bean>

bean属性注入特殊字符的方式

错误写法:

<!--首先下面是错误写法,无法注入特殊字符-->
    <bean id="userEntity" class="cn.fish9.spring.UserEntity">
        <property name="key1" value="<<test>>"></property>
    </bean>

需要注入特殊字符,有两种方式:

①转移特殊符号

<<转移为&lt;&lt;

>>转移为&gt;&gt;

②使用cdata

<bean id="userEntity" class="cn.fish9.spring.UserEntity">
    <property name="key1" >
        <value><![CDATA[<<test>>]]></value>
    </property>
</bean>

spring注入属性外部bean

<!--memberService注入到ioc容器中-->
<bean id="memberService"  class="cn.fish9.spring.MemberService">
<!--
name="membberDao" MemberService类中属性的名称
ref memberDao 在ioc容器中注入的beanid
-->
    <property name="memberDao" ref="memberDao"></property>
</bean> 
<!--MemberDaoImp注入到ioc容器中-->
<bean id="memberDao" class="cn.fish9.spring.MemberDaoImpl"></bean>

spring注入内部bean对象

<bean id="userEntity" class="cn.fish9.spring.UserEntity">
    <!--普通属性注入-->
    <property name="name" value="fish9"></property>
    <property name="addres" value="湖北省武汉市"></property>
    <!--注入内部bean对象-->
    <property name="deptEntity" >
        <bean id="deptEntity" class="cn.fish9.spring.DeptEntity">
            <property name="name" value="教育部门名字"></property>
        </bean>
    </property>
</bean>

级联赋值bean对象

注意:memberDao对象中的属性需要有get方法才能这样传递级联赋值bean对象。

<bean id="memberService"  class="cn.fish9.spring.MemberService">
    <property name="memberDao" ref="memberDao"></property>
    <property name="memberDao.name" value="部门名称"></property>
</bean> 
<bean id="memberDao" class="cn.fish9.spring.MemberDaoImpl"></bean>

注入集合类型属性

 <bean id="stuEntity" class="cn.fish9.spring.entity.StuEntity">
        <property name="list" >
            <list>
                <value>list01</value>
                <value>list02</value>
            </list>
        </property>

        <property name="array" >
            <array>
                <value>array01</value>
                <value>array02</value>
            </array>
        </property>

        <property name="map" >
            <map>
                <entry key="cat" value="test"></entry>
                <entry key="fish" value="ee"></entry>
            </map>
        </property>

        <property name="set" >
            <set>
                <value>array01</value>
                <value>array02</value>
            </set>
        </property>
    </bean>

注入集合类型为对象

<!--注入list类型为对象类型ref bean-->        
<property name="courser" >
            <list>
                <ref bean="courseEntity_java"></ref>
                <ref bean="courseEntity_c"></ref>
            </list>
        </property>

<!--下面开始创建需要注入的对象-->
<!--java课程的对象-->
<bean id="courseEntity_java" class="cn.fish9.entity.CourseEntity">
    <property name="name" value="java"></property>
</bean>
<!--c课程的对象-->
<bean id="courseEntity_c" class="cn.fish9.entity.CourseEntity">
    <property name="name" value="c"></property>
</bean>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基于xml方式注入属性
    • 第一种实现方式:基于对象属性set方法实现
      • 第二种实现方式:基于有参数构造函数实现
        • 使用p标签为属性注入值
          • bean属性注入空值与特殊字符
            • spring注入属性外部bean
              • spring注入内部bean对象
                • 级联赋值bean对象
                  • 注入集合类型属性
                    • 注入集合类型为对象
                    相关产品与服务
                    容器服务
                    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档