Spring5框架 -2 IOC容器 bean的管理配置,注入,数据库配置

1、什么是 IOC

(1)控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理

(2)使用 IOC 目的:为了耦合度降低

(3)做入门案例就是 IOC 实现 、

2、IOC 底层原理

(1)xml 解析、工厂模式、反射

3、画图讲解 IOC 底层原理

原始模式:耦合度太高了,一旦路径或者方法名改变,那么就是牵一发动全身,不利己开发

工厂模式:降低了耦合度。但是工厂和对象之间耦合度还是紧密联系

IOC容器解耦:如果UserDao路径发生改变只需要更改xml配置中的路径即可,降低耦合

 IOC(BeanFactory 接口)

1、IOC 思想基于 IOC 容器完成,IOC 容器底层就是对象工厂
2、Spring 提供 IOC 容器实现两种方式:(两个接口)
(1)BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用
* 加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象
(2)ApplicationContext:BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人
员进行使用
* 加载配置文件时候就会把在配置文件对象进行创建
3、ApplicationContext 接口有实现类

FileSystemXmlApplicationContext:xml文件在磁盘上的路径
ClassPathXmlApplicationContext:工程下src类的文件路径

 IOC的操作Bean管理

1、什么是 Bean 管理

(0)Bean 管理指的是两个操作

(1)Spring 创建对象

(2)Spirng 注入属性

2、Bean 管理操作有两种方式

(1)基于 xml 配置文件方式实现

(2)基于注解方式实现

IOC 操作 Bean 管理(基于 xml 方式)

1、基于 xml 方式创建对象

(1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建 (2)在 bean 标签有很多属性,介绍常用的属性

* id 属性:唯一标识

* class 属性:类全路径(包类路径)

*name属性:跟id是一样的,name属性可以加特殊字符

<!--配置user类的对象的创建-->
<bean id="user" class="com.atguigu.sping5.User"></bean>

(3)创建对象时候,默认也是执行无参数构造方法完成对象创建

   注意一旦对象创建了有参的构造方法,那么公共的无参构造方法就失效了,除非在创建一个无参构造方法

2、基于 xml 方式注入属性

(1)DI:依赖注入,就是注入属性

3、第一种注入方式:使用 set 方法进行注

(1)创建类,定义属性和对应的 set 方法

public class Book {
    private String bname;
    public void setBname(String bname) {
        this.bname = bname;
    }
    public static void main(String[] args) {
        Book book=new Book();
        book.setBname("chao");
    }
}

有参构造注入

public class Book {
    private String bname;
    public void setBname(String bname) {
        this.bname = bname;
    }
    public Book(String bname) {
        this.bname = bname;
    }
    public static void main(String[] args) {
        Book book=new Book("chao");
    }
}

(2)在 spring 配置文件配置对象创建,配置属性注入

book 类

public class Book {
    private String bname;
    private String bauthor;
    public void setBname(String bname) {
        this.bname = bname;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
    public void testDemo(){
        System.out.println("name="+bname);
        System.out.println("bauthor="+bauthor);
    }
}

bean.xml

<!--set方法注入 注意book类中必须提供set方法-->
<bean id="book" class="com.atguigu.sping5.Book">
    <!--使用property完成属性注入
       name:类中的属性名称 value:name属性的值
    -->
    <property name="bname" value="chao"></property>
    <property name="bauthor" value="chao2"></property>

</bean>

测试方法:

@Test
public void testBook(){
    //1.加载sping的配置文件
    ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
    System.out.println(context);
    //2.获取配置创建的对象
    Book book = context.getBean("book", Book.class);
    System.out.println(book);
    //调用对象的方法
    book.testDemo();
}

4、第二种注入方式:使用有参数构造进行注入

(1)创建类,定义属性,创建属性对应有参数构造方法

public class Order {
    private String name;
    private String address;
    //有参数的构造器
    public Order(String name, String address) {
        this.name = name;
        this.address = address;
    }
}

(2)在 spring 配置xml文件中进行配置

<!--有参构造注入属性-->
<bean id="order" class="com.atguigu.sping5.Order">
    <constructor-arg name="name" value="电脑"></constructor-arg>
    <constructor-arg name="address" value="北京"></constructor-arg>
<!--index=0 是order类中的第一个参数
<constructor-arg index="0" value="电脑"></constructor-arg>-->
</bean>

(3)测试类

@Test
public void testOrder(){
    //1.加载sping的配置文件
    ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
    System.out.println(context);
    //2.获取配置创建的对象
    Order order = context.getBean("order", Order.class);
    System.out.println(order);
    //调用对象的方法
    order.say();
}

IOC 操作 Bean 管理(xml 注入其他类型属性)

1、字面量

(1)null 值

(2)属性值包含特殊符号 >]]>

