zoukankan      html  css  js  c++  java
  • sbs spring ioc

    TODO

    ref 的 parent 与 local 属性

    ioc的三种类型

    • 构造函数注入
    • 属性注入
    • 接口注入

    spring的注入类型

    • 构造函数注入
    • 属性注入
    • 工厂方法注入

    属性注入

    <!-- 通过属性注入 -->
    <bean id="user" class="com.laolang.sstudy.ioc.domain.User">
        <property name="id" value="1001" />
        <property name="age" value="23" />
        <property name="name" value="laolang" />
    </bean>

    根据JavaBean关于属性命名的特殊规范,变量的前两个字母要么全部大写,要么全部小写

    要提供相应的setter方法

    构造函数注入

    构造函数

    public User(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    按类型匹配入参

    <!-- 按类型匹配入参 -->
    <bean id="user2" class="com.laolang.sstudy.ioc.domain.User">
        <constructor-arg type="java.lang.Long">
            <value>1001</value>
        </constructor-arg>
        <constructor-arg type="java.lang.String">
            <value>xiaodaima</value>
        </constructor-arg>
        <constructor-arg type="java.lang.Integer">
            <value>23</value>
        </constructor-arg>
    </bean>

    按索引匹配入参

    <!-- 按索引匹配入参 -->
    <bean id="user3" class="com.laolang.sstudy.ioc.domain.User">
        <constructor-arg index="0" value="1001" />
        <constructor-arg index="1" value="tianya" />
        <constructor-arg index="2" value="23" />
    </bean>

    联合使用类型和索引匹配入参

    如果有两个构造函数的入参数目相同,则可以同时使用类型和索引匹配

    public Student(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    
    public Student(Long id, String name, Long number) {
        this.id = id;
        this.name = name;
        this.number = number;
    }
        <!-- 联合使用类型和索引匹配入参 -->
        <bean id="student" class="com.laolang.sstudy.ioc.domain.Student">
            <constructor-arg index="0" type="java.lang.Long">
                <value>1001</value>
            </constructor-arg>
            <constructor-arg index="1" type="java.lang.String">
                <value>student</value>
            </constructor-arg>
            <constructor-arg index="2" type="java.lang.Integer">
                <value>23</value>
            </constructor-arg>
        </bean>
        <bean id="student2" class="com.laolang.sstudy.ioc.domain.Student">
            <constructor-arg index="0" type="java.lang.Long">
                <value>1001</value>
            </constructor-arg>
            <constructor-arg index="1" type="java.lang.String">
                <value>student</value>
            </constructor-arg>
            <constructor-arg index="2" type="java.lang.Long">
                <value>14061001</value>
            </constructor-arg>
        </bean>

    工厂方法注入

    非静态工厂方法

    工厂

    package com.laolang.sstudy.ioc.factory;
    
    import com.laolang.sstudy.ioc.domain.Student;
    
    public class StudentFactory {
    
        public Student createStudent(){
            Student student = new Student();
            student.setId(1001L);
            student.setAge(23);
            student.setName("student by factory without param");
            student.setNumber(14061001L);
            return student;
        }
    
        public Student createStudent( Long id , String name){
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            return student;
        }
    }

    xml

    <!-- 非静态工厂方法 -->
    <!-- 工厂类 bean -->
    <bean id="studentFactory" class="com.laolang.sstudy.ioc.factory.StudentFactory" />
    <!--
        factory-bean : 指定工厂类bean
        factory-method : 指定创建 bean 的方法
    -->
    <bean id="student3" factory-bean="studentFactory" factory-method="createStudent" />
    <!-- 带参数的工厂方法 -->
    <bean id="student4" factory-bean="studentFactory" factory-method="createStudent">
        <constructor-arg index="0" value="1001" />
        <constructor-arg index="1" value="student by factory with param" />
    </bean>

    静态工厂方法

    工厂

    package com.laolang.sstudy.ioc.factory;
    
    import com.laolang.sstudy.ioc.domain.Student;
    
    public class StudentStaticFactory {
    
        public static Student createStudent(){
            Student student = new Student();
            student.setId(1001L);
            student.setAge(23);
            student.setName("student by static factory without param");
            student.setNumber(14061001L);
            return student;
        }
    
        public static Student createStudent( Long id , String name){
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            return student;
        }
    }

    xml

    <!-- 静态方法工厂 无参-->
    <bean id="student5" class="com.laolang.sstudy.ioc.factory.StudentStaticFactory" factory-method="createStudent" />
    <!-- 静态方法工厂 带参-->
    <bean id="student6" class="com.laolang.sstudy.ioc.factory.StudentStaticFactory" factory-method="createStudent" >
        <constructor-arg index="0" value="1001" />
        <constructor-arg index="1" value="student by static factory with param" />
    </bean>

     注入 Date 类型

    <!-- 注入 date 类型 -->
    <bean id="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd" />
    </bean>
    <bean id="user4" class="com.laolang.sstudy.ioc.domain.User">
        <property name="birthday">
            <bean factory-bean="dateFormat" factory-method="parse">
                <constructor-arg value="1991-12-06" />
            </bean>
        </property>
    </bean>

    参考:Sping注入Date类型的3种方式

    注入 null 值

    <!-- 注入 null 值 -->
    <bean id="user5" class="com.laolang.sstudy.ioc.domain.User">
        <property name="id" value="1001" />
        <property name="age" value="23" />
        <property name="birthday"><null /></property>
        <property name="name"><null /></property>
    </bean>

    引用其他 bean

    ref*

    ref 的 parent与local属性

    <!-- 引用其他 bean  -->
    <bean id="driver" class="com.laolang.sstudy.ioc.domain.Driver">
        <property name="name" value="driver one" />
    </bean>
    <bean id="car" class="com.laolang.sstudy.ioc.domain.Car">
        <property name="brand" value="红旗CA72" />
        <property name="maxSpeed" value="200" />
        <property name="price" value="123.456" />
        <property name="driver" ref="driver" />
    </bean>

    内部 bean

    <!-- 内部 bean  -->
    <bean id="car2" class="com.laolang.sstudy.ioc.domain.Car">
        <property name="brand" value="桑塔纳2000" />
        <property name="maxSpeed" value="200" />
        <property name="price" value="456.123" />
        <property name="driver">
            <bean class="com.laolang.sstudy.ioc.domain.Driver">
                <property name="name" value="driver two" />
            </bean>
        </property>
    </bean>

    外部配置文件

    <!-- 导入配置文件 -->
    <!-- 导入单个配置文件 -->
    <!--<context:property-placeholder location="classpath:user.properties" />-->
    <!-- 导入多个配置文件 -->
    <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- 允许JVM参数覆盖 -->
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <!-- 忽略没有找到的资源文件 -->
        <property name="ignoreResourceNotFound" value="true" />
        <!-- 配置资源文件 -->
        <property name="locations">
            <list>
                <value>classpath:user.properties</value>
            </list>
        </property>
    </bean>
    khl
  • 相关阅读:
    C++ 字符串与数字之间的转换
    两种常见的模式匹配算法(代码实现)
    C++ string整行读取带空格的字符串
    JavaEE(一)开发环境搭建(JDK+Eclipse+Tomcat+Mysql+Spring)
    25java模拟容器的实现
    24java的StringBuilder类和StringBuffer类
    23java的String类常用方法
    22java的回调&内部类
    21java的抽象类和接口
    20java的组合&IDEA踩坑合集1
  • 原文地址:https://www.cnblogs.com/khlbat/p/8325668.html
Copyright © 2011-2022 走看看