Spring 在 xml配置文件 或 annotation 注解中 运用Spring EL表达式
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/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
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 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:相应的类:
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;
- }
- }
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;
- }
- }
在Person类中
- @Value("#{book}")
- private Book book;
- @Value("#{book.name}")
- private String 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 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