spring表达式SpEL

  • 语法#{..},为bean的属性进行动态赋值
  • 通过bean的id对bean进行引用
  • 调用方法以及引用对象中的属性
  • 计算表达式的值
  • 正则表达式的匹配

修改Address.java、Car.java、Person.java等文件

详情请看starryfei GitHub.

spring的SpEL的xml配置文件

<!-- 使用SpEL表达式#{}进行赋值等同于value="北京" -->
<bean id="address" class="com.test.SpEL.Address">
<property name="city" value="#{'北京'}"></property>
<property name="street" value="#{'长安街'}"></property>
</bean>
<!--使用SpEL引用类的静态属性 -->
<bean id="car" class="com.test.SpEL.Car">
<property name="name" value="#{'现代'}"></property>
<property name="price" value="#{10000}"></property>
<!-- 通过T()调用一个类的静态方法,它将返回一个ClassObject,然后调用相对应的方法 或属性 -->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*60}"></property>
</bean>
<!--使用SpEL引用其他的bean -->
<bean id="person" class="com.test.SpEL.Person">
<property name="name" value="#{'张三'}"></property>
<!--使用SpEL引用其他的bean ,等同于ref="car"-->
<property name="car" value="#{car}"></property>
<!--使用SpEL引用其他的bean的属性 -->
<property name="city" value="#{address.city}"></property>
<!-- SpEL使用运算符if.. -->
<property name="info" value="#{car.price >= 20000 ? '金领':'白领'}"></property>
</bean>

运行结果

constructor create....
Address [city=北京, street=长安街]
Car [name=现代, price=10000.0, tyrePerimeter=188.49555921538757]
Person [name=张三, car=Car [name=现代, price=10000.0, tyrePerimeter=188.49555921538757], city=北京, info=白领]

spring管理Bean的生命周期

spring IOC容器可以管理Bean的生命周期管理其过程为:

  • 通过构造器或工厂方法创建Bean的实例;
  • 为Bean的属性设置值和对其他Bean的引用;
  • 调用Bean的初始化方法。初始化:在Bean的声明里设置init-method属性;
  • Bean可以使用;
  • 当容器关闭时,调用Bean的销毁方法。销毁方法:在Bean的声明里设置

    destroy-method属性。

新建User.java

package com.test.cycle;

public class User {

	public User() {
System.out.println("User's Constructor...");
} private String name; public void setName(String name) {
System.out.println("setName..");
this.name = name;
}
//自定义初始化方法
private void init() {
// TODO Auto-generated method stub
System.out.println("init mothod..");
}
//自定义销毁方法
private void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy method... ");
} @Override
public String toString() {
return "User [name=" + name + "]";
}
}

配置spring管理Bean的生命周期的xml文件

<!--init-method="初始化方法 destroy-method="销毁方法"" -->
<bean id="user" class="com.test.cycle.User" init-method="init"
destroy-method="destroy">
<property name="name" value="starryfei"></property>
</bean>

测试方法主要代码

public static void main(String[] args) {
//ClassPathXmlApplicationContext
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"cycle.xml");
User user = (User) ctx.getBean("user");
System.out.println(user);
// 关闭IOC容器
ctx.close();
}

运行结果

User's Constructor...
setName..
init mothod..
User [name=starryfei]
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@c4437c4: startup date [Mon Jan 23 00:21:08 CST 2017]; root of context hierarchy
destroy method...

创建Bean的后置处理器-BeanPostProcessor接口实现

spring IOC容器可以管理Bean的生命周期管理其过程为:

  • 通过构造器或工厂方法创建Bean的实例;
  • 为Bean的属性设置值和对其他Bean的引用;
  • 将Bean的实例传递给Bean的后置处理器的postProcessBeforeInitialization方法;
  • 调用Bean的初始化方法;
  • 将Bean的实例传递给Bean的后置处理器的postProcessAfterInitialization方法;
  • Bean可以使用;
  • 当容器关闭时,调用Bean的销毁方法。

实现BeanPostProcessor接口的java文件

public class MyBeanPostProcessor implements BeanPostProcessor {

	@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException { // 添加过滤
if ("user".equals(beanName)) {
System.out.println("postProcessBeforeInitialization: " + bean
+ " , " + beanName);
}
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization: " + bean + " , "
+ beanName);
// 在方法中修改返回的bean
User user = new User();
user.setName("qin");
return user;
} }

**-配置Bean的后置处理器xml文件 **


<!-- 实现BeanPostProcessor接口,并具体提供以下两个方法: Object postProcessBeforeInitialization(Object
bean, String beanName):init-method之前被调用 Object postProcessAfterInitialization(Object
bean, String beanName):init-method之后被调用 bean:bean实例本身 beanName:IOC 容器配置的bean的名字
返回值:实际上返回给用户的那个Bean,注意:可以在两个方法中修改返回值bean,也可以返回一个新的bean --> <!--配置Bean的后置处理器 不需要指定id,IOC容器可以自动识别 -->
<bean class="com.test.cycle.MyBeanPostProcessor"></bean>

测试方法同上

运行结果

User's Constructor...
setName..
postProcessBeforeInitialization: User [name=starryfei] , user
init mothod..
postProcessAfterInitialization: User [name=starryfei] , user
User's Constructor...
setName..
User [name=qin]
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@c4437c4: startup date [Mon Jan 23 00:32:59 CST 2017]; root of context hierarchy
destroy method...

spring学习-5的更多相关文章

  1. spring 学习之 bean 的注入方式 property和constructor-arg的使用方式

    spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...

  2. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  3. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  4. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  5. Spring学习 Ioc篇(一 )

    一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...

  6. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  7. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  8. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  9. Spring学习8-Spring事务管理

      http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html   Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...

  10. Spring学习之Ioc控制反转(1)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

随机推荐

  1. start() vs. run()

          I'm reading a Blog. But a rather familiar question occurred to me, "What's the difference ...

  2. 异常:System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)

    异常:System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid) 原因:该引用所需.NET Framework版本为4.5 ...

  3. 31 整数中1出现的次数(从1到n整数中1出现的次数)

    题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...

  4. flex label 换行

    Flex中label换行有两种情况 在AS中赋值: label.text="Online\r\nResources" 在mxml中赋值: text="Online Res ...

  5. JAVA 文件转字节数组转字符串

    public static void main(String[] args) throws IOException { byte[] bytes = FileUtils.readFileToByteA ...

  6. 添加code到github上

    第一步:github上新建远程仓库 1. 在 https://github.com/  注册账号 2. new 一个新仓库 (1) 点击加号下的`New repository` (2)在Reposit ...

  7. valn 配置

    内核修改: /device drivers/Network device support/MAC-VLAN support 1.创建目录和文件#cd /usr#mkdir vlan#cd vlan#c ...

  8. UVA11297 Census

    题目 UVA11297 Census 做法 二维线段树,单点修改,矩阵查询,树套树(\(x,y\)),维护最大值最小值废话 有一点要注意的是:\(x\)树传到\(y\)树里面修改的时候,如果\(x\) ...

  9. 使用awk来提取内容

    1.提取gff文件中的HLA基因的相关bed文件. gff的格式: zcat *gz|gawk 'BGIN{FS="\t";OFS="\t"}$3==" ...

  10. Cocos2d-x项目移植到WP8系列之六:C#工程使用C++的DLL

    原文链接: http://www.cnblogs.com/zouzf/p/3984510.html 此时,一些大问题都被解决后,整个工程基本能跑起来了,最后一个大问题是:业务层是用Lua开发的,底层的 ...