Spring IOC(二)

创建bean对象

Student类:

class Student{
    private String name;
    private String className;
    private Integer age;
    private Date now;
    public Student(){}
    public Student(String name,String className,Integer age,Date now){
        this.name=name;
        this.className=className;
        this.age=age;
        this.now=now;
    }
    //省略setter和getter
}

1、无参构造方法

<bean id="student1" class="com.mystudy.entity.Student"></bean>

2、有参构造方法

<bean id="now" class="java.lang.Date"></bean>
<bean id="student" class="com.mystudy.entity.Student">
	<constructor-arg name="name" value="张三"/>
    <constructor-arg name="className" value="五年3班"/>
    <constructor-arg name="age" value="12"/>
    <constructor-arg name="current" ref="now"/>
</bean>

有参构造方法,借助constructor-arg标签注入值

(1)往哪里注入

name:形参名,常用,形参名

index:形参数组下标,从0开始。不常用

type:形参类型。不常用

在spring中,类型和类名一般指全路径类名

(2)注入的值

value:直接注入基本数据类型或字符串的值

ref:引入其它bean

3、工厂静态方法

也指普通静态方法

工厂类:

class StudentFactory{
    static Student getStudent(){
        return new Student();
    }
}

spring配置:

<bean id="user3" class="com.mystudy.entity.StudentFactory" factory-method="getStudent">

4、工厂方法

也指普通方法

工厂类:

class StudentFactory{
    Student getStudent2(){
        return new Student();
    }
}

spring配置:

<bean id="studentFactory" class="com.mystudy.entity.StudentFactory"></bean>
<bean id="user4" class="com.mystudy.entity.Student" factory-bean="studentFactory" factory-method="getStudent2">

注入属性值

1、有参构造方法

2、set方法

<bean id="now" class="java.lang.Date"></bean>
<bean id="student" class="com.mystudy.entity.Student">
	<property name="name" value="张三"/>
    <property name="className" value="五年3班"/>
    <property name="age" value="12"/>
    <property name="current" ref="now"/>
</bean>

set方法,借助property标签注入值。
set方法,需要默认构造方法和每个属性的set方法。
(1)往哪里注入
name:形参名
set方法没有index和type属性
(2)注入的值
value:直接注入基本数据类型或字符串的值
ref:引入其它bean
3、p命名空间
本质还是调用set方法

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user6" class="com.tj.entity.User"
        p:name="张三" p:className="五年3班" p:age="12" p:current-ref="now" >
    </bean>
    <bean id="now" class="java.lang.Date"></bean>
</beans>
  • p命名空间需要引入单独的一条约束
  • p:username直接注入,p:username-ref注入其它bean对象

4、SpEL

 <property name="username" value="#{表达式}"/>

数字值和字符串值:#{123}, #{‘zhangsan’}
引用其他bean值:#{bean的id},注意这里仍然是为value赋值而不是ref
引用其他bean的属性:#{bean的id.属性名}
执行其他bean的方法:#{bean的id.方法名}
引用静态方法或属性:#{T(类路径).属性名/方法名}

5、自动装配

前提:需要给对应的属性设置set方法
针对单个bean的配置
如果当前People类中的属性名和某个bean的id名一致,那么Spring会自动将bean对象赋值给属性

<bean id="student7" class="com.yaorange.entity.Student" autowire="byName"/>

如果当前People类中的属性的数据类型和某个bean的Class一致,那么Spring会自动将bean对象赋值给属性

<bean id="student8" class="com.yaorange.entity.Student" autowire="byType"/>

提示:基于类型的自动注入必须保证在整个Spring容器中只有唯一的一个bean类型满足要求
配置全局(针对所有bean的配置)

<?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" default-autowire="byType">

注入集合

注入集合数据
List结构的: array,list,set
Map结构的 map,entry,props,prop
只要结构相同,标签可以互换

<!-- 给数组注入数据 --> 
<property name="myStrs"> 
    <set> 
        <value>AAA</value> 
        <value>BBB</value> 
        <value>CCC</value> 
    </set> 
</property> 
<!-- 注入list集合数据 --> 
<property name="myList"> 
    <array> 
        <value>AAA</value> 
        <value>BBB</value> 
        <value>CCC</value> 
    </array> 
</property> 
<!-- 注入set集合数据 --> 
<property name="mySet"> 
    <list> 
        <value>AAA</value> 
        <value>BBB</value> 
        <value>CCC</value> 
    </list> 
</property> 
<!-- 注入Map数据 --> 
<property name="myMap"> 
    <props> 
        <prop key="testA">aaa</prop> 
        <prop key="testB">bbb</prop> 
    </props> 
</property> 
<!-- 注入properties数据 -->
<property name="myProps"> 
    <map> 
        <entry key="testA" value="aaa">/entry> 
        <entry key="testB"> <value>bbb</value> </entry> 
    </map> 
</property> 
posted @ 2023-04-19 08:57  挖洞404  阅读(1)  评论(0编辑  收藏  举报