Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式
Spring EL
一:在Spring xml 配置文件中运用 Spring EL
1:运用EL表达式的配置文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- more bean definitions for data access objects go here -->
- <bean id="book" class="com.myapp.core.spel.xml.Book">
- <property name="name" value="Effective Java" />
- <property name="pages" value="300"/>
- </bean>
- <bean id="person" class="com.myapp.core.spel.xml.Person">
- <property name="book" value="#{book}" />
- <property name="bookName" value="#{book.name}"/>
- </bean>
- </beans>
在person bean 的配置中, 属性 book 引用了 book bean 通过EL表达式 形式是:<property name="book" value="#{book}" /> 相当于 在person bean中注入 book
2:测试以上配置:
- package com.myapp.core.spel.xml;
- public class Book {
- private String name ;
- private int pages;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getPages() {
- return pages;
- }
- public void setPages(int pages) {
- this.pages = pages;
- }
- }
Person类:
- package com.myapp.core.spel.xml;
- public class Person {
- private Book book;
- private String bookName;
- public void setBook(Book book) {
- this.book = book;
- }
- public Book getBook(){
- return this.book;
- }
- public String getBookName(){
- return this.bookName;
- }
- public void setBookName(String bookName) {
- this.bookName = bookName;
- }
- }
测试类:
- package com.myapp.core.spel.xml;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("resource/spel.xml");
- Person person = (Person)context.getBean("person");
- System.out.println(person.getBookName());
- System.out.println(person.getBook().getPages());
- }
- }
输出结果:
- 三月 18, 2013 5:17:18 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
- INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@11e831: startup date [Mon Mar 18 17:17:18 CST 2013]; root of context hierarchy
- 三月 18, 2013 5:17:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
- INFO: Loading XML bean definitions from class path resource [resource/spel.xml]
- 三月 18, 2013 5:17:18 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
- INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@498b39: defining beans [book,person]; root of factory hierarchy
- Effective Java
- 300
二:注解中使用 EL
1:xml中配置,扫描含有注解的包;
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- more bean definitions for data access objects go here -->
- <context:component-scan base-package="com.myapp.core.spel.annotation" />
- </beans>
2:相应的类:
- package com.myapp.core.spel.annotation;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- @Component("book")
- public class Book {
- @Value("Effective Java")
- private String name ;
- @Value("300")
- private int pages;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getPages() {
- return pages;
- }
- public void setPages(int pages) {
- this.pages = pages;
- }
- }
在book的属性中 注入了值。
- package com.myapp.core.spel.annotation;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- @Component("person")
- public class Person {
- @Value("#{book}")
- private Book book;
- @Value("#{book.name}")
- private String bookName;
- public void setBook(Book book) {
- this.book = book;
- }
- public Book getBook(){
- return this.book;
- }
- public String getBookName(){
- return this.bookName;
- }
- public void setBookName(String bookName) {
- this.bookName = bookName;
- }
- }
在Person类中
- @Value("#{book}")
- private Book book;
注入book到person 通过EL表达式的方式
- @Value("#{book.name}")
- private String bookName;
同样以上的bookName也是通过EL表达式的方式
- package com.myapp.core.spel.annotation;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("resource/spel.xml");
- Person person = (Person)context.getBean("person");
- System.out.println(person.getBookName());
- System.out.println(person.getBook().getPages());
- }
- }
测试结果:
- 三月 18, 2013 5:25:23 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
- INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@11e831: startup date [Mon Mar 18 17:25:23 CST 2013]; root of context hierarchy
- 三月 18, 2013 5:25:23 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
- INFO: Loading XML bean definitions from class path resource [resource/spel.xml]
- 三月 18, 2013 5:25:24 下午 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFilters
- INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning
- 三月 18, 2013 5:25:24 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
- INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
- 三月 18, 2013 5:25:24 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
- 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
- Effective Java
Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式的更多相关文章
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...
- JavaWeb_(Spring框架)xml配置文件
系列博文 JavaWeb_(Spring框架)xml配置文件 传送门 JavaWeb_(Spring框架)注解配置 传送门 Xml配置 a)Bean元素:交由Spring管理的对象都要配置在bean ...
- Spring根据XML配置文件注入对象类型属性
这里有dao.service和Servlet三个地方 通过配过文件xml生成对象,并注入对象类型的属性,降低耦合 dao文件代码: package com.swift; public class Da ...
- spring读取xml配置文件(二)
一.当spring解析完配置文件名的占位符后,就开始refresh容器 @Override public void refresh() throws BeansException, IllegalSt ...
- spring 基于xml的申明式AspectH中的后置通知的返回值获取
spring 基于xml的申明式AspectH中的后置通知的返回值获取 1. 配置文件 <aop:config> <aop:aspect ref="myAspect&quo ...
- 如何配置多个Spring的xml配置文件(多模块配置)
如何使用多个Spring的xml配置文件(多模块配置) (2009-08-22 13:42:43) 如何使用多个Spring的xml配置文件(多模块配置) 在用Struts Spring Hibe ...
- [error] eclipse编写spring等xml配置文件时只有部分提示,tx无提示
eclipse编写spring等xml配置文件时只有<bean>.<context>等有提示,其他标签都没有提示 这时就需要做以下两步操作(下面以事务管理标签为例) 1,添加命 ...
- JS中如何使用EL表达式中的对象
JS中如何使用EL表达式中的对象 2017年09月25日 15:33:09 lhpnba 阅读数:4859 1.js中使用el表达式要加双引号或单引号:'${list}' 2.js变量获取el表达 ...
随机推荐
- 洛谷P1063能量项链题解
$题目$ 不得不说,最近我特别爱刷这种区间DP题,因为这个跟其他的DP有些不一样的地方,主要是有一定的套路,就是通过小区间的状态更新大区间,从而得到原题给定区间的最优解. $但是$ 这个题应该跟$石子 ...
- WEB请求处理
https://blog.csdn.net/bpingchang/article/details/51328941
- 【XSY2469】graph 分治 并查集
题目大意 给你一张\(n\)个点\(m\)条边的无向图,问删去每个点后,原图是不是二分图. \(n,m\leq 100000\) 题解 一个图是二分图\(\Longleftrightarrow\)该图 ...
- vscode跳转到函数定义处
需要安装对应语言的插件,帮助-欢迎使用,安装javascript, php php还需要安装php7, 到官网https://windows.php.net/download#php-7.2 下载解压 ...
- Hdoj 2108.Shape of HDU 题解
Problem Description 话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,"徐队"的称呼逐渐被"徐总"所取代,海东 ...
- OpenDCIM-19.01操作手册
OpenDCIM-19.01操作手册 1. 界面标签解析 1.1 用户管理 用户管理 部门管理 用户管理被存在数据表fac_User中,包含以下字段: UserID:是管理员还是用户 Name:报表 ...
- HDU 6319 Problem A. Ascending Rating(单调队列)
要求一个区间内的最大值和每次数过去最大值更新的次数,然后求每次的这个值异或 i 的总和. 这个序列一共有n个数,前k个直接给出来,从k+1到n个数用公式计算出来. 因为要最大值,所以就要用到单调队列, ...
- Hello Object Oriented!
继计组之后,北航计算机学院又一大神课! 希望能以此为契机,和更多热爱技术的朋友们交流.让我们一起,共同进步~ [2019.4.27更新] 建立博客园的最初目的,是为了北航计算机学院OO课程设计的需要. ...
- java 内存调试 mat
https://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-ma/ http://www.open-open.com/lib/view/ope ...
- CSS修改滚动条样式
<div class="qq_bottom">超出部分变滚动条</div> /*//滚动条整体部分*/ .qq_bottom::-webkit-scroll ...