Spring-Context之七:使用p-namesapce和c-namespace简化bean的定义

在Spring中定义bean的方式多种多样,即使使用xml的方式来配置也能派生出很多不同的方式。

比如如下的bean定义:

1
2
3
4
5
6
7
8
9
10
11
12
<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">


    <bean id="person" class="Person">
        <property name="name" value="Tom"/>
        <property name="age" value="20"/>
    </bean>

</beans>

这样的bean有三行,通过使用p-namespace以后可以简化为一行。

1
2
3
4
5
6
7
8
9
<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">

    <bean id="person" class="Person" p:name="Tom" p:age="20"/>

</beans>

那么什么是p-namespace那?它的作用就是使用xml中的元素属性取代<property/>节点来定义bean的属性。这个神奇的p是什么东西那?它其实是使用了namespace的xml扩展配置格式。beans的配置格式是定义在一个xsd格式中的(即 http://www.springframework.org/schema/beans/spring-beans.xsd),但p却没有一个xsd格式文件与其对应,但是它可以被spring内核解析处理。

上面只是演示了对属性为普通值的时使用p-namespace的注入,如果属性为另一个bean的引用时该如何处理那?很简单。

这是使用正常方式注入属性。

1
2
3
4
5
6
    <bean id="messageService" class="SimpleMessageService"/>
    <bean id="messageHandler" class="MessageHandler">
        <property name="messageService">
            <ref bean="messageService" />
        </property>
    </bean>

使用p-namespace后是这样的。

1
2
    <bean id="messageService" class="SimpleMessageService"/>
    <bean id="messageHandler" class=“MessageHandler” p:messageService-ref=“messageService”/>

加上-ref后缀即表示是对一个bean的引用。

那既然setter方法注入bean可以使用p-namespace,那么构造器方式注入有没有相应的简写那?答案是肯定的,那就是c-namespace,原理和使用方法与p-namespace大同小异。

使用c-namespace前:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="person" class="Person">
        <constructor-arg name="name">
            <value>Tom</value>
        </constructor-arg>
        <constructor-arg name="age" value="20"/>
    </bean>

</beans>

使用c-namespace后:

1
2
3
4
5
6
7
8
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="person"  c:name="Tom" c:age="20"/>
</beans>

也可以使用-ref后缀来表示对另一个bean的引用。

1
2
 <bean id="messageService" class="SimpleMessageService"/>
    <bean id="messageHandler" class="MessageHandler" c:messageService-ref="messageService"/>

在前面章节讲解构造器注入时,可以使用构造参数索引来注入依赖,c-namespace也支持这一方式。

1
2
3
4
5
6
7
8
9
10
11
12
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="person”  c:_0="Tom" c:_1="20"/>

<bean id="messageService" class="SimpleMessageService"/>
<bean id="messageHandler" class="MessageHandler" c:_0-ref="messageService"/>

</beans>

怎么样,是不是很强大啊。但是太过强大也容易伤人伤己。在项目中使用这些技巧之前最好先和项目成员达成一致。

本例中的源码请在我的GitHub上自行下载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要彻底删除一个namespace,你可以按照以下步骤进行操作: 1. 首先,你可以使用kubectl命令查询该namespace下的所有pod,以确认是否还有正在运行的pod。你可以使用以下命令进行查询: ``` kubectl get pods -n <namespace> ``` 2. 如果还有正在运行的pod,你可以使用kubectl命令删除这些pod。你可以使用以下命令进行删除: ``` kubectl delete pod <podname> -n <namespace> ``` 3. 接下来,你可以使用kubectl命令查询该namespace下的所有deployment,以确认是否还有正在运行的deployment。你可以使用以下命令进行查询: ``` kubectl get deployment -n <namespace> ``` 4. 如果还有正在运行的deployment,你可以使用kubectl命令删除这些deployment。你可以使用以下命令进行删除: ``` kubectl delete deployment <deployment名> -n <namespace> ``` 5. 最后,你可以使用kubectl命令删除该namespace。你可以使用以下命令进行删除: ``` kubectl delete namespace <namespace> ``` 请注意,删除一个namespace将会删除该namespace下的所有资源,包括pod、deployment等。确保在执行删除操作之前,你已经确认了要删除的资源,并且备份了重要的数据。 #### 引用[.reference_title] - *1* *2* [k8s中如何正确删除pod](https://blog.csdn.net/Aaron_Zhang939/article/details/129709530)[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] - *3* [K8S如何强制删除namespace](https://blog.csdn.net/LONG_Yi_1994/article/details/122298459)[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、付费专栏及课程。

余额充值