Spring In Action ②

初始化和销毁Bean

init-method && destory-method

<bean id="auditorium" class="test.Audirorium" init-method="turnOnLights" destory-method="turnOffLights"/>

 

如果上下文中有很多Bean都有相同name的初始化方法和销毁方法。可以在Beans元素的default-init-method && default-destory-method方法中声明。

<beans xmlns=**********
    default-init-method="init"
    default-destroy-method="destory">
   <bean/>....

<beans>

使用<property>元素配置Bean属性,需要调用属性的setter方法来注入.

类似如下:

<property name="age" value="15"/>

命名空间p

<beans *********
    xmlns:p="http://www.springframework.org/schema/p">
....

   <p:age = "15"/>
   <p:instrument-ref="saxophone"/>

内部Bean(inner Bean) : 可以使用在setter和构造器注入两种.

<property name="instrument">
    <bean class="test.Saxophone"/>
</property>

类似:

<bean id="saxophone" class="test.Saxophone"/>
...
  <property name="instrument" ref="saxophone"/>
...

装配集合:可以装配各种集合,如java.util.List, java.util.Set, java.util.Collection, java.util.Map,java.util.Properties等等。可以设置properties和null值。

<list> 可重复

<set> 不可重复

<map>  name  value 可以是任意类型

<props> name value 必须是string类型

<property name="instruments">
    <list>
        <ref bean="guitar"/>
        <ref bean="harmonica"/>
    </list>
</property>
*****************************************
<property name="instruments">
    <list>
        <ref bean="guitar"/>
        <ref bean="guitar"/>
    </list>
</property>
*****************************************
<property name="instruments">
    <map>
        <entry key="Guitar" value-ref="guitar"/>
        <entry key="Hamonica" value-ref="harmonica"/>
    </map>
</property>
***************************************
key: entry键为string类型时
key-ref: entry键为其他Bean时
value: entry值为string类型时
value-ref: entry值为其他Bean时
***************************************
<property name="instruments">
    <props>
        <prop key="Guitar">guitar hum hum hum</prop>
        <prop key="Hamonica"><null/></prop>
    </props>
</property>

使用表达式装配

SpEL(Spring Expression Language)

字面值

<property name="count" value="#{5}"/>
<property name="count" value="#{5.98}"/>
<property name="name" value="#{‘Chuck‘}"/>
<property name="enable" value="#{true}"/>

 

引用bean

<!-- 引用bean -->
<property name="instrument" value="#{guitar}"/>
<property name="instrument" ref="guitar"/>

<!-- 会调用 kenny.getInstrument() -->
<property name="instrument" ref="#{kenny.instrument}"/>

<property name="song" ref="#{kenny.selectSong().toUpperCase()}"/>
<!-- 避免空指针异常,使用 ?. 代替 . 确保左边值不为null -->
<property name="song" ref="#{kenny.selectSong()?.toUpperCase()}"/>

 

调用类的方法: T()运算符

<...    value="#{T(java.lang.Math).PI}"/>

 

使用SpEL进行数值运算

<...  value="#{counter.total + 42}"/>
<...  value="#{T(java.lang.Math).PI * circle.radius^2}"/>

<!-- 比较值 -->
<...  value="#{ circle.radius le 21}"/>
<!-- le less or equal -->
<!--eq   equal -->
<!--lt less -->
<!--gt great -->
<!-- ge great or equal -->
<!-- 逻辑表达式 and or not -->
<!-- 条件表达式 ?:-->
<!-- 正则表达式 -->
<...  value="#{admin.email matches ‘[a-zA-Z0-9._%+-][email protected][a-zA-Z0-9.-]+\\.com‘ ? "right email" : "wrong email"}"

 

使用SpEL筛选集合

public class City{
    private String name;
    private String state;
    private Integer population;
}

 

<util:list id="cities">
    <bean class="test.City" p:name="Chicago" p:state"IL" p:population="232322"/>
    <bean class="test.City" p:name="Houston" p:state"TX" p:population="235452"/>
</util:list>
<!-- 访问集合 -->
<... value="#{cities[1]}"/>

 

加载properties配置文件

<util:properties id="settings" location="classpath:settings.properties"/>

<... value="#{settings[‘twitter.accessToken‘]}"/>

 

systemEnvironment 机器上的所有环境变量

