spring学习-5
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的更多相关文章
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- Spring学习之第一个AOP程序
IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...
- MyEclipse Spring 学习总结三 SpringMVC
MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...
- Spring学习 Ioc篇(一 )
一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- Spring学习8-Spring事务管理
http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
随机推荐
- python学习之路-第一天-接触python
我的入门就决定用<简明Python教程> <简明Python教程> 1. python的优势 简单:专注于解决问题而不是关注语言本身 易学:容易上手 开源.免费 可移植性非常强 ...
- SecureCRT卡死的问题
ctrl+s是一个古老的shell控制键,不小心按倒就卡死了.在输入ctrl+q就可以恢复了
- English Phrases
@1:Phrases requst sth from/of sb 向某人要求某物 a new lease on life 重获新生.焕发生机 state of the art 最先进的 at th ...
- Windows.old
如果通过执行自定义安装来安装 Windows 7,而没有在安装过程中格式化分区,则以前版本的 Windows中使用的文件存储在 Windows.old文件夹中.此文件夹中文件的类型取决于您的电脑.使用 ...
- active scaffold
模板文件路径:/.rvm/gems/ruby-2.2.3/gems/active_scaffold-3.5.3/app/views/active_scaffold_overrides
- 模块调用,datetime,time,logging,递归,双层装饰器, json,pickle迭代器和生成器
一.python模块(导入,内置,自定义,开源) 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用python ...
- 31 整数中1出现的次数(从1到n整数中1出现的次数)
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- predis操作大全
predis是php连接redis的操作库,由于它完全使用php编写,大量使用命名空间以及闭包等功能,只支持php5.3以上版本,故实测性能一般,每秒25000次读写,相信改换c语言编写的php扩展后 ...
- ACM训练小结-2018年6月16日
今天题目情况如下:A题:线段树+XOR性质.情况:由于写法问题,调试困难,浪费大量时间.B题:(对所有满足i mod p==q,求a[i]之和),无修改,直接上n*sqrt(n)的分块写法.情况:由于 ...
- c++ boost库学习一:时间和日期
timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmai ...