日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring的两种容器后处理器(PropertyPlaceholderConfigurer和PropertyOverrideConfigurer)

發布時間:2025/3/20 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring的两种容器后处理器(PropertyPlaceholderConfigurer和PropertyOverrideConfigurer) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、容器后處理器

  • Spring有如下幾個常用容器后處理器:
    PropertyPlaceholderConfigurer:屬性點位符配置器
    PropertyOverrideConfigurer:重寫占位符配置器
    CustomAutowireConfigurer:自定義自動裝配的配置器
    CustomScopeConfigurer:自定義作用域的配置器

  • 容器后處理器用于負責處理容器本身,須實現BeanFactoryPostProcessor接口
    并實現該接口的postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)方法。

  • 如果采用ApplicationContext作為Spring容器,會自動注冊容器后處理器,如果采用BeanFactory作為Spring容器,則需要手動調用該容器來處理Spring容器。

  • 代碼示例:
    bean.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:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><bean id="user" class="com.kting.agx.test.User"/></beans>

    Java代碼:

    //容器后處理器 public class MyClass implements BeanFactoryPostProcessor{ @Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)throws BeansException {System.out.println("這是容器后處理器,名字是:"+beanFactory);}/*** 測試*/public static void main(String[] args) {//采用ApplicationContext作為Spring容器ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");User user = (User) app.getBean("user");//"user"參數是在bean.xml里注入User類時的iduser.getMessage();}}

    如果用BeanFactory作為Spring容器,則需要在bean.xml里配置MyClass類,即手動注冊容器后處理器:

    <bean id="myId" class="com.kting.agx.test.MyClass"/>

    二、屬性占位符配置器:PropertyPlaceholderConfigurer

    該容器后處理器負責讀取Properties屬性文件里的屬性值,并將這些屬性值設置成Spring配置文年的元數據,例如數據的URL、用戶名、密碼等等…..

    base-conf-local.properties文件代碼:

    com.kting.agx.base.dataSource0.jdbc.driverClassName=com.mysql.jdbc.Driver com.kting.agx.base.dataSource0.jdbc.url=jdbc:mysql://localhost:3306/agx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true com.kting.agx.base.dataSource0.jdbc.username=agx com.kting.agx.base.dataSource0.jdbc.password=agx987654com.kting.agx.base.dataSource1.jdbc.driverClassName=com.mysql.jdbc.Driver com.kting.agx.base.dataSource1.jdbc.url=jdbc:mysql://localhost/agx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true com.kting.agx.base.dataSource1.jdbc.username=agx com.kting.agx.base.dataSource1.jdbc.password=agx987654

    bean.xml配置文件代碼:

    <!-- 這樣配置后,可通過${}形式獲取前面properties文件里的值 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>base-conf-local.properties</value><!-- 如果有多個配置文件,可依次列出來 : --><!-- <value>base-conf-online.properties</value> --></list></property> </bean> <bean id="write_dataSource" class="com.alibaba.druid.pool.DruidDataSource"parent="commonDataSourceProperties" init-method="init" destroy-method="close"><!-- 基本屬性 url、user、password --><property name="url" value="${com.kting.agx.base.dataSource0.jdbc.url}" /><property name="username" value="${com.kting.agx.base.dataSource0.jdbc.username}" /><property name="password" value="${com.kting.agx.base.dataSource0.jdbc.password}" /> </bean><bean id="read_dataSource" class="com.alibaba.druid.pool.DruidDataSource"parent="commonDataSourceProperties" init-method="init" destroy-method="close"><!-- 基本屬性 url、user、password --><property name="url" value="${com.kting.agx.base.dataSource1.jdbc.url}" /><property name="username" value="${com.kting.agx.base.dataSource1.jdbc.username}" /><property name="password" value="${com.kting.agx.base.dataSource1.jdbc.password}" /> </bean>

    如果在bean.xml文件的頭部分導入了context Schema:

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

    則我們可通過如下方式配置該屬性占位符:

    <context:property-placeholder location="classpath:base-conf-local.properties" />

    如果有多個properties文件時,可用通配符的形式

    <context:property-placeholder location="classpath*:*-conf-local.properties" />

    三、重寫占位符配置器:PropertyOverrideConfigurer

    總結

    以上是生活随笔為你收集整理的Spring的两种容器后处理器(PropertyPlaceholderConfigurer和PropertyOverrideConfigurer)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。