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...

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

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

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  2. Spring 应用外部属性文件 配置 context 错误

    在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...

  3. IoC容器-Bean管理XML方式(引入外部属性文件)

    IoC操作Bean管理(引入外部属性文件) 1,直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池依赖jar包 2,通过引入外部属性文件配置数据库连接池 (1)创建外部属性文件,pro ...

  4. Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...

  5. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  6. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  8. Spring-Bean配置-使用外部属性文件(转)

    Spring-Bean配置-使用外部属性文件 所以可以通过@value注解获取配置文件的key-value,生成一个配置文件bean.用以在代码中直接使用bean的方式. •在配置文件里配置Bean时 ...

  9. spring4-2-bean配置-6-使用外部属性文件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk0AAAFGCAIAAAD4tzxRAAAgAElEQVR4nO2d27HsOm+tOxWn4CeXAm ...

随机推荐

  1. java简单的邮件发送

    java实现简单的邮件发送案例,学会了这个你就可以利用这个来整你的好友了,不断地给他进行邮箱轰炸(当然个人不建议瞎搞),最重要的是明白其中的原理最好了.话不多说,直接看代码案例了.首先需要导入的jar ...

  2. 手写数字识别---demo

    数据准备 课程中获取数据的方法是从库中直接load_data from keras.datasets import mnist (x_train, y_train), (x_test, y_test) ...

  3. Delphi - 让Delphi10.2在Windows下开发的图形界面程序运行在64位Linux中!

    FmxLinux官网:https://fmxlinux.com/ 参考: http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Linux_Applica ...

  4. C - 前m大的数 (结构体)

    点击打开链接 还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的 ...

  5. 初识PHP之php运行流程及原理(一)

    初识PHP一.用脚本命令行运行php(1)打开cmd.exe(winkey+R)(2)找到php.exe(拖进cmd即可)(3)输入命令php.exe -f "文件实际路径"注:运 ...

  6. C#4.0使用dynamic 动态添加属性

    最近做一个项目,用wpf mvvm实现,而前台表格需要根据数据库某表的设置不同生成不同的列名.过去用winform和Ado.net实现这种功能的时候就只需要拼装DataTable,拼成最后需要的表格, ...

  7. 下载Centos7 64位镜像

    下载Centos7 64位镜像 1.打开Centos官网 打开Centos官方网站地址:https://www.centos.org/,点击Get CentOS Now 2.点击Minimal ISO ...

  8. 【bzoj3684】 大朋友和多叉树 生成函数+多项式快速幂+拉格朗日反演

    这题一看就觉得是生成函数的题... 我们不妨去推下此题的生成函数,设生成函数为$F(x)$,则$[x^s]F(x)$即为答案. 根据题意,我们得到 $F(x)=x+\sum_{i∈D} F^i(x)$ ...

  9. C#:注册机的实现

    先看界面 软件的实现: SoftReg类: using System; using System.Collections.Generic; using System.Linq; using Syste ...

  10. 页面按钮埋点+跟踪location.search

    <a href="javascript: void(0)" onclick="setUrl('https://baoxian.pingan.com/pa18shop ...