spring依赖注入 注入值 spring表达式

来源:互联网 发布:淘宝登陆页面打不开 编辑:程序博客网 时间:2024/05/19 09:40

依赖注入

方式一 使用set方式注入

public class A {private IB b;public void setB(IB b) {System.out.println("setB()");this.b = b;}public A() {System.out.println("A()");}public void execute(){System.out.println("execute()");b.f1();}}

public class B implements IB{public B() {System.out.println("B()");}public void f1(){System.out.println("B's f1()");}}

<bean id="b1" class="ioc.B" /><!-- property元素:表示使用set方法来注入依赖关系.其中,name属性指定属性名,ref属性指定属性值(是被注入bean的id) --><bean id="a1" class="ioc.A" ><property name="b" ref="b1"/></bean>

方式二 构造器方式注入

package ioc2;public class A {private B b;public A() {System.out.println("A()");}public void execute(){System.out.println("execute()");b.f1();}public A(B b) {System.out.println("A(B)");this.b = b;}}
<bean id="b1" class="ioc2.B"></bean><!-- constructor-arg元素:用来配置构造器方式注入.index属性:指定参数的下标(从0开始) --><bean id="a1" class="ioc2.A"><constructor-arg index="0" ref="b1"/></bean>

方式三 自动装配

<!-- autowire属性:让容器自动装配,该属性有如下3个值byName:容器依据类中的属性名查找对应的bean,然后调用对应的set方法来完成注入注:a.如果找不到对应的bean,注入null.b.不可能找到多个符合条件的bean.byType:容器依据类中的属性类型查找对应的bean,然后调用对应的set方法完成注入注:a.如果找不到对应的bean,注入null.b.有可能找到多个符合条件的bean,此时会出错.constructor:与byType类似,不同的是调用对应的构造器来完成注入 --><bean id="wt" class="ioc2.Waiter"></bean><bean id="rest" class="ioc2.Restaurant" autowire="constructor"></bean>

注入值

注入基本类型的值

<bean id="sp1" class="value.SpelBean"><property name="name" value="zhangsan"></property></bean>

注入集合类型的值

<bean id="vb1" class="value.ValueBean"><property name="name" value="胡八一"></property><property name="age" value="30"></property><property name="city"><list><value>北京</value><value>天津</value><value>南京</value><value>南京</value></list></property><property name="interest"><set><value>钓鱼</value><value>做饭</value><value>喝酒</value></set></property><property name="score"><map><entry key="英语" value="60"/><entry key="math" value="66"/></map></property><property name="db"><props><prop key="username">Tiger</prop><prop key="password">1234</prop></props></property></bean>

将集合类型的值配置成一个bean

<util:list id="cityBean"><value>上海</value><value>深圳</value><value>武汉</value></util:list><util:set id="interestBean"><value>书法</value><value>绘画</value><value>下棋</value></util:set><util:map id="scoreBean"><entry key="english" value="80"></entry><entry key="math" value="99"></entry></util:map><util:properties id="dbBean"><prop key="username">Sally</prop><prop key="password">1234</prop></util:properties>

引用的方式注入集合类型的值

<bean id="vb2" class="value.ValueBean"><property name="city" ref="cityBean"></property><property name="interest" ref="interestBean"></property><property name="score" ref="scoreBean"></property><property name="db" ref="dbBean"></property></bean>

读取配置文件

<!-- 读取properties文件的内容classpath:按照类路径来搜索spring容器会依据路径找到对应的properties文件,然后读取该文件的内容到properties对象 --><util:properties id="config" location="classpath:config.properties"></util:properties>

spring表达式

<bean id="sp1" class="value.SpelBean"><property name="name" value="#{vb1.name}"></property><property name="city" value="#{vb1.city[1]}"></property><property name="score" value="#{vb1.score['英语']}"></property><property name="pageSize" value="#{config.pageSize}"></property></bean>


0 0