<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                你可以以定義屬性和構造參數來引用其他bean,或者定義為內部值.xml的元素`<property/> `和` <constructor-arg/>`用來支持配置. [TOC] ## Straight values (primitives, Strings, and so on) `<property/>`元素的屬性`value`表現為字符串,spring的[conversion service](https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/core.html#core-convert-ConversionService-API) 會把它轉為正確的類型. ~~~xml <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 調用 setDriverClassName(String)把值注入 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="username" value="root"/> <property name="password" value="masterkaoli"/> </bean> ~~~ 下面使用[p-namespace](https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/core.html#beans-p-namespace)簡寫 ~~~xml <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="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/mydb" p:username="root" p:password="masterkaoli"/> </beans> ~~~ 上面的方式很簡潔,但是在運行時容易有錯別字,建議使用帶幫助功能的IDE,如 IntelliJ IDEA 或 the Spring Tool Suite. 你還可以配置java.util.Properties實例 ~~~xml <bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- java.util.Properties類型 --> <property name="properties"> <value> jdbc.driver.className=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mydb </value> </property> </bean> ~~~ ### The idref element idref元素只是一種防錯的方式,可以將容器中另一個bean的id(字符串值 - 不是引用)傳遞給`<constructor-arg />`或`<property />`元素。 ~~~xml <bean id="theTargetBean" class="..."/> <bean id="theClientBean" class="..."> <property name="targetName"> <idref bean="theTargetBean"/> </property> </bean> ~~~ 上面的方式和下面是等效的 ~~~xml <bean id="theTargetBean" class="..." /> <bean id="client" class="..."> <property name="targetName" value="theTargetBean"/> </bean> ~~~ 第一種方式比第二種方式更好一點,因為idref允許spring在部署的時候去校驗bean是否存在.第二種方式value只有在bean實例化是才會校驗 > idref元素的屬性local從4.0開始不再支持了.因為local不支持規則bean之外的引用,要升級到4.0,需要把屬性local改為bean. > 元素<idref/>配置的地方帶來一個價值就是在`ProxyFactoryBean `配置的spring AOP攔截器,當指定攔截器名稱時,使用`<idref/>`避免拼寫錯誤實例的id. ## References to other beans (collaborators) 元素`<constructor-arg/>` 或 `<property/>`的子元素`<ref/>`指向了另一個bean.`<ref/>`標簽的屬性`bean`指向目標bean的id或name值. ~~~xml <ref bean="someBean"/> ~~~ 使用`parent`屬性指定引用的父容器內的bean.當你需要通過代理包裝父容器的bean時,名稱保持一致. ~~~xml <!--父容器--> <bean id="accountService" class="com.foo.SimpleAccountService"> </bean> ~~~ ~~~xml <!--子容器--> <bean id="accountService" <!-- 和父容器的名字一樣 --> class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref parent="accountService"/> <!-- 引用父容器的bean --> </property> </bean> ~~~ ## Inner beans `<property/>` 或 `<constructor-arg/>`內部的`<bean/>`定義為內部類 ~~~xml <bean id="outer" class="..."> <!-- 在內部定義bean --> <property name="target"> <bean class="com.example.Person"> <!-- 這是內部類 --> <property name="name" value="Fiona Apple"/> <property name="age" value="25"/> </bean> </property> </bean> ~~~ 內部類不需要定義id和name,定義了也不起作用,包括范圍也無效,內部類是匿名的且通過外部類來創建,不可能把內部類注入到其他bean里. ## Collections 元素`<list/>, <set/>, <map/>, <props/> `分別對應java的集合`List, Set, Map, Properties` ~~~xml <bean id="moreComplexObject" class="example.ComplexObject"> <!-- 調用setAdminEmails(java.util.Properties) --> <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> <!-- 調用setSomeList(java.util.List) --> <property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myDataSource" /> </list> </property> <!-- 調用setSomeMap(java.util.Map) --> <property name="someMap"> <map> <entry key="an entry" value="just some string"/> <entry key ="a ref" value-ref="myDataSource"/> </map> </property> <!--調用setSomeSet(java.util.Set) --> <property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource" /> </set> </property> </bean> ~~~ ### Collection merging spring支持集合的合并,元素`<list/>, <set/>, <map/>, <props/> `可以嵌套元素`<list/>, <set/>, <map/>, <props/> `子元素的值會覆蓋父元素的值 ~~~xml <beans> <bean id="parent" abstract="true" class="example.ComplexObject"> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.com</prop> <prop key="support">support@example.com</prop> </props> </property> </bean> <bean id="child" parent="parent"> <property name="adminEmails"> <!-- 在子集合上指定合并 --> <props merge="true"> <prop key="sales">sales@example.com</prop> <prop key="support">support@example.co.uk</prop> </props> </property> </bean> <beans> ~~~ 當`child`實例化后,會擁有`parent`的屬性,并且覆蓋了`support`的值,最終為 ~~~properties administrator=administrator@example.com sales=sales@example.com support=support@example.co.uk ~~~ 其他集合元素`<list/>, <set/>, <map/> `是類似的,注意list會有order屬性. ### Limitations of collection merging 集合合并,只能在子元素上設置,并且相同類型的才可以合并,否則會有異常. ### Strongly-typed collection 在java5之后,可以強制限制集合的允許的類型,如果bean注入強類型的集合,可以利用spring的類型轉換把集合轉成對應的類型,然后在注入bean. ~~~java public class Foo { private Map<String, Float> accounts; public void setAccounts(Map<String, Float> accounts) { this.accounts = accounts; } } ~~~ ~~~xml <beans> <bean id="foo" class="x.y.Foo"> <property name="accounts"> <map> <entry key="one" value="9.99"/> <entry key="two" value="2.75"/> <entry key="six" value="3.99"/> </map> </property> </bean> </beans> ~~~ ## Null and empty string values spring對待空參數或屬性是空字符串("") ~~~xml <bean class="ExampleBean"> <property name="email" value=""/> </bean> ~~~ 上面的配置,等效如下java代碼 ~~~java exampleBean.setEmail(""); ~~~ 元素`<null/>`處理null值 ~~~xml <bean class="ExampleBean"> <property name="email"> <null/> </property> </bean> ~~~ 上面配置等效如下java代碼 ~~~java exampleBean.setEmail(null); ~~~ ## XML shortcut with the p-namespace ## XML shortcut with the c-namespace ## Compound property names
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看