02-SpringIOC配置详解

大家好,我是徐庶老师,专注java,想要学习java的同学可以欢迎关注我。

结合视频观看效果更佳哦:2022最新Spring5入门到源码【完整资料+源码】_哔哩哔哩_bilibili

目录

IOC配置详解

1.2. 容器概述

1.2.1. 配置元数据

1.2.2. 容器的实例化

1.2.3. 容器的使用

1.3. bean的概述

1.3.1. 命名bean

1.4. 依赖

1.4.1. 依赖注入

1.4.2. 依赖和配置的细节

1.4.3. 使用 depends-on属性

1.4.4. 懒加载bean

1.4.5. 自动注入

1.5. Bean 的作用域

1.5.1. Singleton(单例)的作用域

1.5.2. Prototype(原型)的作用域 

1.6. 自定义bean的特性

1.6.1. 生命周期回调

1.7. Bean 定义的继承

spring创建第三方bean对象

spring引用外部配置文件

SpEL的使用


IOC配置详解

1.2. 容器概述

ApplicationContext是Spring IoC容器实现的代表,它负责实例化,配置和组装Bean。容器通过读取配置元数据获取有关实例化、配置和组装哪些对象的说明 。配置元数据可以使用XML、Java注解或Java代码来呈现。它允许你处理应用程序的对象与其他对象之间的互相依赖关系。

1.2.1. 配置元数据

  • 使用xml的配置

特点:简单、直观 适合入门

常用注解:@Compont(@serivce @controller @repository) @Autowride

特点:Spring 2.5 支持基于注解的元数据配置. SSM框架开发中的使用

常用注解:@Confiration @Bean @Import

特点:从 Spring 3.0开始, 由Spring JavaConfig项目提供的功能已经成为Spring核心框架的一部分。因此,你可以使用Java配置来代替XML配置定义外部bean .

从spring4.0开始支持springboot1.0 之后 springboot完全采用javaConfig的方式进行开发。

1.2.2. 容器的实例化

对象在Spring容器创建完成的时候就已经创建完成,不是需要用的时候才创建

1.2.3. 容器的使用

ApplicationContext是能够创建bean定义以及处理相互依赖关系的高级工厂接口,使用方法T getBean(String name, Class requiredType)获取容器实例。

// 创建spring上下文 加载所有的

// 创建spring上下文  加载所有的bean
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

// 获取bean
PetStoreService service = context.getBean("petStore", PetStoreService.class);

// 使用bean的对象
List<String> userList = service.getUsernameList();

1.3. bean的概述

1.3.1. 命名bean

<bean class="com.tuling.entity.User" id="user" name="user2 user3,user4;user5"></bean>
<alias name="user" alias="user6"></alias>

1.3.2. 实例化Bean

缺点:无法干预实例化过程

<bean class="com.tuling.service.impl.UserServiceImpl" id="userService2"
      factory-method="createUserServiceInstance"   >
</bean>

 可以在createUserServiceInstance方法中自由控制实例化过程

public static  UserServiceImpl createUserServiceInstance(){
    return new UserServiceImpl();
}

<bean class="com.tuling.service.impl.UserServiceImpl" id="userService"
      factory-bean="serviceFactory"
      factory-method="createUserService" >
</bean>
public class createUserService{

    public UserServiceImpl createUserFactory(){
        return new UserServiceImpl();
    }
}

<!--基于setter方法的依赖注入
    1. 属性必须声明了set方法
    2. name是根据set方法的名字来的  比如方法名字是: setIdxx   ->  name="idxx"
-->
<bean class="cn.tulingxueyuan.beans.User" id="user2">
    <property name="idtuling" value="1"></property>
    <property name="username" value="zhangsan"></property>
    <property name="realname" value="张三"></property>
</bean>

<!--基于构造函数的依赖注入
    1. 将会调用自定义构造函数来实例化对象,就不会调用默认的无参构造函数
    2. name是根据构造函数的参数名来的,  比如:User(String idxx) -> name="idxx"
    3. name属性可以省略 但是要注意参数的位置 
    4. 如果非要把位置错开 可以使用 name 或者 index 或者 type
    5. index 是下标  从0开始
    6. type 在位置错开情况下只能在类型不一样的时候指定才有明显效果
