1.创建一个spring 工程 导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
2.写四个文件
public interface StuService {
void show();
}
public class StuServiceImpl implements StuService {
private StuDao stuDao;
public StuDao getStuDao() {
return stuDao;
}
/**
* 方法一:set方法 bean依赖注入
* @param stuDao
*/
public void setStuDao(StuDao stuDao) {
this.stuDao = stuDao;
}
public StuServiceImpl() {
}
/**
* 方法二:带参构造 bean依赖注入
* @param stuDao
*/
public StuServiceImpl(StuDao stuDao) {
this.stuDao = stuDao;
}
@Override
public void show() {
stuDao.show();
}
}
public interface StuDao {
void show();
}
public class StuDaoImpl implements StuDao {
@Override
public void show() {
System.out.println("我是show方法");
}
//初始化
private void initMethod() {
System.out.println("方法初始化");
}
//销毁
private void destroyMethod() {
System.out.println("方法销毁");
}
}
3.配置文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd"
>
<!-- init-method="initMethod" 初始化
destroy-method="destroyMethod" 销毁
scope="singleton" 单例默认
scope="prototype" 多例-->
<!-- <bean id="stuDao" class="com.example.dao.impl.StuDaoImpl" >-->
<!--<!– init-method="initMethod"–>-->
<!--<!– destroy-method="destroyMethod"–>-->
<!-- </bean>-->
<!--set方法 bean依赖注入-->
<!-- <bean id="stuService" class="com.example.service.impl.StuServiceImpl">-->
<!--<!– name="stuDao" 是StuServiceImpl中创建的stuDao对象-->
<!-- ref="stuDao" 是bean装配的id名–>-->
<!-- <property name="stuDao" ref="stuDao"></property>-->
<!-- </bean>-->
<!--带参构造方法 bean依赖注入-->
<!-- <bean id="stuService" class="com.example.service.impl.StuServiceImpl">-->
<!-- <constructor-arg name="stuDao" ref="stuDao"></constructor-arg>-->
<!-- </bean>-->
<!--引入数据源-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${MYSQL_DRIVER}"/>
<property name="url" value="${MYSQL_URL}"/>
<property name="username" value="${MYSQL_USER}"/>
<property name="password" value="${MYSQL_PASSWORD}"/>
</bean>
</beans>
MYSQL_DRIVER=com.mysql.jdbc.Driver
MYSQL_URL=jdbc:mysql://localhost:5040/db_zdy?useUnicode=true&characterEncoding=utf-8&useSSL=false
MYSQL_USER=root
MYSQL_PASSWORD=123456
4.测试
public class StuTest {
// @Test
// public void Test01(){
// ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//
// StuDao stuDao = (StuDao) classPathXmlApplicationContext.getBean("stuDao");
//
// stuDao.show();
// }
// @Test
// public void test02(){
// ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
// StuServiceImpl stuService = (StuServiceImpl) classPathXmlApplicationContext.getBean("stuService");
// stuService.show();
// }
@Test
/**
* 通过容器获取数据源对象
*/
public void test03() throws SQLException {
// 获取容器对象
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml");
// 通过容器对象获取 DataSource 对象
DataSource dataSource = (DataSource) app.getBean("dataSource");
// 通过 DataSource 对象获取连接
Connection connection = dataSource.getConnection();
System.out.println(connection); // 打印连接地址
// 释放连接
connection.close();
}
}