P命令空间[了解]
- 对“setter方法注入”进行简化,替换<property name="属性名">,而是在
<bean p:属性名="普通值" p:属性名-ref="引用值">
- p命名空间使用前提,必须添加命名空间
<bean id="personId" class="com.itheima.f_xml.c_p.Person" p:pname="禹太璞" p:age="22" p:homeAddr-ref="homeAddrId" p:companyAddr-ref="companyAddrId"> </bean>
<bean id="homeAddrId" class="com.itheima.f_xml.c_p.Address" p:addr="DG" p:tel="东莞"> </bean> <bean id="companyAddrId" class="com.itheima.f_xml.c_p.Address" p:addr="DG" p:tel="岛国"> </bean> |
-
-
- SpEL[了解]
-
- 对<property>进行统一编程,所有的内容都使用value
<property name="" value="#{表达式}">
#{123}、#{'jack'} : 数字、字符串
#{beanId} :另一个bean引用
#{beanId.propName} :操作数据
#{beanId.toString()} :执行方法
#{T(类).字段|方法} :静态方法或字段
<!-- <property name="cname" value="#{'jack'}"></property> <property name="cname" value="#{customerId.cname.toUpperCase()}"></property> 通过另一个bean,获得属性,调用的方法 <property name="cname" value="#{customerId.cname?.toUpperCase()}"></property> ?. 如果对象不为null,将调用方法 --> <bean id="customerId" class="com.itheima.f_xml.d_spel.Customer" > <property name="cname" value="#{customerId.cname?.toUpperCase()}"></property> <property name="pi" value="#{T(java.lang.Math).PI}"></property> </bean> |
- 阅读:
集合注入
<!-- 集合的注入都是给<property>添加子标签 数组:<array> List:<list> Set:<set> Map:<map> ,map存放k/v 键值对,使用<entry>描述 Properties:<props> <prop key=""></prop> 【】
普通数据:<value> 引用数据:<ref> --> <bean id="collDataId" class="com.itheima.f_xml.e_coll.CollData" > <property name="arrayData"> <array> <value>DS</value> <value>DZD</value> <value>屌丝</value> <value>屌中屌</value> </array> </property>
<property name="listData"> <list> <value>于嵩楠</value> <value>曾卫</value> <value>杨煜</value> <value>曾小贤</value> </list> </property>
<property name="setData"> <set> <value>停封</value> <value>薄纸</value> <value>关系</value> </set> </property>
<property name="mapData"> <map> <entry key="jack" value="杰克"></entry> <entry> <key><value>rose</value></key> <value>肉丝</value> </entry> </map> </property>
<property name="propsData"> <props> <prop key="高富帅">嫐</prop> <prop key="白富美">嬲</prop> <prop key="男屌丝">挊</prop> </props> </property> </bean> |
装配Bean 基于注解
- 注解:就是一个类,使用@注解名称
- 开发中:使用注解 取代 xml配置文件。
1. @Component取代<bean class="">
@Component("id") 取代 <bean id="" class="">
2.web开发,提供3个@Component注解衍生注解(功能一样)取代<bean class="">
@Repository :dao层
@Service:service层
@Controller:web层
3.依赖注入 ,给私有字段设置,也可以给setter方法设置
普通值:@Value("")
引用值:
方式1:按照【类型】注入
@Autowired
方式2:按照【名称】注入1
@Autowired
@Qualifier("名称")
方式3:按照【名称】注入2
@Resource("名称")
4.生命周期
初始化:@PostConstruct
销毁:@PreDestroy
5.作用域
@Scope("prototype") 多例
- 注解使用前提,添加命名空间,让spring扫描含有注解类
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 组件扫描,扫描含有注解的类 --> <context:component-scan base-package="com.itheima.g_annotation.a_ioc"></context:component-scan> </beans> |