-->
<bean class="cn.tulingxueyuan.beans.User" id="user3">
    <constructor-arg name="username"  value="lisi"></constructor-arg>
    <constructor-arg name="id_xushu"  value="1"></constructor-arg>
    <constructor-arg name="realname" value="李四"></constructor-arg>
</bean>

1.4.2. 依赖和配置的细节

<!--复杂数据类型-->
<bean class="cn.tulingxueyuan.beans.Person" id="person" p:wife-ref="wife2">
    <property name="id" value="1"></property>
    <property name="realName" value=""></property>
    <!--设置null值-->
    <property name="name">
        <null></null>
    </property>
    <!--当依赖其他bean: 内部bean   inner bean
    <property name="wife">
        <bean class="cn.tulingxueyuan.beans.Wife" >
            <property name="age" value="18"></property>
            <property name="name" value="迪丽热巴"></property>
        </bean>
    </property>-->
    <!--当依赖其他bean: 引用外部bean
    <property name="wife" ref="wife"></property>-->
    <property name="birthday" value="2020/05/20"></property>
    <property name="hobbies">
        <list>
            <value>唱歌</value>
            <value>跳舞</value>
            <!--如果List的泛型是比如:List<Wife> <bean>-->
        </list>
    </property>
    <property name="course" >
        <map>
            <entry key="1" value="JAVA"> </entry>
            <entry key="2" value="HTML"> </entry>
        </map>
    </property>

</bean>

<!--可以使用p命名空间来简化基于setter属性注入   它不支持集合-->
<bean class="cn.tulingxueyuan.beans.Wife" id="wife" p:age="18" p:name="迪丽热巴"  >
</bean>

<!--可以使用c命名空间来简化基于构造函数属性注入   它不支持集合-->
<bean class="cn.tulingxueyuan.beans.Wife" id="wife2"  c:age="20" c:name="xxx">
   <!-- <constructor-arg name="age" value="18"></constructor-arg>-->
</bean>
<!--使用depends-on可以设置先加载的Bean   也就是控制bean的加载顺序-->
<bean class="cn.tulingxueyuan.beans.Person" id="person" depends-on="wife"></bean>
<bean class="cn.tulingxueyuan.beans.Wife" id="wife"></bean>

<!--使用lazy-init设置懒加载
默认为false: 在spring容器创建的时候加载(实例化)
      true: 在使用的时候(getBean)才会去加载(实例化)-->
<bean class="cn.tulingxueyuan.beans.Person" id="person2" lazy-init="true">
    <property name="id" value="1"></property>
    <property name="realName" value="吴彦祖"></property>
    <property name="name" value="徐庶"></property>
</bean>

当一个对象中需要引用另外一个对象的时候,在之前的配置中我们都是通过property标签来进行手动配置的,其实在spring中还提供了一个非常强大的功能就是自动装配,可以按照我们指定的规则进行配置,配置的方式有以下几种:

  • default/no:不自动装配
  • byName:按照名字进行装配,以属性名作为id去容器中查找组件,进行赋值,如果找不到则装配null
  • byType:按照类型进行装配,以属性的类型作为查找依据去容器中找到这个组件,如果有多个类型相同的bean对象,那么会报异常,如果找不到则装配null
  • constructor:按照构造器进行装配,先按照有参构造器参数的类型进行装配,没有就直接装配null;如果按照类型找到了多个,那么就使用参数名作为id继续匹配,找到就装配,找不到就装配null

通过将autowire-candidate 属性设置为false,避免对bean定义进行自动装配,如下一节所述

通过将其 元素的primary属性设置为 true,将单个bean定义指定为主要候选项。

<!--**************************************基于xml自动注入 begin**************************************************-->

