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

时间:2023-03-10 05:26:00
Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式

Spring  EL

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

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

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

  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. <!-- more bean definitions for data access objects go here -->
  10. <bean id="book" class="com.myapp.core.spel.xml.Book">
  11. <property name="name" value="Effective Java" />
  12. <property name="pages" value="300"/>
  13. </bean>
  14. <bean id="person" class="com.myapp.core.spel.xml.Person">
  15. <property name="book" value="#{book}" />
  16. <property name="bookName" value="#{book.name}"/>
  17. </bean>
  18. </beans>

在person  bean 的配置中, 属性 book 引用了  book bean 通过EL表达式 形式是:<property name="book" value="#{book}" /> 相当于 在person bean中注入 book

person属性中的bookName属性注入了 book bean中的 name的值  

2:测试以上配置:

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

Person类:

  1. package com.myapp.core.spel.xml;
  2. public class Person {
  3. private  Book  book;
  4. private  String  bookName;
  5. public void setBook(Book book) {
  6. this.book = book;
  7. }
  8. public Book getBook(){
  9. return  this.book;
  10. }
  11. public  String   getBookName(){
  12. return  this.bookName;
  13. }
  14. public void setBookName(String bookName) {
  15. this.bookName = bookName;
  16. }
  17. }

测试类:

  1. package com.myapp.core.spel.xml;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainTest {
  5. public static void main(String[] args) {
  6. ApplicationContext  context  = new  ClassPathXmlApplicationContext("resource/spel.xml");
  7. Person  person =  (Person)context.getBean("person");
  8. System.out.println(person.getBookName());
  9. System.out.println(person.getBook().getPages());
  10. }
  11. }

输出结果:

  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中配置,扫描含有注解的包;

  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. <!-- more bean definitions for data access objects go here -->
  10. <context:component-scan base-package="com.myapp.core.spel.annotation" />
  11. </beans>

2:相应的类:

Book类:
  1. package com.myapp.core.spel.annotation;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. @Component("book")
  5. public class Book {
  6. @Value("Effective Java")
  7. private  String  name ;
  8. @Value("300")
  9. private  int   pages;
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public int getPages() {
  17. return pages;
  18. }
  19. public void setPages(int pages) {
  20. this.pages = pages;
  21. }
  22. }

在book的属性中 注入了值。

Person类:
  1. package com.myapp.core.spel.annotation;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. @Component("person")
  5. public class Person {
  6. @Value("#{book}")
  7. private  Book  book;
  8. @Value("#{book.name}")
  9. private  String  bookName;
  10. public void setBook(Book book) {
  11. this.book = book;
  12. }
  13. public Book getBook(){
  14. return  this.book;
  15. }
  16. public  String   getBookName(){
  17. return  this.bookName;
  18. }
  19. public void setBookName(String bookName) {
  20. this.bookName = bookName;
  21. }
  22. }

在Person类中 

  1. @Value("#{book}")
  2. private  Book  book;

注入book到person 通过EL表达式的方式

  1. @Value("#{book.name}")
  2. private  String  bookName;

同样以上的bookName也是通过EL表达式的方式

测试类:
  1. package com.myapp.core.spel.annotation;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainTest {
  5. public static void main(String[] args) {
  6. ApplicationContext  context  = new  ClassPathXmlApplicationContext("resource/spel.xml");
  7. Person  person =  (Person)context.getBean("person");
  8. System.out.println(person.getBookName());
  9. System.out.println(person.getBook().getPages());
  10. }
  11. }

测试结果:

  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