本文共 1167 字,大约阅读时间需要 3 分钟。
package shangbo.spring.core.example21;import java.util.Properties;public class DataService { private Properties dbconfig; public void setDbconfig(Properties dbconfig) { this.dbconfig = dbconfig; } public String toString() { return dbconfig.toString(); }}
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/mydb
package shangbo.spring.core.example21;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App { public static void main(String[] args) { // 实例化 Spring IoC 容器 ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", DataService.class); // 从容器中获得 Service 对象 DataService p = context.getBean(DataService.class); // 使用对象 System.out.println(p); }}
我们也可以通过如下方式定义Properties
jdbc.driver.className=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mydb
–
– 声 明:转载请注明出处 – Last Updated on 2017-06-17 – Written by ShangBo on 2017-05-22 – End