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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 【第五章】Spring表達式語言 之 5.4在Bean定義中使用EL—跟我學spring3 ## ### 5.4.1? xml風格的配置 SpEL支持在Bean定義時注入,默認使用“#{SpEL表達式}”表示,其中“#root”根對象默認可以認為是ApplicationContext,只有ApplicationContext實現默認支持SpEL,獲取根對象屬性其實是獲取容器中的Bean。 首先看下配置方式(chapter5/el1.xml)吧: 1. &lt;bean?id="world"?class="java.lang.String"&gt;?? 2. &lt;constructor-arg?value="#{'?World!'}"/&gt;?? 3. &lt;/bean&gt;?? 4. &lt;bean?id="hello1"?class="java.lang.String"&gt;?? 5. &lt;constructor-arg?value="#{'Hello'}#{world}"/&gt;?? 6. &lt;/bean&gt;???? 7. &lt;bean?id="hello2"?class="java.lang.String"&gt;?? 8. &lt;constructor-arg?value="#{'Hello'?+?world}"/&gt;?? 9. &lt;!--?不支持嵌套的?--&gt;?? 10. &lt;!--&lt;constructor-arg?value="#{'Hello'#{world}}"/&gt;--&gt;?? 11. &lt;/bean&gt;?? 12. &lt;bean?id="hello3"?class="java.lang.String"&gt;?? 13. &lt;constructor-arg?value="#{'Hello'?+?@world}"/&gt;?? 14. &lt;/bean&gt;?? 模板默認以前綴“#{”開頭,以后綴“}”結尾,且不允許嵌套,如“#{'Hello'#{world}}”錯誤,如“#{'Hello' + world}”中“world”默認解析為Bean。當然可以使用“@bean”引用了。 接下來測試一下吧: 1. @Test?? 2. public?void?testXmlExpression()?{?? 3. ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext("chapter5/el1.xml");?? 4. String?hello1?=?ctx.getBean("hello1",?String.class);?? 5. String?hello2?=?ctx.getBean("hello2",?String.class);?? 6. String?hello3?=?ctx.getBean("hello3",?String.class);?? 7. Assert.assertEquals("Hello?World!",?hello1);?? 8. Assert.assertEquals("Hello?World!",?hello2);?? 9. Assert.assertEquals("Hello?World!",?hello3);?? 10. }????? 是不是很簡單,除了XML配置方式,Spring還提供一種注解方式@Value,接著往下看吧。 ### 5.4.2? 注解風格的配置 基于注解風格的SpEL配置也非常簡單,使用@Value注解來指定SpEL表達式,該注解可以放到字段、方法及方法參數上。 測試Bean類如下,使用@Value來指定SpEL表達式: 1. package?cn.javass.spring.chapter5;?? 2. import?org.springframework.beans.factory.annotation.Value;?? 3. public?class?SpELBean?{?? 4. @Value("#{'Hello'?+?world}")?? 5. private?String?value;?? 6. //setter和getter由于篇幅省略,自己寫上?? 7. }?? 首先看下配置文件(chapter5/el2.xml): 1. &lt;?xml?version="1.0"?encoding="UTF-8"?&gt;?? 2. &lt;beans??xmlns="http://www.springframework.org/schema/beans"?? 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?? 4. xmlns:context="http://www.springframework.org/schema/context"?? 5. xsi:schemaLocation="?? 6. http://www.springframework.org/schema/beans?? 7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd?? 8. http://www.springframework.org/schema/context?? 9. http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;?? 10. &lt;context:annotation-config/&gt;?? 11. &lt;bean?id="world"?class="java.lang.String"&gt;?? 12. &lt;constructor-arg?value="#{'?World!'}"/&gt;?? 13. &lt;/bean&gt;?? 14. &lt;bean?id="helloBean1"?class="cn.javass.spring.chapter5.SpELBean"/&gt;?? 15. &lt;bean?id="helloBean2"?class="cn.javass.spring.chapter5.SpELBean"&gt;?? 16. &lt;property?name="value"?value="haha"/&gt;?? 17. &lt;/bean&gt;?? 18. &lt;/beans&gt;?? 配置時必須使用“**&lt;context:annotation-config/&gt;**”來開啟對注解的支持。 有了配置文件那開始測試吧: 1. @Test?? 2. public?void?testAnnotationExpression()?{?? 3. ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext("chapter5/el2.xml");?? 4. SpELBean?helloBean1?=?ctx.getBean("helloBean1",?SpELBean.class);?? 5. Assert.assertEquals("Hello?World!",?helloBean1.getValue());?? 6. SpELBean?helloBean2?=?ctx.getBean("helloBean2",?SpELBean.class);?? 7. Assert.assertEquals("haha",?helloBean2.getValue());?? 8. }?? 其中“helloBean1 ”值是SpEL表達式的值,而“helloBean2”是通過setter注入的值,這說明setter注入將覆蓋@Value的值。 ### 5.4.3? 在Bean定義中SpEL的問題 如果有同學問“#{我不是SpEL表達式}”不是SpEL表達式,而是公司內部的模板,想換個前綴和后綴該如何實現呢? 那我們來看下Spring如何在IoC容器內使用BeanExpressionResolver接口實現來求值SpEL表達式,那如果我們通過某種方式獲取該接口實現,然后把前綴后綴修改了不就可以了。 此處我們使用BeanFactoryPostProcessor接口提供postProcessBeanFactory回調方法,它是在IoC容器創建好但還未進行任何Bean初始化時被ApplicationContext實現調用,因此在這個階段把SpEL前綴及后綴修改掉是安全的,具體代碼如下: 1. package?cn.javass.spring.chapter5;?? 2. import?org.springframework.beans.BeansException;?? 3. import?org.springframework.beans.factory.config.BeanFactoryPostProcessor;?? 4. import?org.springframework.beans.factory.config.ConfigurableListableBeanFactory;?? 5. import?org.springframework.context.expression.StandardBeanExpressionResolver;?? 6. public?class?SpELBeanFactoryPostProcessor?implements?BeanFactoryPostProcessor?{?? 7. @Override?? 8. public?void?postProcessBeanFactory(ConfigurableListableBeanFactory?beanFactory)?? 9. throws?BeansException?{?? 10. StandardBeanExpressionResolver?resolver?=?(StandardBeanExpressionResolver)?beanFactory.getBeanExpressionResolver();?? 11. resolver.setExpressionPrefix("%{");?? 12. resolver.setExpressionSuffix("}");?? 13. }?? 14. }?? 首先通過 ConfigurableListableBeanFactory的getBeanExpressionResolver方法獲取BeanExpressionResolver實現,其次強制類型轉換為StandardBeanExpressionResolver,其為Spring默認實現,然后改掉前綴及后綴。 開始測試吧,首先準備配置文件(chapter5/el3.xml): 1. &lt;?xml?version="1.0"?encoding="UTF-8"?&gt;?? 2. &lt;beans??xmlns="http://www.springframework.org/schema/beans"?? 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?? 4. xmlns:context="http://www.springframework.org/schema/context"?? 5. xsi:schemaLocation="?? 6. http://www.springframework.org/schema/beans?? 7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd?? 8. http://www.springframework.org/schema/context?? 9. http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;?? 10. &lt;context:annotation-config/&gt;?? 11. &lt;bean?class="cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor"/&gt;?? 12. &lt;bean?id="world"?class="java.lang.String"&gt;?? 13. &lt;constructor-arg?value="%{'?World!'}"/&gt;?? 14. &lt;/bean&gt;?? 15. &lt;bean?id="helloBean1"?class="cn.javass.spring.chapter5.SpELBean"/&gt;?? 16. &lt;bean?id="helloBean2"?class="cn.javass.spring.chapter5.SpELBean"&gt;?? 17. &lt;property?name="value"?value="%{'Hello'?+?world}"/&gt;?? 18. &lt;/bean&gt;?? 19. &lt;/beans&gt;?? 配置文件和注解風格的幾乎一樣,只有SpEL表達式前綴變為“%{”了,并且注冊了“cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor”Bean,用于修改前綴和后綴的。 寫測試代碼測試一下吧: 1. @Test?? 2. public?void?testPrefixExpression()?{?? 3. ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext("chapter5/el3.xml");?? 4. SpELBean?helloBean1?=?ctx.getBean("helloBean1",?SpELBean.class);?? 5. Assert.assertEquals("#{'Hello'?+?world}",?helloBean1.getValue());?? 6. SpELBean?helloBean2?=?ctx.getBean("helloBean2",?SpELBean.class);?? 7. Assert.assertEquals("Hello?World!",?helloBean2.getValue());?? 8. }?????? 此處helloBean1 中通過@Value注入的“#{'Hello' + world}”結果還是“#{'Hello' + world}”說明不對其進行SpEL表達式求值了,而helloBean2使用“%{'Hello' + world}”注入,得到正確的“"Hello World!”。
                  <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>

                              哎呀哎呀视频在线观看