Spring_Bean的作用域
使用bean的
scope属性来配置bean的作用域
singleton:默认值
容器初始化时就已经创建bean实例(还未获取bean实例时),在整个容器的生命周期内只创建这一个bean,单例的
<bean id="car" class="com.wul.spring.beans.scope.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="300000"></property>
</bean>
prototype:
容器初始化时不创建bean实例,每次请求时都创建一个新的bean实例并返回
<bean id="car2" class="com.wul.spring.beans.scope.Car" scope="prototype">
<property name="brand" value="Audi"></property>
<property name="price" value="300000"></property>
</bean>
Spring使用外部属性文件
在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(例如:文件路径,数据源配置信息等)。
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="1230"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
而这些部署细节实际上
需要和Bean配置相分离。
Spring提供了一个 PropertyPlaceholderConfigurer的 BeanFactory后置处理器,
Spring提供了一个 PropertyPlaceholderConfigurer的 BeanFactory后置处理器,
这个处理器
允许用户将Bean配置的部分内容外移到属性文件中,可以在Bean配置文件里使用形式为
${var}的变量,
PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性来替换变量。
Spring还允许在属性文件中使用${propName},以实现属性之间的相互引用。
Spring还允许在属性文件中使用${propName},以实现属性之间的相互引用。
注册PropertyPlaceholderConfigurer
spring2.0时:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties"></property >
</bean>
spring2.5之后:可通过<context:property-placeholder>元素简化
--添加命名空间:<beans>中添加context Schema定义(xmlns:context="http://www.springframework.org/schema/context")--在配置文件下加入配置:<context:property-placeholder location="classpath:db.properties"/>
<!-- 导入属性文件 -->
<context:property-placeholder location="classpath:db_properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean>
db_properties:
user=zucc
password=zucc
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test
需额外加入相应jar包测试(
c3p0-0.9.2.1.jar
mchange-commons-java-0.2.3.4.jar
mysql-connector-java-5.1.7-bin.jar
mchange-commons-java-0.2.3.4.jar
mysql-connector-java-5.1.7-bin.jar
)注意:若spring的jar包是4.0.0的可以不需mchange-commons-java-0.2.3.4.jar。
Spring表达式语言:spEL
Spring表达式语言(简单SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言,
语法类似于EL:SpEL使用#{...}作为定界符,所有在大框号中的字符都将被认为是SpEL。
SpEL为bean的属性进行动态赋值提供了便利。
SpEL为bean的属性进行动态赋值提供了便利。
SpEL:字面量
字面量的表示:
--- 整数:<property name="count" value="#{5}"/>
--- 小数:<property name="frequency" value="#{89.7}"/>
--- 科学计数法:<property name="capacity" value="#{1e4}"/>
--- String可以使用单引号或者双引号作为字符串的定界符号:
<property name="name" value="#{"Chuck"}"/>或<property name="name" value="#{'Chuck'}"/>
--- Boolean:<property name="enabled" value="#{false}"/>
字面量的表示:
--- 整数:<property name="count" value="#{5}"/>
--- 小数:<property name="frequency" value="#{89.7}"/>
--- 科学计数法:<property name="capacity" value="#{1e4}"/>
--- String可以使用单引号或者双引号作为字符串的定界符号:
<property name="name" value="#{"Chuck"}"/>或<property name="name" value="#{'Chuck'}"/>
--- Boolean:<property name="enabled" value="#{false}"/>
<!-- 使用spel为属性赋一个字面值 -->
<bean id="address" class="com.wul.spring.beans.spel.Address">
<property name="city" value="#{'Beijing'}"></property>
<property name="street" value="WuDaoKou"></property>
</bean>
通过SpEL可以实现:
1.引用其他对象
<!-- 使用spel来应用其他的bean -->
<property name="car" value="#{car}"></property>
2.引用其他对象的属性
<!--使用spel来应用其他的bean的属性 -->
<property name="city" value="#{address.city}"></property>
3.调用其他方法,还可以链式操作
4.算数运算符:+ - * / % ^
5.加号还可以用作字符串连接
6.比较运算符:< > == <= >= lt gt eq le ge
7.逻辑运算符:and,or,not,|
8.if-else运算符:?: (temary) , ?:(Elvis)
<!-- 在spel中使用运算符 -->
<property name="info" value="#{car.price>=300000?'金领':'白领'}"></property>
9.if-else的变体
10.正则表达式:matches
11.调用静态方法或静态属性:通过T()调用一个类的静态方法,它将返回一个Class Object,然后再调用相应的方法或属性。
<!--使用spel引用类的静态属性 -->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property>
下面给出示例:
Spring_Bean的作用域
Car.java
package com.wul.spring.beans.scope;
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
public Car() {
System.out.println("Car's Constructor");
}
}
bean-scope.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--
使用bean的scope属性来配置bean的作用域,
singleton:默认值
容器初始化时就已经创建bean实例(还未获取bean实例时),
在整个容器的生命周期内只创建这一个bean,单例的
-->
<bean id="car" class="com.wul.spring.beans.scope.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="300000"></property>
</bean>
<!--
prototype:
容器初始化时不创建bean实例,
每次请求时都创建一个新的bean实例并返回
-->
<bean id="car2" class="com.wul.spring.beans.scope.Car" scope="prototype">
<property name="brand" value="Audi"></property>
<property name="price" value="300000"></property>
</bean>
</beans>
MainScope.java
package com.wul.spring.beans.scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainScope {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-scope.xml");
System.out.println("--------获取实例----------");
Car car = (Car) ctx.getBean("car");
Car car2 = (Car) ctx.getBean("car");
//结果为true,说明其默认是单例的
System.out.println(car==car2);
//多例
System.out.println("--------获取实例----------");
Car car3 = (Car) ctx.getBean("car2");
Car car4 = (Car) ctx.getBean("car2");
System.out.println(car3==car4);
}
}
Spring使用外部属性文件
db_properties
user=zucc
password=zucc
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test
bean-properties.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="1230"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
</bean> -->
<!-- 导入属性文件 -->
<context:property-placeholder location="classpath:db_properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean>
</beans>
MainProperties.java
package com.wul.spring.beans.properties;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainProperties {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-properties.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
}
Spring表达式语言:spEL
Address.javapackage com.wul.spring.beans.spel;
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
}
Car.java(同上com.wul.spring.beans.scope.Car一样)
Person.java
package com.wul.spring.beans.spel;
public class Person {
private String name;
private Car car;
//引用address bean的city属性
private String city;
//根据car的price确定info:car的price>=300000:金领
//否则为:白领
private String info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Person [name=" + name + ", car=" + car + ", city=" + city
+ ", info=" + info + "]";
}
}
beans-spel.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用spel为属性赋一个字面值 -->
<bean id="address" class="com.wul.spring.beans.spel.Address">
<property name="city" value="#{'Beijing'}"></property>
<property name="street" value="WuDaoKou"></property>
</bean>
<bean id="car" class="com.wul.spring.beans.spel.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="500000"></property>
<!--使用spel引用类的静态属性 -->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property>
</bean>
<bean id="person" class="com.wul.spring.beans.spel.Person">
<!-- 使用spel来应用其他的bean -->
<property name="car" value="#{car}"></property>
<!--使用spel来应用其他的bean的属性 -->
<property name="city" value="#{address.city}"></property>
<!-- 在spel中使用运算符 -->
<property name="info" value="#{car.price>=300000?'金领':'白领'}"></property>
<property name="name" value="wul"></property>
</bean>
</beans>
MainSpel.java
package com.wul.spring.beans.spel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainSpel {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml");
Address address = (Address) ctx.getBean("address");
System.out.println(address);
Car car = (Car) ctx.getBean("car");
System.out.println(car);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
}