Loading

第二章 Spring Bean进阶

一、复杂属性赋值

1、数组

<array>
  <value>值1</value>
</array>

2、集合

<list>
  <value>值1</value>
</list>

3、哈希数组Map<key,value>

<map>
  <entry key="键1" value="值1"></entry>
</map>

4、Properties属性集合

<props>
  <prop key="键1">值1</prop>
</props>

二、注解

1、定义:其实就是一个Java类
2、格式:@关键字
3、位置:类名或、属性名、方法的上一行
4、用途:用来替代xml文件中某些标签和属性的,简化或提取xml文件
5、‼️重要的注解

@component   //代替<bean>标签
@component("id名称")  //代替<bean id="id名称"></bean>
@value("属性值")  //代替set注入<property name="" value=""></property>
@autowired  //代替set注入<property name="" ref=""></property>

⚠️注意:
如果想要将被注解修饰的类注册到Spring容器中,必须要扫包
中添加

       xmlns:context="http://www.springframework.org/schema/context"
       并在xsi:schemaLocation="中添加
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
		<!-- 扫包 -->
		<context:component-scan base-package="com.yuki.bean"></context:component-scan>
posted @ 2021-07-20 13:54  Ryosetsu  阅读(21)  评论(0)    收藏  举报