Spring 在 xml配置文件或annotation注解中运用Spring EL
来源:互联网 发布:大学生java培训课程 编辑:程序博客网 时间:2024/06/05 17:26
Spring EL
一:在Spring xml 配置文件中运用 Spring EL
Spring EL 采用 #{Sp Expression Language} 即 #{spring表达式}
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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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
person属性中的bookName属性注入了 book bean中的 name的值
2:测试以上配置:
Book类:
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 prepareRefreshINFO: 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 loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [resource/spel.xml]三月 18, 2013 5:17:18 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@498b39: defining beans [book,person]; root of factory hierarchyEffective Java300
二:注解中使用 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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:相应的类:
Book类: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的属性中 注入了值。Person类: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;}}
测试类: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 prepareRefreshINFO: 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 loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [resource/spel.xml]三月 18, 2013 5:25:24 下午 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFiltersINFO: 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 preInstantiateSingletonsINFO: 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 hierarchyEffective Java300
一:在Spring xml 配置文件中运用 Spring EL
Spring EL 采用 #{Sp Expression Language} 即 #{spring表达式}
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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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
person属性中的bookName属性注入了 book bean中的 name的值
2:测试以上配置:
Book类:
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 prepareRefreshINFO: 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 loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [resource/spel.xml]三月 18, 2013 5:17:18 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@498b39: defining beans [book,person]; root of factory hierarchyEffective Java300
二:注解中使用 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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:相应的类:
Book类:
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的属性中 注入了值。
Person类:
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;}}
测试类:
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 prepareRefreshINFO: 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 loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [resource/spel.xml]三月 18, 2013 5:25:24 下午 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFiltersINFO: 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 preInstantiateSingletonsINFO: 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 hierarchyEffective Java300
0 0
- Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL
- Spring 在 xml配置文件或annotation注解中运用Spring EL
- Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL
- Spring中使用注解代替xml配置文件
- Spring中加载 .properties 文件在java注解中使用及xml的el表达式使用
- Spring中基于配置XML与Annotation注解配置AOP
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- spring中annotation的常用注解
- Spring配置文件中<context:annotation-config/>
- 注解:配置实体类,并在spring配置文件中配置
- spring annotation 注解 简记
- Spring Annotation(注解) 详解
- Spring Annotation(注解) 详解
- Spring Annotation 注解
- Spring Annotation(注解) @Resource
- Spring Annotation(注解) @Component
- 【framework】spring-注解(annotation)
- Spring注解Annotation
- pl sql 连接oracleTNS-12541: TNS: 无监听程序(缺少tnsnames.ora和listener.ora文件)
- Freemarker_内置函数_数字、字符串、日期格式化
- Git索引
- Binder机制2---Binder的数据结构以及Binder驱动
- poj 2349 Arctic Network (prim算法)
- Spring 在 xml配置文件或annotation注解中运用Spring EL
- Test
- 同步锁--sleep,wait,lock,synchronized
- SyntaxError: Non-ASCII character '\xe7' in file解决方法
- Android性能优化之布局优化
- jQuery 核心函数以及jQuery对象
- android 开源项目收集
- Binder机制1---Binder原理介绍
- 【HDU】1317 XYZZY spfa判负环+floyd求传递闭包