使用外部属性文件配置Bean以及Bean的生命周期方法

1.使用外部属性文件配置Bean

在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离。Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 变量,Spring 2.5 之后,可通过 <context:property-placeholder> 元素简化。

示例:在外部属性文件中进行数据库的连接配置。

配置文件beans-properties.xml中进行配置:

<!--导入属性文件-->
<context:property-placeholder location="classpath:db.properties"/> <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--${var} var要与外部属性文件中的key一致--> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> </bean>

外部属性文件db.properties文件内容:

user=root
password=0404
jdbcUrl=jdbc:mysql://localhost:3303/extra
driverClass=com.mysql.jdbc.Driver

2.Spring表达式语言SpEL

Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL。它 为 bean 的属性进行动态赋值提供了便利。

通过 SpEL 可以实现:

  • 通过 bean 的 id 对 bean 进行引用
  • 调用方法以及引用对象中的属性
  • 计算表达式的值
  • 正则表达式的匹配

示例代码:

Address.java

package com.java.spring.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

package com.java.spring.SpEL;

public class Car {

	private String brand;
	private double price;
	private String tyrePrimeter;
	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;
	}
	public String getTyrePrimeter() {
		return tyrePrimeter;
	}
	public void setTyrePrimeter(String tyrePrimeter) {
		this.tyrePrimeter = tyrePrimeter;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + ", tyrePrimeter=" + tyrePrimeter + "]";
	}
	
}

Person.java

package com.java.spring.SpEL;

public class Person {
	private String name;
	private Car car;
	//引用Address的city属性
	private String city;
	private String info;
	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;
	}
	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;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info
				+ "]";
	}
	
}

在xml中进行配置:(使用SpEL)

<bean id="address" class="com.java.spring.SpEL.Address">
  <property name="city" value="上海"></property>
  <property name="street" value="南京路"></property>
</bean>
<bean id="car" class="com.java.spring.SpEL.Car">
  <property name="brand" value="Audi"></property>
  <property name="price" value="300000.0000"></property>
  <!-- 使用SpEL的T()调用一个类的静态方法 -->
  <property name="tyrePrimeter" value="#{(T(java.lang.Math).PI)*3}"></property>
</bean>
<bean id="person" class="com.java.spring.SpEL.Person">
  <property name="name" value="Tom"></property>
  <!-- 使用SpEL引用其他Bean -->
  <property name="car" value="#{car}"></property>
  <!-- 使用SpEL引用其他对象的属性 -->
  <property name="city" value="#{address.city}"></property>
  <!-- 在SpEL中使用运算符 -->
  <property name="info" value="#{car.price>300000.000?'金领':'白领'}"></property>
</bean>

在主方法中实例化Bean:

package com.java.spring.SpEL;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-SpEL.xml");
		Person person=(Person)ctx.getBean("person");
		System.out.println(person);
	}
}

运行后输出:

Person [name=Tom, car=Car [brand=Audi, price=300000.0, tyrePrimeter=9.42477796076938], city=上海, info=白领]

3.IOC 容器中 Bean 的生命周期方法

Spring IOC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务.

Spring IOC 容器对 Bean 的生命周期进行管理的过程:

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

在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法。

示例代码:

Car.java

package com.java.spring.Cycle;

public class Car {
//1.通过构造器或工厂方法创建 Bean 实例; public Car(){ System.out.println("Car's constructor..."); } private String brand;
//3.调用 Bean 的初始化方法; public void init(){ System.out.println("Car's init method..."); }
//2.为 Bean 的属性设置值和对其他 Bean 的引用; public void setBrand(String brand){ System.out.println("Car's setBrand method..."); this.brand=brand; } public void destroy(){ System.out.println("Car's destroy method..."); } @Override public String toString() { return "Car [brand=" + brand + "]"; } }

在xml中进行配置:

<bean id="car" class="com.java.spring.Cycle.Car" init-method="init" destroy-method="destroy">
  <property name="brand" value="Audi"></property>
</bean>

在主方法中实例化并且关闭IOC容器:

public class Main {
	public static void main(String[] args){
		ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans-cycle.xml");
//4.Bean 可以使用了; Car car=(Car)ctx.getBean("car"); System.out.println(car);
//5.当容器关闭时, 调用 Bean 的销毁方法; ctx.close(); }

运行后输出:

Car's constructor...
Car's setBrand method...
Car's init method...
Car [brand=Audi]
Car's destroy method...

 

转载于:https://www.cnblogs.com/naihuangbao/p/10151933.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值