使用Spring创建数据库连接池,加载外部属性文件dbconfig.properties
dbconfig.properties 文件配置如下:
jdbc.username=rootjdbc.password=12345678jdbc.jdbcUrl=jdbc:mysql://localhost:3306/testjdbc.driverClass=com.mysql.jdbc.Driver
加载外部属性文件,引入了classpath类路径下面的dbconfig文件
基于XML的自动装配
Person类
package com.sf.jin;import java.util.List;import java.util.Map;import java.util.Properties;public class Person { private String name; private Integer age; private String email; private String gender; private Car car; private List bookList; private Map
Car类
package com.sf.jin;public class Car { private String carName; private Integer price; private String color; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car{" + "carName='" + carName + ''' + ", price=" + price + ", color='" + color + ''' + '}'; }}
给Person赋值Car的值,使用 property:ref 这是手动赋值,使用autowire进行自动装配更方便,
主要有 autowire="byName"根据名字进行装配和 autowire="byType"根据类型进行装配
<?xml version="1.0" encoding="UTF-8"?>