Spring 简介 (1)

Spring 简介 (1)

1.Spring概述

①Spring是一个开源框架

②Spring为简化企业级开发而生,使用Spring开发可以将Bean对象,Dao组件对象,Service组件对象等交给Spring容器来管理,这样使得很多复杂的代码在Spring中开发却变得非常的优雅和简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。

③Spring是一个"IOC(DI)和***AOP***容器框架。

④Spring的优良特性

[1]侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API

[2]控制翻转:IOC——Inversion of Control,指的是将对象的创建权交给Spring去创建。使用Spring之前,对象的创建都是由我们自己在代码中new创建。而使用Spring之后。对象的创建都是由给了Spring框架。

[3]依赖注入:DI——Dependency Injection,是指依赖的对象不需要手动调用setXX方法去设置,而是通过配置赋值。

[4]面向切面编程:Aspect Oriented Programming——AOP

[5]容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期

[6]组件化了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。

[7]一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的Spring JDBC)。

2.Spring的模块介绍

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FX3FZCHV-1608195664657)(file:///C:\Users\psd\AppData\Local\Temp\ksohtml8432\wps1.jpg)]****

Spring框架分为四大模块:

Core核心模块。负责管理组件的Bean对象

spring-beans-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

面向切面编程

spring-aop-4.0.0.RELEASE.jar

spring-aspects-4.0.0.RELEASE.jar

数据库操作

spring-jdbc-4.0.0.RELEASE.jar

spring-orm-4.0.0.RELEASE.jar

spring-oxm-4.0.0.RELEASE.jar

spring-tx-4.0.0.RELEASE.jar

spring-jms-4.0.0.RELEASE.jar

Web模块

spring-web-4.0.0.RELEASE.jar

spring-webmvc-4.0.0.RELEASE.jar

spring-websocket-4.0.0.RELEASE.jar

spring-webmvc-portlet-4.0.0.RELEASE.jar

4.IOC的依赖注入

4.1什么是IOC

IOC 全称指的是 Inverse Of Control 控制反转。

在使用spring模块之前都是我们自己去new , 而使用spring模块之后,对象的创建交给Spring去创建 .

注:所有使用Spring功能的对象都是需要交给Spring容器去创建

4.2什么是DI

DI 指的是Dependency Injection 。是依赖注入的意思。

依赖 : A要完成某种功能 ,在这个过程中需要B的存在 , 若果没有B就无法完成 , A对于 B来说 就是 ,A依赖B

// 比如web阶段书城中 
public class BookService{
 bookDao bookDao ;
 public void setBookDao(BookDao bookDao){
     this.bookDao = bookDao;
 }
}

**依赖注入:**就是给依赖的对象进行赋值操作

在Spring中 , 我们的依赖注入, 可以通过xml配置文件以及注解进行操作

4.3 第一个IOC示例程序 – 通过id获取对象 (重点)

4.3.1 导包

junit_4.12.jar
org.hamcrest.core_1.3.0.jar
spring-beans-5.2.5.RELEASE.jar
spring-context-5.2.5.RELEASE.jar
spring-core-5.2.5.RELEASE.jar
spring-expression-5.2.5.RELEASE.jar
spring-jcl-5.2.5.RELEASE.jar

4.3.2创建Person对象
public class Person {
    private Integer id;
    private String name;
    private String phone;
    private Integer age;
    } 
4.3.3 配置application.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
        bean : 表示配置一个Bean对象实例
        class : 表示全路径
        id : 表示唯一标识
        property : 标签是通过setxxx 方法对属性进行赋值操作 
        name : 表示属性
        value : 表示值
    -->
    <bean id="person1" class="com.atguigu.pojo.Person">
        <property name="id" value="1"/>
        <property name="name" value="laoxu"/>
        <property name="phone" value="1231345345"/>
        <property name="age" value="18"/>
     </bean>

</beans>
4.3.4 测试
public class IOCTest1 {
    @Test
    public void test1(){
        /**
         * ClassPathXmlApplicationContext 表示从类路径加载xml的配置文件
         */
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        // 通过id获取指定的对象
        Person person = (Person) applicationContext.getBean("person1");
        System.err.println(person);
    }
}

问题 :

  1. FileSystemXmlApplicationContext怎么用

    // 跟使用JavaSe相对路径一样, 不推荐使用 
    FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\JavaEE_Second\\javaSSM\\springhello01\\resources\\bean.xml");
    
  2. Bean 在什么时候被创建

    **答 😗*在创建ApplicatiocnContext容器对象时一起创建Bean对象 (默认),

    项目启动扫描application.xml文件时 , lazy-init=“true” 时 ,什么时候调用会调用

  3. 如果调用getBean多次 , 会被创建多少次

    **答 😗*默认创建是同一个 , 默认是单例

  4. ​ scope=“prototype”,多例Spring中bean 线程安全吗?

    bean不是线程安全的,需要开发人员自己来管理bean

    TheadLocal

    k:Thread.currentThread(); v:Object

    常见的错误:

    指定的id不存在。找不到bean对象。

4.4 IOC示例程序 – 通过类型获取对象

<bean class="com.atguigu.pojo.Person" id="p2">
    <property name="id" value="2"/>
    <property name="name" value="class 类型查找"/>
    <property name="age" value="18"/>
    <property name="phone" value="110"/>
</bean>
4.5 IOC示例程序 – 通过构造器方法参数名注入
<bean class="com.atguigu.pojo.Person" id="p3">
   <!--
   public Person(Integer id, String name, Integer age, String phone)
   -->
    <constructor-arg name="id" value="3"/>
    <constructor-arg name="name" value="构造器赋值"/>
    <constructor-arg name="age" value="18"/>
    <constructor-arg name="phone" value="120"/>

</bean>

4.6 IOC示例程序 – index属性指定构造器 参数的顺序

<bean class="com.atguigu.pojo.Person" id="p4">
    <constructor-arg index="0" value="4"/>
    <constructor-arg index="1" value="参数的类型"/>
    <constructor-arg index="2" value="18"/>
    <constructor-arg index="3" value="18990932434"/>
</bean>

4.7 IOC示例程序 – 根据参数类型注入

  <bean class="com.atguigu.pojo.Person" id="p5">
      <!--
    public Person(Integer id, String name, Integer age, String phone)
    public Person(Integer id, String name, String phone, Integer age)

    type属性表示参数的具体类型
-->
      <constructor-arg index="0" value="5" type="java.lang.Integer"/>
      <constructor-arg index="1" value="type 类型" type="java.lang.String"/>
      <constructor-arg index="2" value="1100" type="java.lang.Integer"/>
      <constructor-arg index="3" value="111111111111 类型" type="java.lang.String"/>
  </bean>

4.8 IOC之P名称空间

<!--
	 注意使用P标签时 需要导入一个标签
     xmlns:p="http://www.springframework.org/schema/p"
-->
<bean class="com.atguigu.pojo.Person"
      id="p6" p:id="6" p:name="p名称空间" p:phone="120"/>

4.9 测试null值的使用

<bean class="com.atguigu.pojo.Person" id="p7">
    <property name="id" value="7"/>
    <property name="name" value="null 测试"/>
    <property name="phone" >
        <null></null>
    </property>
</bean>

4.10 IOC之子对象的赋值测试 (重点)

创建个新的工程。测试Spring的开发环境。此不重复。请参阅前面,环境搭建。

public class Car {
 private String type;
 private String carNo;
 ...
}
public class Person {
 private Integer id;
 private String name;
 private String phone;
 private Integer age;
 private Car car;
 ...
}
<bean id="car" class="com.atguigu.pojo.Car">
    <property name="type" value="捷豹"/>
    <property name="carNo" value="京A876"/>
</bean>
<!-- ref 表示引用id的car -->
<bean id="person2" class="com.atguigu.pojo.Person">
    <property name="id" value="1"/>
    <property name="name" value="laoxu"/>
    <property name="phone" value="1231345345"/>
    <property name="age" value="18"/>
    <property name="car" ref="car"/>
</bean>

test:

@Test
public void test3(){
    /**
     * ClassPathXmlApplicationContext 表示从类路径加载xml的配置文件
     */
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
    // 通过id获取指定的对象
    Person person = (Person) applicationContext.getBean("person1");

    System.err.println(person);
}

4.11 IOC之内部Bean的使用

<bean id="person4" class="com.atguigu.pojo.Person">
    <property name="id" value="1"/>
    <property name="name" value="laoxu"/>
    <property name="phone" value="1231345345"/>
    <property name="age" value="18"/>
    <property name="car">
    <!-- 内部Bean只能赋值使用.无法通过Spring容器直接获取到 -->
        <bean class="com.atguigu.pojo.Car"  id="car2">
            <property name="type" value="路虎"/>
            <property name="carNo" value="京A78999"/>
        </bean>
    </property>

</bean>

4.12 IOC之list属性的赋值

<bean class="com.atguigu.pojo.Person" id="p10">
    <property name="id" value="10"/>
    <!--
        list标签表示给list集合类型的属性赋值
    -->
    <property name="list">
        <list>
            <!--
                每个value表其中一个值
            -->
            <value>item1</value>
            <value>item2</value>
            <value>item3</value>
            <value>item4</value>
        </list>
    </property>
</bean>

4.13 IOC之Map属性的赋值

<bean class="com.atguigu.pojo.Person" id="p11">
    <property name="id" value="11"/>
    <property name="map">
        <!--
            map 标签表示赋值类型是map集合
        -->
        <map>
            <entry key="k1" value="v1"/>
            <entry key="k2" value="v2"/>
            <entry key="k3" value="v3"/>
        </map>
    </property>
</bean>

4.14 IOC之Properties属性的赋值

public class Person {
    private Integer id;
    private String name;
    private Integer age;
    private String phone;
    private Car car;
    private List<String> list;
    private Map<String , Object> map;
    private Properties props;
}
<bean class="com.atguigu.pojo.Person" id="p11">
    <property name="id" value="11"/>
  
    <!--
          prop表示一个键值对
              key表示键
              标签里的内容表示值
      -->
    <property name="props">
        <props>
            <prop key="url">jdbc:mysql://localhost:3306/test</prop>
            <prop key="username">root</prop>
            <prop key="password">root</prop>
        </props>
    </property>
</bean>

4.15 IOC之util名称空间

<util:list id="list01">
    <value>item1</value>
    <value>item2</value>
    <value>item3</value>
</util:list>

<bean id="p13" class="com.atguigu.pojo.Person">
    <property name="id" value="13"/>
    <property name="list" value="list01"/>
</bean>

4.16 IOC之静态工厂实例方法创建Bean

实验15:配置通过静态工厂方法创建的bean

工厂类:

public class PersonFactory {
    public static Person create(){
        return new Person(1,"static create()","24314234",18);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--   class属性 + factory-method属性   是静态工厂方法创建Bean对象 -->
    <bean id="personFactory"
          class="com.atguigu.factory.PersonFactory" factory-method="create"/>
</beans>

test:

public class IOCTest02 {

    @Test
    public void test4() {
        /**
         * ClassPathXmlApplicationContext 表示从类路径加载xml的配置文件
         */
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml");
        // 通过id获取指定的对象
        Person person = (Person) applicationContext.getBean("personFactory");

        System.err.println(person);
    }

}

4.17 IOC之实例工厂实例方法创建Bean

<!-- 配置工厂实例 -->
<bean id="person" class="com.atguigu.factory.PersonFactory"/>

<!-- factory-bean属性 + factory-method属性  工厂实例方法创建Bean对象  -->
<bean id="per" factory-bean="person" factory-method="create2"/>

4.18 IOC之接口方法创建对象

BeanFactory:SpringIoc最顶层的接口

FactoryBean:用于Spring整合第三方类库暴露出去的接口

实验17:配置FactoryBean接口创建Bean对象

  1. 创建一个类去实现FactoryBean接口
  2. 实现它的方法
  3. 到Spring的配置文件中去配置

FactroryBean接口实现类:

public class PersonFactoryBean implements FactoryBean<Person> {

    /**
     * 创建bean对象,将当前方法的返回值放入容器
     * @return
     * @throws Exception
     */
    @Override
    public Person getObject() throws Exception {
        return new Person(17,"FactoryBean接口方式","120",18);
    }

    /**
     * 获取bean的Class类型
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return Person.class;
    }

    /**
     * 是否为单例
     * @return
     */
    @Override
    public boolean isSingleton() {
        return false;
    }
}

4.19 IOC之继承方法创建对象

<bean id="parent" class="com.atguigu.pojo.Person">
    <property name="id" value="1"/>
    <property name="name" value="laoxu"/>
    <property name="phone" value="1231345345"/>
    <property name="age" value="18"/>
    <property name="car" ref="car"/>
</bean>
<bean id="person6" class="com.atguigu.pojo.Person" parent="parent">
    <property name="id" value="1001"/>
</bean>

4.20 IOC之abstract抽象Bean

<!--
         abstract="true" 表示当前配置信息,只能用于继承,不能被实例化,默认为false
    -->
<bean id="parent" class="com.atguigu.pojo.Person" abstract="true">
    <property name="id" value="1"/>
</bean>

4.21 IOC之组件(Bean对象) 创建顺序

实验20:bean之间的依赖 depends-on 属性

在Spring容器中.Bean对象的创建顺序默认是他们在配置文件中,从上到下的顺序决定

public class A {
    public A() {System.out.println("A 被创建了");}
}
public class B {
    public B() {System.out.println("B 被创建了");}
}
public class C {
    public C() {System.out.println("C 被创建了");}
}

application.xml:

<!--
  1 在Spring容器中.Bean对象的创建顺序默认是他们在配置文件中,从上到下的顺序决定.
  2 可以在bean的配置上,使用属性depends-on表示前置创建
-->
<bean class="com.atguigu.depends.A" id="a" depends-on="c,b"/>
<bean class="com.atguigu.depends.B" id="b" />
<bean class="com.atguigu.depends.C" id="c" />

4.22 IOC之Bean的单例和多例(重点)

实验21:测试bean的作用域,分别创建单实例和多实例的bean★

<!--
        scope:指定bean的范围
            singleton:单例
                默认
                会跟着Spring容器一起被创建:立即加载
                多次调用getBean()方法都会返回一个对象

            prototype:多例
                不会跟着Spring容器一起被创建:延迟加载
                每次调用getBean()会重新创建一个新的

   request:表示一次请求内多次调用getBean都返回同一个对象
            session:表示一个会话内多次调用getBean都返回同一个对象
            globalsession: 全局session
     -->
<bean id="person18" class="com.atguigu.pojo.Person" scope="prototype">
    <property name="id" value="20"/>
</bean>

4.23 IOC之Bean的生命周期

实验22:创建带有生命周期方法的bean

给Person添加初始化方法和销毁方法:

public class Person {
    private Integer id;
    private String name;
    private String phone;
    private Integer age;
    private Car car;

    /*初始化方法 */
    public void init(){
        System.out.println(" init() 这里可以做一些初始化操作 ");
    }
    /* 销毁方法 */
    public void destroy(){
        System.out.println(" destroy() 这里可以做一些销毁操作 ");
    }
}
  <!--
        scope:指定bean的范围
            singleton:单例
                默认
                会跟着Spring容器一起被创建:立即加载
                多次调用getBean()方法都会返回一个对象

            prototype:多例
                不会跟着Spring容器一起被创建:延迟加载
                每次调用getBean()会重新创建一个新的

   request:表示一次请求内多次调用getBean都返回同一个对象
            session:表示一个会话内多次调用getBean都返回同一个对象
            globalsession: 全局session
     -->
    <bean id="person20" class="com.atguigu.pojo.Person"
          init-method="init" destroy-method="destroy"
    scope="prototype">

    </bean>

4.24 懒加载

<!--
        false:立即加载,容器启动时就立即进行实例化		
 -->
<bean id="user30" class="com.atguigu.pojo.User" lazy-init="true">
    <property name="id" value="26"/>
    <property name="name" value="小村"/>
</bean>

5.注入

5.1 基于xml配置文件的自动注入

先创建Person类和Car类

public class Person {
    private Integer id;
    private String name;
    private String phone;
    private Integer age;
    private Car car;
    private List<String> list;
    private Map<String,Object> map;
    private Properties props;
    
    public Person(Car car){
        this.car = car;
    }
}

public class Car {
    private String name;
    private String carNo;
}
    <!--
 autowire 配置自动为子对象赋值
        no             默认值 ,也就是你不配置,子对象就没值

        byName          是指按子对象的属性名,做为id到Spring容器中查找Bean对象并赋值
                        1 如果找到就直接赋值
                        2 如果找不到就null值.

        byType          根据子对象的类型到Spring容器中查找,并赋值
                        1 找到一个就赋值
                        2 如果找不到,就null值
                        3 如果找到多个,就报错

        constructor     按构造器参数进行赋值.
                        1 先按构造器参数的类型来进行查找并赋值
                        2 如果找不到,就调用其他构造器
                        3 如果找到多个,就会再按照参数的名称做为id继续到Spring容器中查找并赋值
    -->
    <bean id="car1" class="com.atguigu.pojo.Car">
        <property name="type" value="捷豹"/>
        <property name="carNo" value="京A876"/>
    </bean>
    <bean id="car2" class="com.atguigu.pojo.Car">
        <property name="type" value="粪叉子"/>
        <property name="carNo" value="京A876"/>
    </bean>
    <bean id="person21" class="com.atguigu.pojo.Person" autowire="constructor">
        <property name="id" value="21"></property>
    </bean>

5.2、Bean的后置处理器之BeanPostProcessor

后置处理器,可以在对象创建初始化的前后,对对象做一些扩展,增强,等的操作

Bean的后置处理使用步骤:

1 先编写一个类去实现后置处理器接口

2 实现后置处理器的方法

3 去容器中配置后置处理器

实验23:测试bean的后置处理器

applicationContext.xml中的配置:

<!--bean会进入工厂-->
<bean id="person" class="com.atguigu.pojo.Person" init-method="init"
      destroy-method="destroy"></bean>
<!--配置bean的后置处理器-->
<!-- 配置到容器中就可以 -->
<bean id="myBeanPostProcessor" class="com.atguigu.processor.MyBeanPostProcessor"/>
<bean id="MyBeanFactoryPostProcessor" class="com.atguigu.processor.MyBeanFactoryPostProcessor"/>

后置处理器的代码:

public class MyBeanPostProcessor implements BeanPostProcessor {
    /**
     * postProcessBeforeInitialization()在初始化前执行
     * @param bean      正在创建的对象
     * @param beanName  对象的唯一标识
     * @return  正在初始化 ( 创建 ) 的对象
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.err.println("before:" + beanName);
        return bean ;
    }

    /**
     * postProcessBeforeInitialization()在初始化之后执行
     * @param bean      正在创建的对象
     * @param beanName  对象的唯一标识
     * @return  正在初始化 ( 创建 ) 的对象
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName);
        if ("person".equals(beanName)) {
            Person person = (Person) bean;
            person.setId(9527);
            person.setName("小小");
        }
        return bean;
    }
}
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("bean 工厂被创建了");
    }
}

6.Spring管理数据库连接池(重点)

6.1 Spring配置管理数据库连接池对象(重点)

先导入需要的jar包

druid-1.1.9.jar

junit_4.12.jar

mysql-connector-java-5.1.37-bin.jar

org.hamcrest.core_1.3.0.jar

spring-beans-5.2.5.RELEASE.jar

spring-context-5.2.5.RELEASE.jar

spring-core-5.2.5.RELEASE.jar

spring-expression-5.2.5.RELEASE.jar

spring-jcl-5.2.5.RELEASE.jar

applicationContext.xml 中的配置:

<!--
    使用Spring创建德鲁伊数据库连接池
-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
    <property name="username" value="root" />
    <property name="password" value="root" />
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/mbg" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="10" />
</bean>

test :

@Test
public void test17() throws SQLException {
    ApplicationContext applicationContext =
            new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
    System.err.println(dataSource.getConnection());
}

6.2 使用context名称空间加载jdbc.properties配置

jdbc.properties属性配置文件:

user=root
password=root
url=jdbc:mysql://localhost:3306/book
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10

application.xml配置

<!--
    Spring中有一个context名称空间,它可以用来做很多扫描操作
    其中一个是context:property-placeholder用来扫描加载properties属性配置文件

    location属性就是指定你要加载的properties属性配置文件路径
    classpath: 表示从类路径下开始查找文件
    classpath:jdbc.properties 表示从类路径下找jdbc.properteis属性配置文件加载
-->
<context:property-placeholder location="jdbc.properties"/>

<!--
    使用Spring创建德鲁伊数据库连接池
-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
    <property name="username" value="${user}"/>
    <property name="password" value="${password}"/>
    <property name="driverClassName" value="${driverClassName}"/>
    <property name="url" value="${url}"/>
    <property name="initialSize" value="${initialSize}"/>
    <property name="maxActive" value="${maxActive}"/>
</bean>

注意 jdbc.properties属性配置文件: 中不能用username,因为spring在解析的时候 , 还包含操作系统还有一些管理信息以及用户名.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WzUSGeV5-1608195664664)(C:\Users\psd\AppData\Roaming\Typora\typora-user-images\image-20201204232228347.png)]

DataSourceTest :

public class DataSourceTest {
    @Test
    public void test() throws SQLException {
        // 读取配置信息
        ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("bean.xml");
        // 指定id
       DataSource dataSource = application.getBean("dataSource", DataSource.class);
        System.err.println(dataSource.getConnection());
    }
}

6.3 Spring引入单独的jdbc.properties 配置文件(重点)

jdbc.properties:配置

user=root
password=root
url=jdbc:mysql://localhost:3306/mbg?characterEncoding=UTF-8
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10

application.xml : 配置

<!-- 加载指定的配置文件读取到spring的配置文件中 -->
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- 
        location : 加载文件
        classpath: 表示类路径下的 jdbc.properties配置文件
       
     -->
    <property name="location" value="classpath:jdbc.properties"/>
</bean>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${driverClassName}"/>
    <property name="username" value="${user}"/>
    <property name="password" value="${password}"/>
    <property name="url" value="${url}"/>
    <property name="initialSize" value="${initialSize}"/>
    <property name="maxActive" value="${maxActive}"/>
</bean>

DataSourceTest :

public class DataSourceTest {
    @Test
    public void test() throws SQLException {
        // 读取配置信息
        ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("bean.xml");
        // 指定id
       DataSource dataSource = application.getBean("dataSource", DataSource.class);
        System.err.println(dataSource.getConnection());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值