Spring 在 xml配置文件 或 annotation 注解中 運用Spring EL表達式


Spring  EL

一:在Spring xml 配置文件中運用   Spring EL

Spring EL 采用 #{Sp Expression  Language} 即 #{spring表達式}

1:運用EL表達式的配置文件如下:

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.     http://www.springframework.org/schema/context  
  8.     http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  9.   
  10.   <!-- more bean definitions for data access objects go here -->  
  11.     
  12.   <bean id="book" class="com.myapp.core.spel.xml.Book">  
  13.    <property name="name" value="Effective Java" />  
  14.    <property name="pages" value="300"/>  
  15.   </bean>  
  16.      
  17.    <bean id="person" class="com.myapp.core.spel.xml.Person">  
  18.     <property name="book" value="#{book}" />  
  19.     <property name="bookName" value="#{book.name}"/>  
  20.    </bean>  
  21. </beans>  

在person  bean 的配置中, 屬性 book 引用了  book bean 通過EL表達式 形式是:<property name="book" value="#{book}" /> 相當於 在person bean中注入 book
person屬性中的bookName屬性注入了 book bean中的 name的值  

2:測試以上配置:

Book類:
[java]  view plain copy
 
  1. package com.myapp.core.spel.xml;  
  2.   
  3. public class Book {  
  4.     private  String  name ;  
  5.     private  int   pages;  
  6.     
  7.     public String getName() {  
  8.         return name;  
  9.     }  
  10.     public void setName(String name) {  
  11.         this.name = name;  
  12.     }  
  13.     public int getPages() {  
  14.         return pages;  
  15.     }  
  16.     public void setPages(int pages) {  
  17.         this.pages = pages;  
  18.     }  
  19.         
  20. }  

Person類:
[java]  view plain copy
 
  1. package com.myapp.core.spel.xml;  
  2.   
  3. public class Person {  
  4.    private  Book  book;  
  5.      
  6.    private  String  bookName;  
  7.   
  8.   
  9. public void setBook(Book book) {  
  10.     this.book = book;  
  11. }  
  12.   
  13. public Book getBook(){  
  14.      return  this.book;  
  15. }  
  16.   
  17. public  String   getBookName(){  
  18.     return  this.bookName;  
  19. }  
  20.   
  21. public void setBookName(String bookName) {  
  22.     this.bookName = bookName;  
  23. }  
  24. }  
 
測試類:
[java]  view plain copy
 
  1. package com.myapp.core.spel.xml;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class MainTest {   
  7.     public static void main(String[] args) {  
  8.           
  9.         ApplicationContext  context  = new  ClassPathXmlApplicationContext("resource/spel.xml");  
  10.           
  11.         Person  person =  (Person)context.getBean("person");  
  12.           
  13.         System.out.println(person.getBookName());  
  14.           
  15.         System.out.println(person.getBook().getPages());  
  16.     }  
  17. }  

輸出結果:
[plain]  view plain copy
 
  1. 三月 18, 2013 5:17:18 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
  2. INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@11e831: startup date [Mon Mar 18 17:17:18 CST 2013]; root of context hierarchy  
  3. 三月 18, 2013 5:17:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  4. INFO: Loading XML bean definitions from class path resource [resource/spel.xml]  
  5. 三月 18, 2013 5:17:18 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
  6. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@498b39: defining beans [book,person]; root of factory hierarchy  
  7. Effective Java  
  8. 300  

二:注解中使用 EL

1:xml中配置,掃描含有注解的包;

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.     http://www.springframework.org/schema/context  
  8.     http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  9.   
  10.   <!-- more bean definitions for data access objects go here -->  
  11.     
  12.   
  13. <context:component-scan base-package="com.myapp.core.spel.annotation" />  
  14.      
  15.      
  16. </beans>  

2:相應的類:

Book類:
[java]  view plain copy
 
  1. package com.myapp.core.spel.annotation;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component("book")  
  7. public class Book {  
  8.       
  9.     @Value("Effective Java")  
  10.     private  String  name ;  
  11.       
  12.     @Value("300")  
  13.     private  int   pages;  
  14.     
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.     public int getPages() {  
  22.         return pages;  
  23.     }  
  24.     public void setPages(int pages) {  
  25.         this.pages = pages;  
  26.     }  
  27. }  
在book的屬性中 注入了值。
Person類:
[java]  view plain copy
 
  1. package com.myapp.core.spel.annotation;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component("person")  
  7. public class Person {  
  8.          
  9.       @Value("#{book}")  
  10.        private  Book  book;  
  11.          
  12.       @Value("#{book.name}")  
  13.        private  String  bookName;  
  14.   
  15.   
  16.     public void setBook(Book book) {  
  17.         this.book = book;  
  18.     }  
  19.   
  20.     public Book getBook(){  
  21.          return  this.book;  
  22.     }  
  23.   
  24.     public  String   getBookName(){  
  25.         return  this.bookName;  
  26.     }  
  27.   
  28.     public void setBookName(String bookName) {  
  29.         this.bookName = bookName;  
  30.     }  
  31. }  

在Person類中 
[java]  view plain copy
 
  1. @Value("#{book}")  
  2.    private  Book  book;  
注入book到person 通過EL表達式的方式
[java]  view plain copy
 
  1. @Value("#{book.name}")  
  2.    private  String  bookName;  
同樣以上的bookName也是通過EL表達式的方式
 
測試類:
[java]  view plain copy
 
  1. package com.myapp.core.spel.annotation;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6.     public class MainTest {   
  7.         public static void main(String[] args) {  
  8.               
  9.             ApplicationContext  context  = new  ClassPathXmlApplicationContext("resource/spel.xml");  
  10.               
  11.             Person  person =  (Person)context.getBean("person");  
  12.               
  13.             System.out.println(person.getBookName());  
  14.               
  15.             System.out.println(person.getBook().getPages());  
  16.         }  
  17.     }  

測試結果:
[plain]  view plain copy
 
  1. 三月 18, 2013 5:25:23 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
  2. INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@11e831: startup date [Mon Mar 18 17:25:23 CST 2013]; root of context hierarchy  
  3. 三月 18, 2013 5:25:23 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  4. INFO: Loading XML bean definitions from class path resource [resource/spel.xml]  
  5. 三月 18, 2013 5:25:24 下午 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFilters  
  6. INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning  
  7. 三月 18, 2013 5:25:24 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>  
  8. INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring  
  9. 三月 18, 2013 5:25:24 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
  10. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@543360: defining beans [book,person,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy  
  11. Effective Java  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM