1.使用外部属性文件

beans-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:p="http://www.springframework.org/schema/p"
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"> <!-- 导入属性文件 -->
<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>

Main.java

package com.aff.spring.beans.properties;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
//com.mchange.v2.c3p0.impl.NewProxyConnection@2db7a79b }
}

db.properties

user=root
password=123456
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test

2.SpEL

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

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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.aff.spring.beans.spel.Address">
<!-- 使用SpEL 为属性 赋一个字面值 -->
<property name="city" value="#{'hefei'}"></property>
<property name="street" value="#{'nanqi'}"></property>
</bean> <bean id="car" class="com.aff.spring.beans.spel.Car">
<property name="brand" value="Audi"></property>
<property name="price" value="300000"></property>
<!-- 使用SpEL 引用类的静态属性 -->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property>
</bean> <bean id="person" class="com.aff.spring.beans.spel.Person">
<property name="name" value="hff"></property>
<!--使用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>
</bean> </beans>

Main.java

package com.aff.spring.beans.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");
Address address = (Address) ctx.getBean("address");
System.out.println(address);
// Address [city=hefei, street=nanqi] Car car = (Car) ctx.getBean("car");
System.out.println(car);
// Car [brand=Audi, price=300000.0, tyrePerimeter=251.32741228718345] Person person = (Person) ctx.getBean("person");
System.out.println(person);
//Person [name=hff, car=Car [brand=Audi, price=300000.0, tyrePerimeter=251.32741228718345], city=hefei, info=白领] }
}

Address.java

package com.aff.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 + "]";
}
public Address() {
super();
}
public Address(String city, String street) {
super();
this.city = city;
this.street = street;
} }

Car.java

package com.aff.spring.beans.spel;

public class Car {
private String brand;
private double price;
// 轮胎的周长
private double tyrePerimeter; 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 double getTyrePerimeter() {
return tyrePerimeter;
} public void setTyrePerimeter(double tyrePerimeter) {
this.tyrePerimeter = tyrePerimeter;
} @Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + "]";
} public Car() {
super();
} public Car(String brand, double price, double tyrePerimeter) {
super();
this.brand = brand;
this.price = price;
this.tyrePerimeter = tyrePerimeter;
} }

Person.java

package com.aff.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 + "]";
}
}

目录

Spring_使用外部属性文件&SpEL的更多相关文章

  1. Spring_使用外部属性文件

    beans-properties.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns ...

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

    1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...

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

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

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

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

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

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

  6. spring 使用外部属性文件

    一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...

  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. Spring---Bean使用外部属性文件

    在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离 Spring 提供了 ...

随机推荐

  1. dij-spfa乱搞

    以前见过一篇另类堆优化dij的题解,然而找不到了 那位作者称它为dij-spfa(大概是这个意思,然而确实很形象 这方法比较玄学,正确性没有严格证出来,然而对拍是验证猜想的最好途径 不过也可能并不玄学 ...

  2. HTML(标题/图片/链接/列表标签)

    <!DOCTYPE html> 声明 <!DOCTYPE html> 是 html5 标准网页声明,全称为 Document Type HyperText Mark-up La ...

  3. ubuntu 下修改MySQL 的root用户密码

    环境:Ubuntu 16.04  :  Mysql 5.7问题:mysql root登录密码 忘记了..忘记了..忘..了 1.登录MySQL $ mysql -u root -p 输入密码,如果登录 ...

  4. 关于【MySQL 子查询——查询最大值】的补充说明

    昨天在使用子查询查找最高分和最低分时遇上了一点问题,情况是这样的:如果找到的最高分或最低分是唯一值则不会有什么问题,但如果有其它班级学生的成绩恰好与查询的最高分或最低分相同时就会把那个学生的信息也显示 ...

  5. spark系列-8、Spark Streaming

    参考链接:http://spark.apache.org/docs/latest/streaming-programming-guide.html 一.Spark Streaming 介绍 Spark ...

  6. 【漫画】JAVA并发编程三大Bug源头(可见性、原子性、有序性)

    原创声明:本文转载自公众号[胖滚猪学编程]​ 某日,胖滚猪写的代码导致了一个生产bug,奋战到凌晨三点依旧没有解决问题.胖滚熊一看,只用了一个volatile就解决了.并告知胖滚猪,这是并发编程导致的 ...

  7. spring学习笔记(九)事务学习(上)

    前述 ​ 这段时间在工作中碰到一个事务相关的问题.先说下这个问题的场景,我们是一个商城项目,正在开发优惠券模块,现在有一个需求是需要批量领取优惠券,而且在领券时,其中一张领取失败不能影响其他符合要求的 ...

  8. react中this.setState的理解

    this.setState作用? 在react中要修改this.state要使用this.setState,因为this.state只是一个对象,单纯的修改state并不会触发ui更新.所以我们需要用 ...

  9. Linux系统上LNMP服务器的搭建

    一.确保登录用户权限为root 如果没有root权限: su  root 切换到root用户,但不切换环境变量: 或者 su - root 完整地切换到root用户环境. 二.开始下载并安装LNMP( ...

  10. 利用一个VI写入或读取另一个VI的控件值

    利用一个VI写入或读取另一个VI的控件值(转载自浅浅的小草) 有一个VI,叫未命名1.vi.   还有一个vi,叫未命名2.vi     现在,我在 1.vi里要对2.vi的字符串控件进行写入.  可 ...