前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 基于XML开发 bean 配置 连接池

Spring 基于XML开发 bean 配置 连接池

作者头像
陶然同学
发布2023-02-24 09:38:58
2630
发布2023-02-24 09:38:58
举报
文章被收录于专栏:陶然同学博客

目录

约束

bean 存入spring容器

整合Junit

手动获取Spring容器

xml配置连接池


约束

        spring约束需要在spring官网复制 为了省去麻烦 这里直接给出网址Core Technologies

方框中的就是约束 把它复制下来放到xml文件中 这里只是简单的约束 下面是全部约束

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans.xsd
                  http://www.springframework.org/schema/mvc
                  http://www.springframework.org/schema/mvc/spring-mvc.xsd
                  http://www.springframework.org/schema/aop
                  http://www.springframework.org/schema/aop/spring-aop.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context.xsd
                  http://www.springframework.org/schema/tx
                  http://www.springframework.org/schema/tx/spring-tx.xsd">

bean 存入spring容器

        bean标签:用于配置让spring创建对象 并且存入ioc容器中

        id:对象的唯一标识

        class:指定类的全限定类名

        例如

代码语言:javascript
复制
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> 

        当对象中有属性的时候必须 要有set方法 xml中使用property给对象属性赋值

        property标签:给对象属性赋值

        name:对象属性名

        ref:引用bean标签创建的对象 赋值给对象的属性

代码语言:javascript
复制
    <bean id = "userDao" class="com.czxy.demo02.dao.impl.UserDaoImpl"></bean>

    <bean id = "userService" class="com.czxy.demo02.service.Impl.UserServiceImpl">
        <property name="userDaoimpl" ref="userDao"></property>
    </bean>

整合Junit

        对应整合Junit只是加载配置文件有些不同 以前加载配置文件@ContextConfiguration的属性是

classes 现在xml加载配置文件@ContextConfiguration属性是locations 是个数组 指定xml配置文件

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:demo03.xml"})
public class TestA {

    @Resource(name = "dataSource")
    private DataSource dataSource;


    @Test
    public void test01(){
        System.out.println(dataSource);
    }
}

手动获取Spring容器

        BeanFactory是所有类的顶层接口

        ClassPathXmlApplicationContext:从类路径下加载配置文件【使用这种】

        FileSystemXmlApplicationContext:从磁盘路径下加载配置文件

        使用getBean获取对象

代码语言:javascript
复制
public class TestB {
    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("demo01.xml");
        User user = (User)beanFactory.getBean("user");
        user.dao();
    }
}

xml配置连接池

        使用context:property-placeholder加载属性配置文件

        最后创建Druid对象 依次注入属性 放入IOC容器

代码语言:javascript
复制
<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans.xsd
                  http://www.springframework.org/schema/mvc
                  http://www.springframework.org/schema/mvc/spring-mvc.xsd
                  http://www.springframework.org/schema/aop
                  http://www.springframework.org/schema/aop/spring-aop.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context.xsd
                  http://www.springframework.org/schema/tx
                  http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:property-placeholder location = "classpath:db.properties"></context:property-placeholder>

    <bean id = "dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
 </beans>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-08-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 约束
  • bean 存入spring容器
  • 整合Junit
  • 手动获取Spring容器
  • xml配置连接池
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档