systemProperties 包含了Java启动时所设置的所有属性(通常用-D参数)

<... name="homePath" value="#{systemEnvironment[‘HOME‘]}"/>
<... name="homePath" value="#{systemProperties[‘application.home‘]}"

 

SpEL查询集合

查询运算符  .?[](所有匹配项)   .^[] (第一个匹配项)  .$[] 最后一个匹配项

eg  cities.?[population gt 10000]  ---  集合

投影运算符  .![]

eg cities.![name]   只包含城市名的string类型集合

     cities.![name + ‘,’+ state]  包含城市名和地区的String类型集合

时间: 2024-08-01 04:43:25

Spring In Action ②的相关文章

Spring实战-Spring in Action, 4th Edition-2015年第4版本

In Action系列中最畅销的Spring图书,近十万读者学习Spring的共同选择!In Action系列中最畅销的Spring图书,有近10万读者选择本书来学习Spring! Spring框架已经成为Java开发人员的必备知识,而且Spring 3引入了强大的新特性,例如SpEL.Spring表达式语言.IoC容器的新注解以及用户急需的对REST的支持.无论你是刚刚接触Spring还是被Spring 3.0的新特性所吸引,本书都是掌握Spring的最佳选择. 下载地址: Spring in

spring in action 4 6.3使用Apache Tiles

配置Tiles视图解析器 需要一个TilesConfigurer bean,该bean用于定位和加载tile定义,还需要一个TilesViewResolver用于将逻辑视图名与tile定义映射起来.在WebConfig.java中 @Configuration @EnableWebMvc @ComponentScan(basePackageClasses={AutoScan.class}) public class WebConfig extends WebMvcConfigurerAdapte

spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化

这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are property names wrapped with ${...},as an exampl,you can resolve the constructor arguments for a BlankDisc in xml like this : <bean id="sgtPeppers&qu

spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入

一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) set 注入.这篇随笔讲的是第一种构造方法注入(Constructor Injection). 其实DI(Dependency Injection)依赖注入你不妨反过来读:注入依赖也就是把"依赖"注入到一个对象中去.那么何为"依赖"呢?依赖就是讲一个对象初始化或者将实例

spring in action 4th --- quick start

读spring in action. 不是特别聪明的人,只好多练习,多实践.读书和思考很容易变得混乱,因为没有实践.因此,虽然花了很多时间,还是要动手记录下来. 环境搭建 quick-start 1.环境搭建 jdk1.8 gradle 2.12 Intelij idea 2016.2.1 1.1创建一个gradle项目 在idea中,new -> project -> gradle 创建一个空项目.创建成功后修改build.gradle : group 'com.test' version

spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。

在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的上下文.ContextLoadListener是加载 其他组件的上下文. 第一种方式:纯注解的方式: 在spring4.0版本以上,倾向用注解的方式配置DispatcherServlet和ContextLoaderListener.配置方式如下: 思路:一个类只要继承AbstractAnnotati

spring中action和url的对应关系

spring 中, action和url的对应关系 在web.xml中,这样配置: <servlet-mapping > <servlet-name> spring</ servlet-name> <url-pattern> *.html</url-pattern > </servlet-mapping > 由此,我们可以看到,要进入到LoginController,我们需要在浏览器中输入:localhost:8080/springM

SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程

一. 1. 2.pizza-flow.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <flow xmlns="http://www.springframework.org/schema/webflow" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h

spring in action 4 第6章 视图分发

spring 4.0 原生支持13种视图解析器 从spring3.2版本开始,spring支持上述13种解析器,但是spring 3.1不支持Tiles 3 TilesViewResolver,其它的12种都支持. 视图解析器所在的配置文件 @Configuration @EnableWebMvc @ComponentScan("_5BuildingSpringwebapplications") public class WebConfig extends WebMvcConfigur

Spring in action(Spring实战) 第四版翻译

第一部分 Spring核心 Spring提供了很多功能,但是所有这些功能的基础是是依赖注入(DI)和面向方面编程(AOP). 第一章 Springing into action 本章包括: Spring的bean容器 探索Spring的核心模块 强大的Spring生态系统 Spring的新特性 现在是java程序员的好时代.在长达20年的发展过程中,java经历了一些好时光,也经历了一些坏时光.尽管有一些粗糙的地方,例如applet,Enterprise javabean(EJB),Java数据