2、注入属性-外部 bean

(1)创建两个类 service 类和 dao 类

public interface UserDao {
    public void update();
}
public class UserDaoImpl implements UserDao{

    @Override
    public void update() {
        System.out.println("UserDaoImpl.update");
    }
}

(2)在 service 调用 dao 里面的方法

public class UserService {
    //创建Userdao的类型属性,生成set方法
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void add(){
        System.out.println("UserService.add");
/*        原始方式
        UserDao userDao = new UserDaoImpl();
        userDao.update();*/
        userDao.update();
    }
}

(3)在 spring 配置文件中进行配置

    <!--service和dao对象的创建-->
    <bean id="userservice" class="com.atguigu.service.UserService">
<!--
        注入userdao的对象 name:UserService类里面的属性
        ref:注入外部类
-->
            <property name="userDao" ref="userdao"></property>
    </bean>
    <bean id="userdao" class="com.atguigu.dao.UserDaoImpl"></bean>
</beans>

测试:

@Test
public void setUserDao() {
    ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");
    UserService userservice = context.getBean("userservice", UserService.class);
    userservice.add();
}

输出

UserService.add
UserDaoImpl.update

3、注入属性-内部 bean

(1)一对多关系:部门和员工 一个部门有多个员工,一个员工属于一个部门 部门是一,员工是多

