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

   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:指定类的全限定类名



        例如  

 






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



        property标签:给对象属性赋值



        name:对象属性名



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



<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配置文件



@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获取对象



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容器



<?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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值