首页 > 编程语言 > 详细

Spring IOC(三)

时间:2020-10-10 18:28:15      阅读:34      评论:0      收藏:0      [点我收藏+]

注入

有参构造方法

<!--有参构造方法-->
<bean id="user2" class="com.tj.entity.User">
    <constructor-arg name="username" value="张三"></constructor-arg>
    <constructor-arg name="password" value="abc123"></constructor-arg>
</bean>

给谁注入:

  • index:指定参数在构造函数参数列表的索引位置(一般不使用)
  • type:指定参数在构造函数中的数据类型(一般不需要指定)
  • name:指定参数在构造函数中的名称 用这个找给谁赋值 (常用)

注入的值:

  • value:直接注入
  • ref:为其它bean类型

set方法

<bean id="user5" class="com.tj.entity.User">
    <property name="username" value="liuliu"></property>
    <property name="password" value="ll123"></property>
</bean>

给谁注入:

  • name:指定参数在构造函数中的名称 用这个找给谁赋值 (常用)

注入的值:

  • value:直接注入
  • ref:为其它bean类型

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:username="hejiujiu" p:password="hj123">
    </bean>
</beans>
  • p命名空间需要引入单独的一条约束
  • p:username直接注入,p:username-ref注入其它bean对象

注入集合

注入集合数据
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> 

SpEL

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

数字值和字符串值:#{123}, #{‘zhangsan’}
引用其他bean值:#{bean的id}
引用其他bean的属性:#{bean的id.属性名}
执行其他bean的方法:#{bean的id.方法名}
引用静态方法或属性:#{T(类路径).属性名/方法名}

自动装配

前提:需要给对应的属性设置set方法

针对单个bean的配置

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

<bean id="people" class="com.yaorange.entity.Peopele" autowire="byName"/>

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

<bean id="people" class="com.yaorange.entity.Peopele" 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">

Spring IOC(三)

原文:https://www.cnblogs.com/heibaimao123/p/13793579.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!