13.笔记JAVA Spring框架学习————Bean使用SpEL

来源:互联网 发布:喜欢安静的男生知乎 编辑:程序博客网 时间:2024/06/10 12:05

13.笔记JAVA Spring框架学习————Bean使用SpEL

Spring Expression Language. Spring的表达式语言。

l  Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。

l  语法类似于 EL:SpEL 使用 #{}作为定界符,所有在大框号中的字符都将被认为是 SpEL

l  SpEL 为 bean 的属性进行动态赋值提供了便利

l  通过 SpEL 可以实现:

Ø  通过 bean 的 id 对 bean 进行引用

Ø  调用方法以及引用对象中的属性

Ø  计算表达式的值

Ø  正则表达式的匹配

字面量的表示:

n  整数:<propertyname="count" value="#{5}"/>

n  小数:<propertyname="frequency" value="#{89.7}"/>

n  科学计数法:<propertyname="capacity" value="#{1e4}"/>

String可以使用单引号或者双引号作为字符串的定界符号:<propertyname=“name” value="#{'Chuck'}"/> 或 <propertyname='name' value='#{"Chuck"}'/>

n  Boolean:<property name="enabled" value="#{false}"/>

SpEL支持的运算符号

测试

在app.xml文件中配置如下:

      <!--测试 SpEL:可以为属性进行动态的赋值(了解)-->

      <beanid="girl"class="User">

            <propertyname="userName"value="周迅"></property>

      </bean>

     

      <beanid="boy"class="User">

            <propertyname="userName"value="高胜远"></property>

            <propertyname="wifeName"value="#{girl.userName}"></property>

      </bean>

Main.java如下:

import java.sql.SQLException;

importjavax.sql.DataSource;

import org.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

importcom.mchange.v2.c3p0.DataSources;

 

publicclass Main {

     

      publicstaticvoid main(String[]args)throws SQLException {

            //1.创建Spring IOC容器

            ApplicationContextapx =newClassPathXmlApplicationContext("app.xml");

            User user = (User)apx.getBean("girl");

            System.out.println(user);

            User user2 = (User)apx.getBean("boy");

            System.out.println(user2);

      }

     

}

测试执行如下,实现了使用SpEL来赋字面值。

User'sConstrutor...

User'sConstrutor...

setWifhName:周迅

User[userName=周迅, cars=null]

User[userName=高胜远, cars=null]

其中User.java和之前例子中的User.java

蛤蟆此处在复制一下如下:

import java.util.List;

import java.util.Map;

 

public class User {

 

                  privateString userName;

                  privateList<Car> cars;

                 

                  privateString wifeName;

                 

                  publicString getWifeName() {

                                    returnwifeName;

                  }

 

                  publicvoid setWifeName(String wifeName) {

                                    System.out.println("setWifhName:" + wifeName);

                                    this.wifeName= wifeName;

                  }

 

                  publicString getUserName() {

                                    returnuserName;

                  }

 

                  publicvoid setUserName(String userName) {

                                    this.userName= userName;

                  }

 

                  publicList<Car> getCars() {

                                    returncars;

                  }

 

                  publicvoid setCars(List<Car> cars) {

                                    this.cars= cars;

                  }

                 

                  publicUser() {

                                    System.out.println("User'sConstrutor...");

                  }

 

                  @Override

                  publicString toString() {

                                    return"User [userName=" + userName + ", cars=" + cars +"]";

                  }

                 

                  publicvoid init(){

                                    System.out.println("initmethod...");

                  }

                 

                  publicvoid destroy(){

                                    System.out.println("destroymethod...");

                  }

 

}

 

 

 

 

 

阅读全文
0 0
原创粉丝点击