sbs spring

Posted khlbat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sbs spring相关的知识,希望对你有一定的参考价值。

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>

 

以上是关于sbs spring的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 ffmpeg overlay_cuda 过滤器制作 SBS 视频?

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

Spring boot:thymeleaf 没有正确渲染片段

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段