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

Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式的更多相关文章

  1. Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知

    本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知     1. Spring AOP  前置通知 XML配置使用案例     2. Spring AOP   ...

  2. Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)

    Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...

  3. JavaWeb_(Spring框架)xml配置文件

    系列博文 JavaWeb_(Spring框架)xml配置文件  传送门 JavaWeb_(Spring框架)注解配置 传送门 Xml配置 a)Bean元素:交由Spring管理的对象都要配置在bean ...

  4. Spring根据XML配置文件注入对象类型属性

    这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class Da ...

  5. spring读取xml配置文件(二)

    一.当spring解析完配置文件名的占位符后,就开始refresh容器 @Override public void refresh() throws BeansException, IllegalSt ...

  6. spring 基于xml的申明式AspectH中的后置通知的返回值获取

    spring 基于xml的申明式AspectH中的后置通知的返回值获取 1. 配置文件 <aop:config> <aop:aspect ref="myAspect&quo ...

  7. 如何配置多个Spring的xml配置文件(多模块配置)

    如何使用多个Spring的xml配置文件(多模块配置) (2009-08-22 13:42:43)   如何使用多个Spring的xml配置文件(多模块配置) 在用Struts Spring Hibe ...

  8. [error] eclipse编写spring等xml配置文件时只有部分提示,tx无提示

    eclipse编写spring等xml配置文件时只有<bean>.<context>等有提示,其他标签都没有提示 这时就需要做以下两步操作(下面以事务管理标签为例) 1,添加命 ...

  9. JS中如何使用EL表达式中的对象

    JS中如何使用EL表达式中的对象 2017年09月25日 15:33:09 lhpnba 阅读数:4859   1.js中使用el表达式要加双引号或单引号:'${list}' 2.js变量获取el表达 ...

随机推荐

  1. 如何判断是否为同一个App,Ionic3如何修改包名

    如何判断是否同一个App 使用Ionic3创建了两个项目demo1.demo2,然后使用同一个JDK,生成了两个不同的keystore证书. 结果在手机端安装的时候,先安装demo1,没有任何替换的提 ...

  2. 「HDU - 2857」Mirror and Light(点关于直线的对称点)

    题目链接 Mirror and Light 题意 一条直线代表镜子,一个入射光线上的点,一个反射光线上的点,求反射点.(都在一个二维平面内) 题解 找出入射光线关于镜子直线的对称点,然后和反射光线连边 ...

  3. SCOI2016 Day1 简要题解

    目录 「SCOI2016」背单词 题意 题解 代码 「SCOI2016」幸运数字 题意 题解 总结 代码 「SCOI2016」萌萌哒 题意 题解 总结 代码 「SCOI2016」背单词 题意 这出题人 ...

  4. sscanf、sprintf、stringstream常见用法

    转载自:https://blog.csdn.net/jllongbell/article/details/79092891 前言: 以前没有接触过stringstream这个类的时候,常用的字符串和数 ...

  5. XML学习入门

    很久没更新博客了.来一发! 关于XML,当时没学过感觉难吧?其实XML还是蛮好学的,这里留下一些入门级别的内容. XML中dtd(文档类型定义)的主要用处是约束xml,也可以去约束html,xhtml ...

  6. git init github

    Command line instructions 执行这些命令是在windows 右菜单里面的git bash运行. Git global setup git config --global use ...

  7. js 刷新

    方法一: location.reload 重新加载 location.reload(); 如果该方法没有规定参数,或者参数是 false,它就会用 HTTP 头 If-Modified-Since 来 ...

  8. OpenLayers学习笔记(二)— QML与HTML通信之画图

    作者: 狐狸家的鱼 Github: 八至 本文链接:QML与 HTML通信—实现QML中点击功能按钮在地图上画图 一.HTML-map var drarGraphic; var drawType;fu ...

  9. A1014. Waiting in Line

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  10. (三)orcale体系结构

    https://www.cnblogs.com/chengxiao/p/5904783.html 一.什么是Oracle数据库? 众所周知,Oracle DataBase是一款关系型数据库管理系统(不 ...