springIoC的基本配置使用

2023-05-19  本文已影响0人  ChinaGoodStaff

1.容器概述

ApplicationContext是Spring IoC容器实现的代表,它负责实例化,配置 和组装Bean

1.1.配置元数据
1.1.1使用xml配置

最简单、直观、全面的方式;适合入门

1.1.2基于注解的配置

注解+xml的形式
@Compont(@serivce @controller @repository) @Autowride
Spring 2.5 支持基于注解的元数据配置. 主要是SSM框架开发中的使用

1.1.3基于java的配置

@Confiration @Bean @Import
从 Spring 3.0开始, 由Spring JavaConfig项目提供的功能已经成为Spring核心框架的一部分。因此,你可以使用Java配置来代替 XML配置定义外部bean
从spring4.0开始支持springboot1.0 之后 springboot
完全采用javaConfig的方式进行开发

1.2容器的实例化

对象在容器创建完成的时候已经创建,而且只创建一次

1.3容器的使用

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

        //创建spring上下文 加载所有的bean
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring_ioc.xml");
        System.out.println("Spring容器已加载");
        //1.通过class获取bean
        User user = applicationContext.getBean(User.class);
        String name = user.getRealName();

2.bean概述

2.1命名Bean

bean的名称

<beanclass="com.ll.entity.User"id="user"name="user2user3,user4;us er5"></bean>

为外部定义的bean起别名
<aliasname="user"alias="user6"></alias>

2.2实例化Bean
2.2.1使用构造函数

默认使用无参构造函数,无法干预实例话过程

2.2.2使用静态工厂方法

spring_ioc.xml中

 <!--    使用静态工厂实例话bean-->
 <bean class="cn.lll.beans.Person" id="person2" factory-method="createPersonFactory"></bean>

Person.java中

public static Person createPersonFactory(){
     Child child = new  Child();
     child.setName("son");
     return child;
 }
2.2.3使用实例工厂方法

spring_ioc.xml中

<!--使用抽象工厂实例话bean-->
    <bean class="cn.lll.beans.PersonFactory" id="personFactory"></bean>
    <bean class="cn.lll.beans.Person" id="person3" factory-bean="personFactory" factory-method="createPersonFactoryMethod"></bean>

需要创建一个personFactory工厂bean

public class PersonFactory {

    public Person createPersonFactoryMethod(){
        Child child = new Child();
        child.setName("儿子");
        return child;
    }
}

3.Bean的作用域scope

singleton 默认值:单例 只会在Ioc容器种创建一次
prototype 多例(原型bean) 每次获取都会new一次新的<bean />
request
session
application
websocket

4.依赖

依赖注入
基于setter

属性必须声明了set方法
注意:name属性对应的set方法的名字 比如 setId -> name="id" setXxx ->
name="xxx"

    <bean class="cn.lll.beans.User" id="user7">
        <property name="id" value="1"></property>
        <property name="userName" value="xry"></property>
        <property name="realName" value="流川枫"></property>
    </bean>
基于构造函数

1.基于那么属性设置构造函数
2.可以只有value属性,
3.不使用name根据参数顺序赋值
4.如果参数错乱,
可以使用name
可以用index,下标从0开始
还可以使用type:在错乱的参数类型一致情况下不能使用
最终都是用那么使用,比较清晰 显而易见

    <bean class="cn.lll.beans.User" id="user8">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="realName" value="流川枫"></constructor-arg>
        <constructor-arg name="userName" value="xry"></constructor-arg>
    </bean>
依赖和配置的细节

直接值(基本类型、String等)

bean class="cn.lll.beans.Person" id="person">
    <property name="id" value="1"></property>
</bean>

对其他bean的引用(装配)

bean class="cn.lll.beans.Person" id="person">
    <property name="wife" ref="wife"></property>
</bean>

内部bean

bean class="cn.lll.beans.Person" id="person">
    <property name="wife">
            <bean class="cn.lll.beans.Wife">
                <property name="name" value="LYF"></property>
                <property name="age" value="33"></property>
            </bean>
        </property>
</bean>

集合

bean class="cn.lll.beans.Person" id="person">
    <property name="hobbies">
            <list>
                <value>抽烟</value>
                <value>喝酒</value>
                <value>烫头</value>
            </list>
        <property name="course">
            <map>
                <entry key="1" value="java"></entry>
                <entry key="2" value="mysql"></entry>
            </map>
    </property>
</bean>

null和空的字符串

bean class="cn.lll.beans.Person" id="person">
    <property name="name" >
            <null></null>
        </property>
    <property name="gender" value=""></property>
</bean>

使用p命名空间简化基于setter属性注入xml配置;用的比较少
p按alt+Enter引入命名空间
不支持集合

    <bean class="cn.lll.beans.Wife" id="wife2" p:name="王泽嘛" p:age="30"></bean>

使用c命名空间简化基于构造函数的xml;用的比较少
c按alt+Enter引入命名空间
不支持集合

    <bean class="cn.lll.beans.Wife" id="wife3" c:name="xry" c:age="24"></bean>
depends-on属性

加载顺序默认从上向下加载的
depends-on wife 会先加载wife 当一个bean想让另一个备案在他之前加载 可以设置

        <bean class="cn.lll.beans.User" id="user" depends-on="wife"></bean>
        <bean class="cn.lll.beans.Wife" id="wife"></bean>
懒加载

不想在spring容器加载时候加载bean;而是在使用的时候才会加载bean

        <bean class="cn.lll.beans.Wife" id="wife2" lazy-init="true"></bean>

5.自动注入

通过setter自动注入

byName 根据set方法的名字去匹配
byType,根据类型自动匹配,当出现多个类型或者没有匹配到会报错

   <bean class="cn.lll.beans.Person" id="person4" autowire="byName"></bean>
   <bean class="cn.lll.beans.Wife" id="wife" primary="true">
       <property name="name">
           <value>地理特别</value>
       </property>
   </bean>
通过构造函数自动注入

优先根据参数名字匹配,如果名字没有匹配到,会根据参数的类型匹配
会根据构造函数的参数一个完整的自动注入,如果构造函数中Person(User user,Wife wife),ioc容器里必须有两个完整的bean
名字没有匹配到 或根据type进行匹配,如果没有匹配到会注入失败,但不会报错
当根据类型匹配到多个:
设置某个bean为主要bean primary="true"
可以设置不被注入的bean autowire-candidate="true"

    <bean class="cn.lll.beans.Wife" id="wife2" autowire-candidate="true">
        <property name="name">
            <value>地理特别2</value>
        </property>
    </bean>

6.自定义Bean的特性

生命周期回调
初始化方法回调

实现InitializingBean接口

销毁方法回调

实现DisposableBean接口

7.创建第三方Bean

  <bean class="com.alibaba.druid.pool.DruidDataSource" name="druidDataSource">
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="url" value="jdbc:localhost:3306/demo"></property>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver">  </property>
</bean>

8.引入外部属性文件

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

9.SpEl的使用


上一篇 下一篇

猜你喜欢

热点阅读