作者:可菲儿2702934224 | 来源:互联网 | 2023-07-29 21:45
篇首语:本文由编程笔记#小编为大家整理,主要介绍了Spring导入外部资源相关的知识,希望对你有一定的参考价值。
- 创建一个数据库连接的 properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezOne=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=admin888
- 创建ApplicationContext.xml配置文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="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 https://www.springframework.org/schema/context/spring-context.xsd">
这里导入资源有两种方式,选择一种即可
- 创建一个测试类
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext cOntext= new ClassPathXmlApplicationContext("ApplicationContext.xml");
DruidDataSource datasource = context.getBean("datasource", DruidDataSource.class);
System.out.println(datasource);
}
}
- 运行结果