Spring基础框架 二 注解

目录

一  Spring 管理第三方资源

1.1 管理DataSource连接池对象

1.2 管理c3p0连接池

1.3 加载properties文件

二  Spring容器

三  核心容器总结

四  IOC/DI 注解开发 ( 重要 )

4.1 注解开发定义bean

4.2 纯注解开发模式

4.3 注解开发bean作用范围与生命周期管理

4.4 注解开发依赖注入 (自动装配)

五  IOC/DI注解开发管理第三方bean

5.1 注解开发实现为第三方bean注入资源

六  注解开发总结

七  Spring整合Mybatis (重要)

 八  Spring整合Junit


一  Spring 管理第三方资源

1.1 管理DataSource连接池对象

数据库准备

create database if not exists spring_db character set utf8;
use spring_db;
create table if not exists tbl_account(
    id int primary key auto_increment,
    name varchar(20),
    money double
);

insert into tbl_account values(null,'Tom',1000);
insert into tbl_account values(null,'Jerry',1000);

【第一步】添加Druid连接池依赖  MySQL依赖  Spring依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
<dependency>  
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.10.RELEASE</version>
</dependency>

【第二步】配置DruidDataSource连接池Bean对象

说明:

  • driverClassName:数据库驱动
  • url:数据库连接地址
  • username:数据库连接用户名
  • password:数据库连接密码
  • 数据库连接的四要素要和自己使用的数据库信息一致。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

【第三步】在测试类中从IOC容器中获取连接池对象并打印

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);
    }
}

1.2 管理c3p0连接池

【第一步】添加c3p0连接池依赖

<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>

【第二步】配置c3p0连接池Bean对象

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_db"/>
    <property name="user" value="root"/>
    <property name="password" value="root"/>
    <property name="maxPoolSize" value="1000"/>
</bean>

【第三步】在测试类中从IOC容器中获取连接池对象并打印

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);
    }
}

1.3 加载properties文件

上节中我们已经完成两个数据源druidC3P0的配置,但是其中包含了一些问题,我们来分析下:

  • 这两个数据源中都使用到了一些固定的常量如数据库连接四要素,把这些值写在Spring的配置文件中不利于后期维护

  • 需要将这些值提取到一个外部的properties配置文件中

  • Spring框架如何从配置文件中读取属性值来配置就是接下来要解决的问题。

第三方bean属性优化 实现思路

需求:将数据库连接四要素提取到properties配置文件,spring来加载配置信息并使用这些信息来完成属性注入。

1.在resources下创建一个jdbc.properties(文件的名称可以任意)

2.将数据库连接四要素配置到配置文件中

3.在Spring的配置文件中加载properties文件

4.使用加载到的值实现属性注入

其中第3,4步骤是需要大家重点关注,具体是如何实现。

步骤1:准备properties配置文件

resources下创建一个jdbc.properties文件,并添加对应的属性键值对

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=root

步骤2:开启context命名空间

在applicationContext.xml中开context命名空间

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

步骤3:加载properties配置文件

在配置文件中使用context命名空间下的标签来加载properties配置文件

  加载properties文件写法标准写法该怎么写?

  • 不加载系统属性   <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>

  • 加载多个properties文件   <context:property-placeholder location="jdbc.properties,msg.properties"/>

  • 加载所有properties文件  <context:property-placeholder location="*.properties"/>

  • 加载properties文件 标准格式 classpath:代表的是从根路径下开始查找,但是只能查询当前项目的根路径  <context:property-placeholder location="classpath:*.properties"/>

  • 不仅可以加载当前项目还可以加载当前项目所依赖的所有项目的根路径下的properties配置文件   <context:property-placeholder location="classpath*:*.properties"/>

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

步骤4:完成属性注入

使用${key}EL表达式获取来读取properties配置文件中的内容并完成属性注入 , 至此,读取外部properties配置文件中的内容就已经完成。

<?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/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:property-placeholder location="jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans>

二  Spring容器

完成bean与依赖注入的相关知识学习,接下来主要学习的是IOC容器中的==核心容器==。

这里所说的核心容器,可以把它简单的理解为ApplicationContext,前面已经用到过,但是并没有系统的学习,接下来从以下几个问题入手来学习下容器的相关知识:

  • 如何创建容器?

  • 创建好容器后,如何从容器中获取bean对象?

  • 容器类的层次结构是什么?

  • BeanFactory是什么?

容器的创建方式

  • 方式一 : 类路径加载配置文件         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  • 方式二 : 文件路径加载配置文件    ApplicationContext ctx = new FileSystemXmlApplicationContext("D:\\applicationContext.xml"); 
  • 加载多个配置文件 (了解)               ApplicationContext ctx = new ClassPathXmlApplicationContext("bean1.xml" , "bean2.xml"); 

Bean的三种获取方式

  • 方式一 : 使用bean名称获取(需要强转)                                 BookDao bookDao = (BookDao) ctx.getBean("bookDao");
  • 方式二 : 使用bean名称获取并指定类型(解决不需要强转)    BookDao bookDao = ctx.getBean("bookDao",BookDao.class);
  • 方式三 : 使用bean类型获取 (必须要确保IOC容器中该类型对应的bean对象只能有一个)     BookDao bookDao = ctx.ge
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值