崇之他和她

导航

day03601SPRING5IOC

ioc

configapplactioncontext 被入口实现,

  • classpathXmlApplicationContext 读取配置文件方式解析注入对象

  • annotationConfigWebApplicationContext 通过注解获取

  • fileSystemXmlApplicationContext 通过文件获取解析

    加载application 解析的方式很多不止DI

    用xml文件方式解析

    bean 全部配置在xml中 对象由spring容器创建 供程序调用

    实体类中需要有get set

    spring中的对象在配置文件加载后生效

    alias 给bean 对象创建别名<id = "user" class= "sdf.dd.User"> <alias name = "user" alias= "uu"

    <import resource="aaa.xml" 导入引用文件,可以将三层不同的类注册卸载三类.xml文件中

DI 依赖注入

  • 构造器注入

  • set 注入 实体类中必须有set get 方法

  • <?xml version="1.0" encoding="UTF-8"?>
    <beans
            xmlns="http://www.springframework.org/schema/beans"
            xmlns:p="http://www.springframework.org/schema/p"
            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="user" class="com.zhq.bean.Stu">
            <property name="name" value="stuname"></property>
            <!--bean 注入-->
            <property name="address" ref="address"></property>
            <!--数组注入-->
            <property name="books">
                <array>
                    <value>水浒</value>
                    <value>猪八戒</value>
                    <value>狂神</value>
                </array>
            </property>
            <!--list注入-->
            <property name="li">
                <list>
                    <value>媳妇</value>
                    <value>小三</value>
                    <value>工作</value>
                </list>
            </property>
            <!--map注入-->
            <property name="map">
                <map>
                    <entry key="map1" value="00"></entry>
                    <entry key="map2ADDRESS" value ="sdfgdfg"></entry><!--如果时实体类可以value-ref引用-->
                </map>
            </property>
            <!--null注入-->
            <property name="nulll">
                <null></null><!--null-->
                
            </property>
    
    
        </bean>
    
    
        <bean id="address" class="com.zhq.bean.Address">
            <property name="str" value="我是地址类"></property>
        </bean>
    
    
        <!-- 配置,将实现类的对象创建交给Spring管理
            bean标签:
                id:给bean起个名字,唯一,不能重复,不能出现特殊字符,就看做根据面向接口编程new出来的实例
                class:类的全路径,底层根据全路径生成实例
         -->
        <!--<bean id="user" class="com.zhq.bean.User" name="uu ii ll">&lt;!&ndash;实体类中如果有有参构造必需要有构造函数&ndash;&gt;-->
            <!--&lt;!&ndash; 属性依赖注入,底层会自动封装,只要在dao实现类提供属性和set方法就行 &ndash;&gt;-->
            <!--<property name="str" value="张三"></property>-->
        <!--</bean>-->
        <!--<alias name="user" alias="uuuu"/>&lt;!&ndash;给user取别名,可以直接在bean标签上用name 属性取多个别名&ndash;&gt;-->
    </beans>
    
  • 拓展方式注入 c p 命名空间 需要在xml中导入命名规范

bean 的作用域

单例模式 singleton spring创建每个对象都是全局仅有一份的 hashcode 只有一个

原型模式 prototype 每new一个对象都会创建一个新的实例 hashcode 不同 浪费资源

工厂模式 BeanFactory loggerFactory

bean 的自动装配 无需手动在.xml中编码<bean 标签

三种spring装配方式

  • 在xml显示配置 手动
  • java中显示配置 java 工具类应用
  • 隐式自动装配
public class Person {//实体类
    private Cat cat;
    private Dog dog;
    private String name;
    @Autowired(required = false)//属性required默认true false表示可以为null
    private Rabite rabite; //实体类类型
    @Nullable //注解表示此字段可为null
    private String test;
<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        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
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="cat" class="Cat"/>
    <bean id="dog" class="Dog"/>
    <bean id="rabbit" class="Rabite"/>
    <!--<bean id="per" class="Person">--><!--非自动装配-->
        <!--<property name="name" value="xiaoxiao"/>-->
        <!--<property name="cat" ref="cat"/>-->
        <!--<property name="dog" ref="dog"/>-->
    <!--</bean>-->
    <!--byName 会自动查找和属性名相同的Bean  id = 属性名 保证bean 标签中的id 唯一-->
    <!--byType 会自动查找和类型相同的Bean 但是属性类型不能重复 -->
    <!--<bean id="per" class="Person" autowire="byType">-->
    <bean id="per" class="Person" autowire="byName">
        <property name="name" value="xiaoxiao"/>
        <!--<property name="cat" ref="cat"/>-->
        <!--<property name="dog" ref="dog"/>-->
    </bean>
    <!--jdk5之后配置注解支持便可以在实体类中用注解形式注入其他类型-->
    <!--1.导入约束 增加命名空间context  2.配置支持注解开启注解功能 -->
    <context:annotation-config/>
</beans>
@Resource  通过byname默认 和 bytype 去找
@Autowired 通过bytype 去找  需要保证对象必须存在

posted on 2021-02-25 19:59  崇之他和她  阅读(65)  评论(0编辑  收藏  举报