<!--自动注入:
1. bytype 根据类型自动注入(spring会根据bean里面的所有对象属性的类型,只要它匹配到bean里面某一个类型跟属性类型吻合就会自动注入
2. byname 会根据属性setxxx的名字来自动匹配 (spring会根据bean里面的所有对象属性的set的名字,只要它匹配到bean里面某一个名字跟属性名字吻合就会自动注入
3. constructor 优先根据名字来找, 如果名字没有匹配到根据类型来匹配,  如果类型匹配到多个则不会自动注入
注意:bytype  如果匹配到两个同样的类型会出现错误,所以一定要保证ioc容器里面只有一个对应类型的bean
      byname  最能匹配到唯一的那个bean
      constructor 保证构造函数不能包含多余的其他参数
 default:不会进行自动注入

<bean class="cn.tulingxueyuan.beans.Person" id="person6" autowire="constructor" >
    <property name="id" value="1"></property>
    <property name="realName" value="吴彦祖"></property>
    <property name="name" value="徐庶"></property>
 </bean>

<bean class="cn.tulingxueyuan.beans.Wife" id="wife" p:age="18" p:name="迪丽热巴"  >
</bean>
<bean class="cn.tulingxueyuan.beans.Wife" id="QBL" p:age="60" p:name="乔碧螺"  >
</bean>
 **************************************基于xml自动注入 end**************************************************-->
<!--作用域scope
singleton 默认:单例  只会在Ioc容器种创建一次
prototype 多例(原型bean) 每次获取都会new一次新的bean-->

<bean class="cn.tulingxueyuan.beans.Person" id="person3" scope="prototype">
    <property name="id" value="1"></property>
    <property name="realName" value="吴彦祖"></property>
    <property name="name" value="徐庶"></property>
</bean>
/**
 * 生命周期回调
 * 1. 使用接口实现的方式来实现生命周期的回调:
 *      1.1   初始化方法: 实现接口: InitializingBean  重写afterPropertiesSet方法   初始化会自动调用的方法
 *      1.1   销毁的方法: 实现接口: DisposableBean  重写destroy 方法   销毁的时候自动调用方法
 *      什么时候销毁:在spring容器关闭的时候  close()
 *                   或者 使用ConfigurableApplicationContext.registerShutdownHook方法优雅的关闭
 *
 * 2. 使用指定具体方法的方式实现生命周期的回调:
 *    在对应的bean里面创建对应的两个方法
 *   init-method="init" destroy-method="destroy"
 */
<!--bean的继承  一个bean继承另一个bean
可以使用parent属性指定父类bean
如果想让父类bean不能被实例化  abstract="true"

<bean class="cn.tulingxueyuan.beans.Person" id="person4"  abstract="true">
    <property name="id" value="1"></property>
    <property name="realName" value="吴彦祖"></property>
    <property name="name" value="徐庶"></property>
</bean>

<bean class="cn.tulingxueyuan.beans.Person" id="person5"  parent="person4" >
    <property name="realName" value="刘德华"></property>
</bean>-->

以上建议在《Spring源码》章节中进行学习

spring创建第三方bean对象

在Spring中,很多对象都是单实例的,在日常的开发中,我们经常需要使用某些外部的单实例对象,例如数据库连接池,下面我们来讲解下如何在spring中创建第三方bean实例。

1、导入数据库连接池的pom文件

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</version>
</dependency>

2、编写配置文件

ioc.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 id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
       <property name="username" value="root"></property>
       <property name="password" value="123456"></property>
       <property name="url" value="jdbc:mysql://localhost:3306/demo"></property>
       <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
   </bean>
</beans>

3、编写测试文件

​  

public class MyTest {
   public static void main(String[] args) throws SQLException {
       ApplicationContext context = new ClassPathXmlApplicationContext("ioc3.xml");
       DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);
       System.out.println(dataSource);
       System.out.println(dataSource.getConnection());
  }
}

spring引用外部配置文件

在resource中添加dbconfig.properties

username=root
password=123456
url=jdbc:mysql://localhost:3306/demo
driverClassName=com.mysql.jdbc.Driver

编写配置文件

<?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命名空间
-->
   <context:property-placeholder location="classpath:dbconfig.properties"/>
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
       <property name="username" value="${username}"></property>
       <property name="password" value="${password}"></property>
       <property name="url" value="${url}"></property>
       <property name="driverClassName" value="${driverClassName}"></property>
   </bean>
</beans>

SpEL的使用

SpEL:Spring Expression Language,spring的表达式语言,支持运行时查询操作对象

使用#{...}作为语法规则,所有的大括号中的字符都认为是SpEL.

<bean id="user" class="cn.tulingxueyuan.entity.User">
       <!--支持任何运算符-->
       <property name="id" value="#{12*2}"></property>
       <!--可以引用其他bean的某个属性值-->
       <property name="name" value="#{address.province}"></property>
       <!--引用其他bean-->
       <property name="role" value="#{address}"></property>
       <!--调用静态方法-->
       <property name="hobbies" value="#{T(java.util.UUID).randomUUID().toString().substring(0,4)}"></property>
       <!--调用非静态方法-->
       <property name="gender" value="#{address.getCity()}"></property>
</bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值