Java Spring 2024年01月10日 spring bean 依赖 jdbc xml

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"  >-->
<!--&lt;!&ndash;          init-method="initMethod"&ndash;&gt;-->
<!--&lt;!&ndash;          destroy-method="destroyMethod"&ndash;&gt;-->
<!--    </bean>-->

    <!--set方法 bean依赖注入-->
<!--    <bean id="stuService" class="com.example.service.impl.StuServiceImpl">-->
<!--&lt;!&ndash;    name="stuDao" 是StuServiceImpl中创建的stuDao对象-->
<!--        ref="stuDao"  是bean装配的id名&ndash;&gt;-->
<!--        <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();
    }
}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值