SpringDI(XML)

SpringDI总结(XML)

概述

​ DI的全称是Dependency Injection(依赖注入)。IOC是将我们工程中的所有对象交由Spring来管理,DI是此基础,将对象中的属性、依赖的其他对象也管理起来,自动注入到由Spring帮我们管理的对象中。***(将要注入的对象和目标对象都必须是由SpringIOC管理的bean.)***

构造方法注入

​ 将一个bean创建过程中构造方法需要的参数,通过Spring DI的方式,自动注入到构造方法中。

有参

//被管理的java类
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Person {
    private String name;
    private String sex;

    public Person(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public Person() {

    }
}
<!--   配置文件(有参)  -->
<!--   配置文件(无参)  -->
<?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">

	

	<bean id="onePerson" class="com.lanou.spring.bean.Person" >
       <constructor-arg name="name" value="张三"/>
       <constructor-arg name="sex" value=""/>
	</bean>
</beans>

无参

<!--   配置文件(无参)  -->
<?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">

	<bean id="otherPerson" class="com.lanou.spring.bean.Person"/>
</beans>

通过c命名空间注入参数

<!--   配置文件(通过c命名空间注入参数)  -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"       							   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       <!--   要用c命名空间必须先要再beans标签中声明  -->
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans      									   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="p" class="com.lanou.spring.bean.Person" c:name="张三" c:sex=""/>
</beans>

setter方法注入

​ 先通过一个无参的构造方法创建对象,然后通过属性的setter方法,将属性值注入到对象上。

普通的setter方法注入

<!--   普通的setter方法注入  -->
<?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">

	<bean id="p1" class="com.lanou.spring.bean.Person" >
    	<property name="sex" value=""/>
    	<property name="name" value="李四" />
	</bean>
</beans>

p命名空间简写

<!--   配置文件(通过p命名空间注入参数)  -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"       							   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       <!--   要用p命名空间必须先要再beans标签中声明  -->
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans      									   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="p" class="com.lanou.spring.bean.Person" p:name="张三" p:sex=""/>
</beans>

注入匿名内部类

//person类

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Person {
    private String name;
    private String sex;
    private Hobby hobby;
}

//hobby类

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Hobby {
    private String song;
    private String film;

}
<bean id="p2" class="com.lanou.spring.bean.Person">
	<property name="hobby">
    	<!--   内部类  -->
        <bean id="hob" class="com.lanou.spring.bean.Hobby" >
    		<property name="song" value="断桥残雪"/>
    		<property name="film" value="功夫"/>
    	</bean>
    </property>
</bean>

注入集合内部类

  • List

    <bean id="xxx" class="xx.xxx.xxx.AA"></bean>
    <bean>
        <property name="hobbies">
        	<list>
            	<value>简单类型值</value>
                <bean>内部bean</bean>
                <ref bean="xxx" />
            </list>
        </property>
    </bean>
    
  • Map

    <bean id="xxx" class="xx.xxx.xxx.AA"></bean>
    <bean>
        <property name="gameTitle">
        	<map>
            	<entry key="王者荣耀" value="荣耀王者" />
                <entry key="王者荣耀" value-ref="xxx" />
            </list>
        </property>
    </bean>
    
  • Set

    <bean id="xxx" class="xx.xxx.xxx.AA"></bean>
    <bean>
        <property name="hobbies">
        	<!-- set用法和List类似, 里面可以注入普通字面量值、也可以是一个bean引用,或者内部bean、或者是一个set、list、Properties  -->
            <set>
            	<value>简单类型值</value>
                <bean>内部bean</bean>
                <ref bean="xxx" />
            </set>
        </property>
    </bean>
    
  • java.util.Properties

    <!-- props标签是用来注入java.util.Properties类型的属性,用法和map类似,但是属性值是在标签中间写 -->
    <property name="gameNick">
        <props>
            <prop key="王者荣耀">最擅长1V5</prop>
            <prop key="吃鸡">一枪爆头</prop>
        </props>
    </property>
    

注入null,空字符串类型的属性值

<!-- 注入null -->
<property name="gameNick">
    <null />
</property>

<!-- 注入空字符串类型 -->
<property name="gameNick" value="" />

注入外部properties文件中的属性值

#外部properties文件
#key=value
name=张三
sex=男
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"       							   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    	<!-- 引入外部properties文件 -->
		<context:property-placeholder location="asd.properties"/>
        <bean id="p3" class="com.lanou.spring.bean.Person" >
            <!-- 通过${key}获取properties文件中的值 -->
            <property name="name" value="${name}"/>
            <property name="sex" value="${sex}"/>
        </bean>
</beans>

自动装配

  • byType

    按照类型去IOC容器中找需要的bean,如果找到一个,则自动装配;如果没找到,不注入此属性;如果找到了多个匹配类型的bean,就会报错。

  • byName

    按照名称去IOC容器中找需要的bean,如果找到就自动注入;如果没找到,不注入此属性。

  • constructor

    工作原理和byType类似,也是按照类型去IOC容器中找对应的bean。不同的是注入的地方不是setter,而是构造方法的参数。

  • no (默认值)

    如果没有打开自动注入,默认Spring不会自动装配需要的属性。

开启自动装配

<!-- 
通过给当前的bean添加autowire属性开启自动注入
可选的值:参见自动装配章节
 -->
<bean id="xx" class="" autowire="" />

提高自动装配时的权重

<!-- 当其他的bean中需要注入一个Test类型的属性,而满足条件的bean有多个时,会优先注入primary="true"的bean -->
<bean id="xx" class="com.Test" primary="true" />

按类型自动装配时,不参与候选

<!-- 当其他的bean中需要注入一个Test类型的属性,而满足条件的bean有多个时,autowire-candidate="false"的bean会自动退出候选序列 -->
<bean id="xx" class="com.Test" autowire-candidate="false" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值