zoukankan      html  css  js  c++  java
  • Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式

    Spring  EL

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

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

    1:运用EL表达式的配置文件如下:

    [html] view plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    1. @Value("#{book}")  
    2.    private  Book  book;  
    注入book到person 通过EL表达式的方式
    [java] view plaincopy
     
    1. @Value("#{book.name}")  
    2.    private  String  bookName;  
    同样以上的bookName也是通过EL表达式的方式
     
    测试类:
    [java] view plaincopy
     
    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 plaincopy
     
    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  
  • 相关阅读:
    Python学习32天(socket、tcp协议)
    Python学习第31天(异常、异常捕捉)
    Python之旅的第30天(过程记录,选课系统的基本实现)
    Python之旅的第29天(property补充、元类和自定义元类)
    Python之旅的第28天(描述符、类的装饰器)
    Python之旅的第27天(复习、习题实现、__enter__、__exit__)
    Python之旅第26天(__slots__等内置方法、软件开发规范)
    假期第二周
    假期第一周
    第十六周学习进度博客
  • 原文地址:https://www.cnblogs.com/tankaixiong/p/3508360.html
Copyright © 2011-2022 走看看