(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

部门类:

public class Dept {
    private String name;
    @Override
    public String toString() {
        return "Dept{" +
                "name='" + name + '\'' +
                '}';
    }
    public void setName(String name) {
        this.name = name;
    }
}

员工类、

public class Emp {
    private String ename;
    private String gender;
    private Dept dept;
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void add(){
        System.out.println("ename="+ename);
        System.out.println("gender="+gender);
        System.out.println("dept="+dept);
    }
}

(3)在 spring 配置文件中进行配置

<!--内部bean-->
<bean id="emp" class="com.atguigu.bean.emp">
    <!--设置普通的属性-->
    <property name="ename" value="chao"></property>
    <property name="gender" value="boy"></property>
    <!--设置对象类型属性-->
    <property name="dept" >
        <bean id="dept" class="com.atguigu.bean.Dept">
            <!--设置对象dept下属性-->
            <property name="name" value="安保部"></property>
        </bean>
    </property>
</bean>
测试
@Test
public void test(){
    ApplicationContext context=new ClassPathXmlApplicationContext("bean3.xml");
    Emp emp = context.getBean("emp", Emp.class);
    emp.add();
}
 

4、注入属性-级联赋值

(1)第一种写法

<bean id="emp" class="com.atguigu.bean.Emp">
    <!--设置普通的属性-->
    <property name="ename" value="chao"></property>
    <property name="gender" value="boy"></property>
    <!--级联赋值-->
    <property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.atguigu.bean.Dept">
    <property name="name" value="财务部"></property>
</bean>

测试:

@Test
public void test(){
    ApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml");
    Emp emp = context.getBean("emp", Emp.class);
    emp.add();
}

(2)第二种写法

<bean id="emp" class="com.atguigu.bean.Emp">
    <!--设置普通的属性-->
    <property name="ename" value="chao"></property>
    <property name="gender" value="boy"></property>
    <!--级联赋值-->
    <property name="dept" ref="dept"></property>
    <property name="dept.name" value="技术部"></property>
</bean>
<bean id="dept" class="com.atguigu.bean.Dept"></bean>

需要在emp类中的dept属性的get方法写上

public Dept getDept() {
    return dept;
}

总结:

<!--内部bean-->
<bean id="emp" class="com.atguigu.bean.Emp">
    <property name="ename" value="123"></property>
    <property name="gender" value="1"></property>
    <property name="dept">
        <bean id="dept" class="com.atguigu.bean.Dept">
            <property name="name" value="jishubu"></property>
        </bean>
    </property>
</bean>
<!--外部bean-->
<bean id="dept" class="com.atguigu.bean.Dept">
    <property name="name" value="123"></property>
</bean>
<bean id="emp" class="com.atguigu.bean.Emp">
    <property name="dept" ref="dept"></property>
    <property name="gender" value="123"></property>
    <property name="ename" value="123"></property>
</bean>
<!--级联 需要emp类中需要dept对象属性的get方法-->
<bean id="dept" class="com.atguigu.bean.Dept"></bean>
<bean id="emp" class="com.atguigu.bean.Emp">
    <property name="dept" ref="dept"></property>
    <property name="gender" value="123"></property>
    <property name="dept.name" value="jishubu"></property>
</bean>

IOC 操作 Bean 管理(xml 注入集合属性)

1、注入数组类型属性

2、注入 List 集合类型属性

3、注入 Map 集合类型属性 (

1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法

public class Students {
    //数组
    private String[] courses;
    //list集合
    private List<String> list;
    //map集合
    private Map<String,String> map;
    //set集合
    private Set<String> sets;
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
    public void setCourses(String[] courses) {
        this.courses = courses;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}

配置文件

--集合类型属性的注入   配置对象-->
<bean id="students" class="com.atguigu.collectionType.Students">
<!--配置对象的属性-->
    <property name="courses">
        <array>
            <value>java</value>
            <value>database</value>
        </array>
    </property>
    <property name="list">
        <list>
            <value>sunchao</value>
            <value>sun</value>
        </list>
    </property>
    <property name="map">
        <map>
            <entry key="1" value="123"></entry>
            <entry key="2" value="233"></entry>
        </map>
    </property>
    <property name="sets">
        <set>
            <value>Mysql</value>
            <value>Redis</value>
        </set>
    </property>
</bean>

测试;

@Test
public void test(){
    //获取xml配置文件
    ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
    //获取配置文件中的对象
    Students students = context.getBean("students", Students.class);
    //调用对象的方法
    students.say();
    System.out.println(students);
}

4、在集合里面设置对象类型值

course类

package com.atguigu.collectionType;
/**
 * @author sunyc
 * @create 2022-04-12 15:40
 */
public class Course {
    private String name;

    public void setName(String name) {
        this.name = name;
    }
}

student类

public class Students {
    //数组
    private String[] courses;
    //list集合
    private List<String> list;
    //map集合
    private Map<String,String> map;
    //set集合
    private Set<String> sets;

    private List<Course> courseList;
    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
    public void setCourses(String[] courses) {
        this.courses = courses;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public void say(){
        System.out.println("courses="+Arrays.toString(courses));
        System.out.println("list="+list);
        System.out.println("map="+map);
        System.out.println("set="+sets);

    }
    @Override
    public String toString() {
        return "Students{" +
                "courses=" + Arrays.toString(courses) +
                ", list=" + list +
                ", map=" + map +
                ", sets=" + sets +
                '}';
    }
}
xml配置
<!--集合类型属性的注入   配置对象-->
<bean id="students" class="com.atguigu.collectionType.Students">
    <!--配置对象的属性-->
    <property name="courses">
        <array>
            <value>java</value>
            <value>database</value>
        </array>
    </property>
    <property name="list">
        <list>
            <value>sunchao</value>
            <value>sun</value>
        </list>
    </property>
    <property name="map">
        <map>
            <entry key="1" value="123"></entry>
            <entry key="2" value="233"></entry>
        </map>
    </property>
    <property name="sets">
        <set>
            <value>Mysql</value>
            <value>Redis</value>
        </set>
    </property>
    <!--注入list集合类型,值是对象类型-->
    <property name="courseList">
        <list>
            <ref bean="course" ></ref>
            <ref bean="course1" ></ref>
        </list>
    </property>
    
</bean>
<!--创建多个course对象-->
<bean id="course" class="com.atguigu.collectionType.Course">
    <property name="name" value="java"></property>
</bean>
<bean id="course1" class="com.atguigu.collectionType.Course">
    <property name="name" value="js"></property>
</bean>

测试:

@Test
public void test(){
    //获取xml配置文件
    ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
    //获取配置文件中的对象
    Students students = context.getBean("students", Students.class);
    //调用对象的方法
    students.say();
}

5、把集合注入部分提取出来

book类

public class Book {
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
}

(1)在 spring 配置文件中引入名称空间 util

<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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
</beans>

(2)使用 util 标签完成 list 集合注入提取

<!--1 提取 list 集合类型属性注入-->
<util:list id="booklist">
    <value>2</value>
    <value>3</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用-->
<bean id="book" class="com.atguigu.collectionType.Book">
    <property name="list" ref="booklist"></property>
</bean>

测试

@Test
public void test1(){
    //获取xml配置文件
    ApplicationContext context=new ClassPathXmlApplicationContext("bean6.xml");
    //获取配置文件中的对象
    Book students = context.getBean("book", Book.class);
    //调用对象的方法
    students.say();
}

IOC 操作 Bean 管理(FactoryBean)

1、Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)

2、普通 bean:在配置文件中定义 bean 类型就是返回类型

3、工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样

第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean

第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型

创建类:

public class MyBean implements FactoryBean<Course> {
    //定义返回的bean
    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        course.setName("123");
        return course;
    }
    @Override
    public Class<?> getObjectType() {
        return null;
    }
    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}

配置文件:

<bean id="mybean" class="com.atguigu.factoryBean.MyBean"></bean>

测试:

@Test
public void test12(){
    //1.加载sping的配置文件
    ApplicationContext context=new ClassPathXmlApplicationContext("bean7.xml");
    System.out.println(context);
    //2.获取配置创建的对象
    Course mybean = context.getBean("mybean", Course.class);
    System.out.println(mybean);
}

IOC 操作 Bean 管理(bean 作用域)

1、在 Spring 里面,设置创建 bean 实例是单实例还是多实例

2、在 Spring 里面,默认情况下,bean 是单实例对象

调用之前的代码判断:

@Test
public void testBook(){
    //1.加载sping的配置文件
    ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
    System.out.println(context);
    //2.获取配置创建的对象
    Book book = context.getBean("book", Book.class);
    //验证配置bean id=book的对象是单实例还是多实例 ,isSingleton 返回true为单实例 false为多实例
    System.out.println("book="+context.isSingleton("book"));
    //第二种验证
    Book book1 = context.getBean("book", Book.class);
    if(book==book1){
        System.out.println("单实例");
    } else{
        System.out.println("多实例");
    }
    System.out.println(book);
    //调用对象的方法
    book.testDemo();
}

3、如何设置单实例还是多实例

(1)在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例

(2)scope 属性值 第一个值 默认值,singleton,表示是单实例对象 第二个值 prototype,表示是多实例对象 

 配置文件

<bean id="book" class="com.atguigu.sping5.Book" scope="prototype">
@Test
public void testBook(){
    //1.加载sping的配置文件
    ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
    System.out.println(context);
    //2.获取配置创建的对象
    Book book = context.getBean("book", Book.class);
    //验证配置bean id=book的对象是单实例还是多实例 ,isSingleton 返回true为单实例 false为多实例
    System.out.println("book="+context.isSingleton("book"));
    //第二种验证
    Book book1 = context.getBean("book", Book.class);
    if(book==book1){
        System.out.println("单实例");
    } else{
        System.out.println("多实例");
    }
    System.out.println(book);
    //调用对象的方法
    book.testDemo();
}

(3)singleton 和 prototype 区别

第一 singleton 单实例,prototype 多实例

第二 设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象 设置 scope 值是 prototype 时候,不是在加载 spring 配置文件时候创建 对象,在调用 getBean 方法时候创建多实例对象 

IOC 操作 Bean 管理(bean 生命周期)

1、生命周期

(1)从对象创建到对象销毁的过程

2、bean 生命周期

(1)通过构造器创建 bean 实例(无参数构造)

(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)

(3)调用 bean 的初始化的方法(需要进行配置初始化的方法)

(4)bean 可以使用了(对象获取到了)

(5)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

3、演示 bean 生命周期

类;

public class Orders {
    private String name;
    public Orders() {
        System.out.println("orders 无参数构造-创建bean的实例-1");
    }
    public void setName(String name) {
        System.out.println("orders set方法--2");
    }
    //创建初始化的方法
    public void initMethod(){
        System.out.println("orders 初始化方法--3");
    }
    //销毁方法
    public void destoryMethod(){
        System.out.println("orders destoryMethod方法 销毁--5");
    }
}

配置文件:

<bean id="orders" class="com.atguigu.bean.Orders" init-method="initMethod" destroy-method="destoryMethod">
    <property name="name" value="sunchao"></property>
</bean>

测试:

@Test
public void test13(){
    //1.加载sping的配置文件
    ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("beanorders.xml");
    System.out.println(context);
    //2.获取配置创建的对象
    Orders orders = context.getBean("orders", Orders.class);
    System.out.println("获取到了创建bean实例的对象-4");
    System.out.println(orders);
    //手动销毁bean实例
    context.close();
}

4、bean 的后置处理器,bean 生命周期有七步

(1)通过构造器创建 bean 实例(无参数构造)

(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)

(3)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization

(4)调用 bean 的初始化的方法(需要进行配置初始化的方法)

(5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization

(6)bean 可以使用了(对象获取到了)

(7)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

5、演示添加后置处理器效果

(1)创建类,实现接口 BeanPostProcessor,创建后置处理器

public class MyBeanPost implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之前执行的方法");
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之后执行的方法");
        return bean;
    }
}

配置文件

<bean id="orders" class="com.atguigu.bean.Orders" init-method="initMethod" destroy-method="destoryMethod">
    <property name="name" value="sunchao"></property>
</bean>
<!--配置后置处理器-->
<bean id="mybean" class="com.atguigu.bean.MyBeanPost"></bean>

测试:

@Test
public void test13(){
    //1.加载sping的配置文件
    ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("beanorders.xml");
    System.out.println(context);
    //2.获取配置创建的对象
    Orders orders = context.getBean("orders", Orders.class);
    System.out.println("获取到了创建bean实例的对象-4");
    System.out.println(orders);
    //手动销毁bean实例
    context.close();
}

IOC 操作 Bean 管理(xml 自动装配)

1、什么是自动装配

(1)根据指定装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注入 2、演示自动装配过程

(1)根据属性名称自动注入

<!--引用外部bean
 autowire:byname根据属性的名称注入,特点注入值bean的id值要和类的属性名称一样例如dept
          bytype根据属性的类型注入
实现自动装配-->
<bean id="emp" class="com.atguigu.Autowire.Emp" autowire="byName">
   <!-- <property name="dept" ref="dept"></property>-->
</bean>
<bean id="dept" class="com.atguigu.Autowire.Dept"></bean>

(2)根据属性类型自动注入

<!--引用外部bean
 autowire:byname根据属性的名称注入,特点注入值bean的id值要和类的属性名称一样例如dept
          bytype根据属性的类型注入
实现自动装配-->
<bean id="emp" class="com.atguigu.Autowire.Emp" autowire="byType">
   <!-- <property name="dept" ref="dept"></property>-->
</bean>
<bean id="dept" class="com.atguigu.Autowire.Dept"></bean>

 IOC 操作 Bean 管理(外部属性文件

1、直接配置数据库信息

(1)配置德鲁伊连接池

(2)引入德鲁伊连接池依赖 jar 包

2、引入外部属性文件配置数据库连接池

(1)创建外部属性文件,properties 格式文件,写数据库信息

prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://10.0.0.8:3306/test
prop.userName=ecc_event_management
prop.password=94020212

 (2)把外部 properties 属性文件引入到 spring 配置xml文件中

* 引入 context 名称空间

<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:util="http://www.springframework.org/schema/util"
       xmlns:context="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/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/util/spring-context.xsd">

⚫ 在 spring 配置文件使用标签引入外部属性文件

<!--引入外部属性文件-->
<context:property-placeholder location="classpath*:jsbc.properties"/>
<!-- 配置连接池 -->
<!-- DruidDataSource dataSource = new DruidDataSource(); -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <!-- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        set方法注入
    -->
    <!-- 获取properties文件内容,根据key获取,使用spring表达式获取 -->
    <property name="driverClassName" value="${prop.driverClass}"></property>
    <property name="url" value="${prop.url}"></property>
    <property name="username" value="${prop.userName}"></property>
    <property name="password" value="${prop.password}"></property>
</bean>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值