Spring_使用外部属性文件&SpEL

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 + "]";
    }
}

目录

All that work will definitely pay off
原文地址:https://www.cnblogs.com/afangfang/p/12957882.html