一、如何注入bean对象
- 简介
- 如何将bean对象注入到另一个bean对象的属性里面
- 用法
- 具体使用ref标签实现
- 属性1:bean
- 注入当前容器的bean对象,如果当前容器没有bean对象,它还会从父容器里面去找,如果不存在,则报异常
- 属性2:parent
- 注入父容器的bean对象,如果没有,则报异常
- 属性1:bean
- 具体使用ref标签实现
- 举例
- 定义父容器的parentbeans.xml文件
<?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"> <!--配置Car bean对象--> <bean id="car" class="com.gec.bean.Car"> <property name="brand"> <value>特斯拉</value> </property> <property name="price"> <value>300000.00</value> </property> </bean> </beans>
- 定义子容器的childbeans.xml
<?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="boss" class="com.gec.bean.Boss"> <property name="bossName"> <value>钟老板</value> </property> <property name="car"> <!--从父容器里面去找bean对象,id=car--> <ref parent="car" /> </property> </bean> </beans>
- 创建子容器的对象,指定容器的父子关系
package com.gec.app; import com.gec.bean.Boss; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ParentIocMainTest { public static void main(String[] args) { //创建一个父IOC容器 ApplicationContext parentCtx=new ClassPathXmlApplicationContext("parentbean.xml"); //创建一个子IOC容器 ApplicationContext childCtx=new ClassPathXmlApplicationContext( new String[]{"childbean.xml"},parentCtx); Boss bossObj= (Boss) childCtx.getBean("boss"); System.out.println(bossObj.getBossName()); System.out.println(bossObj.getCar()); } }
- 定义父容器的parentbeans.xml文件
二、内部bean用法
- 简介
- 没有声明id或者是name的bean,也就是无法被外部所使用,类似于java的匿名内部类
- 用法
<?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"> <!--配置Boss的bean对象--> <bean id="boss" class="com.gec.bean.Boss"> <property name="bossName"> <value>钟老板</value> </property> <property name="car"> <!--定义一个内部bean对象--> <bean class="com.gec.bean.Car"> <property name="brand"> <value>特斯拉</value> </property> <property name="price"> <value>300000.00</value> </property> </bean> </property> </bean> </beans>
三、级联属性配置
- 简介
- 针对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"> <!--配置Boss的bean对象--> <bean id="boss" class="com.gec.bean.Boss"> <property name="bossName"> <value>钟老板</value> </property> <property name="car"> <bean class="com.gec.bean.Car" /> </property> <property name="car.brand"> <value>宝马</value> </property> <property name="car.price"> <value>300000.00</value> </property> </bean> </beans>
四、集合注入
1、简介
- 针对List、Set、Map集合数据的注入
2、针对List集合的数据注入
- 用法
- 定义Boss类
package com.gec.bean; import java.util.List; public class Boss { private String bossName; private Car car; //有那些兴趣 private List<String> favlist; //有拥有那些车 private List<Car> carList; public String getBossName() { return bossName; } public void setBossName(String bossName) { this.bossName = bossName; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public List<String> getFavlist() { return favlist; } public void setFavlist(List<String> favlist) { this.favlist = favlist; } public List<Car> getCarList() { return carList; } public void setCarList(List<Car> carList) { this.carList = carList; } @Override public String toString() { return "Boss{" + "bossName='" + bossName + '\'' + ", car=" + car + '}'; } }
- 定义beans.xml
<?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="car01" class="com.gec.bean.Car"> <property name="brand"> <value>宝马1</value> </property> <property name="price"> <value>1000000.00</value> </property> </bean> <bean id="car02" class="com.gec.bean.Car"> <property name="brand"> <value>宝马2</value> </property> <property name="price"> <value>2000000.00</value> </property> </bean> <bean id="car03" class="com.gec.bean.Car"> <property name="brand"> <value>宝马3</value> </property> <property name="price"> <value>3000000.00</value> </property> </bean> <!--配置Boss的bean对象--> <bean id="boss" class="com.gec.bean.Boss"> <property name="bossName"> <value>钟老板</value> </property> <property name="car"> <bean class="com.gec.bean.Car" /> </property> <property name="car.brand"> <value>宝马</value> </property> <property name="car.price"> <value>300000.00</value> </property> <property name="favlist"> <list> <value>音乐</value> <value>足球</value> <value>游泳</value> </list> </property> <property name="carList"> <list> <ref bean="car01" /> <ref bean="car02" /> <ref bean="car03" /> </list> </property> </bean> </beans>
- 定义Boss类
3、针对Set集合数据注入
<!--配置Boss的bean对象-->
<bean id="boss" class="com.gec.bean.Boss">
<property name="bossName">
<value>钟老板</value>
</property>
<property name="car">
<bean class="com.gec.bean.Car" />
</property>
<property name="car.brand">
<value>宝马</value>
</property>
<property name="car.price">
<value>300000.00</value>
</property>
<property name="favlist">
<list>
<value>音乐</value>
<value>足球</value>
<value>游泳</value>
</list>
</property>
<property name="carList">
<list>
<ref bean="car01" />
<ref bean="car02" />
<ref bean="car03" />
</list>
</property>
<property name="sports">
<set>
<value>篮球</value>
<value>篮球</value>
<value>篮球</value>
</set>
</property>
</bean>
4、针对Map集合数据注入
5、针对Properties对象数据注入
五、Spring的bean简化配置
1、字面值写法
简化
2、针对引用简化
- 简化之后
3、p标签的简化写法
- 导入p标签的命名空间
xmlns:p="http://www.springframework.org/schema/p"
- 通过p标签定义bean配置
4、通过utils标签定义集合
- 如何使用utils标签,导入此标签的命名空间
xmlns:utils="http://www.springframework.org/schema/util"
- 定义一个ArrayList的bean集合
- 如何定义一个Set集合bean
- 如何定义一个Map集合bean
六、自注入Bean对象
1、简介
- 通过autowire属性配置实现,实现自动的注入Bean对象
2、属性
- byName:从当前IOC容器自动注入name=属性名的bean对象
- byType:根据类型自动注入到bean对象的属性里面
- constructor:通过构造器实现自装配功能
3、用法
- byName
- byType
- 自动根据bean类型自动注入到属性
- constructor
- 自动根据构造器自动注入
- 前提是类对象要有构造器
- 定义Boss类
- 自动根据构造器自动注入
七、Spring表达式语言
1、概述
- 它是从spring3才引入的,表达式作用以一种更加简洁的方式去实现bean对象属性值的注入,并且能够实现动态注入数据值效果
2、语法
#{表达式}
3、功能
- 通过bean id,访问bean对象
- 通过表达式可以访问bean对象的属性及方法
- 对值进行算术运行
- 对集合进行访问
- 对类成员进行调用及访问
4、通过beanid,访问bean对象
5、访问bean对象的属性及方法
6、对类成员进行调用及访问
- 简介
- 如果要在SpEl中访问类作用域的方法和常量的话,要依赖T()这个关键的运算符。
- 用法
7、对值进行算术运算
- 简介
- 实现数据值加、减、乘
- 举例
- 计算圆形周长(2pir)
- 计算圆形面积
- 实现
8、对集合访问
- 定义歌曲对象
- 自动点唱机对象
- 配置歌曲列表
- 获取jukebox的属性songs集合的第二个歌曲对象
- 获取jukebox的属性songs的artist属性等于周杰伦的歌曲列表集合,返回是集合
<property name="songsList" value="#{jukebox.songs.?[artist eq '周杰伦']}"/>
- 获取jukebox的属性songs的第一个artist属性等于周杰伦的歌曲,返回是歌曲对象
<property name="firstSongs" value="#{jukebox.songs.^[artist eq '周杰伦']}"/>
- 获取jukebox的属性songs的最后一个artist属性等于周杰伦的歌曲,返回是歌曲对象
<property name="lastSongs" value="#{jukebox.songs.$[artist eq '周杰伦']}"/>