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  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值