IOC操作Bean管理XML方式(P名称空间注入)

P名称空间注入(用的不多,但是也是一种方式)

目录

P名称空间注入(用的不多,但是也是一种方式)

(1)使用P名称空间注入,可以简化基于xml 配置方式

第一步:添加p 名称空间在配置文件中(和上面的xmlns区别就在于最后的beans和p)

(2)进行属性注入,在标签里面进行操作

第二步:用p标签简化后

测试结果:


 

 

 

 

(1)使用P名称空间注入,可以简化基于xml 配置方式

首先,我们先来看看初始的Spring的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

 P名称空间注入的前提:

修改xmlns中的约束条件:

第一步:添加p 名称空间在配置文件中(和上面的xmlns区别就在于最后的beans和p)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      <!--添加p名称空间在配置文件中-->
       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>

 

(2)进行属性注入,在<bean>标签里面进行操作

其次,我们先来看看初始的Spring的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--配置Book对象创建-->
<bean id="book" class="com.lbj.spring5.Book">
    <!--使用property完成属性注入
    name:类里面的属性名称
    value:向属性注入的值
    -->
    <property name="bname" value="红楼梦"></property>
</bean>

</beans>

 

第二步:用p标签简化后

<?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">

<!--配置Book对象创建-->
<!--使用p标签简化注入-->
<bean id="book" class="com.lbj.spring5.Book" p:bname="红楼梦">
</bean>

</beans>

 

测试结果:

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值