【Spring学习05】四种xml配置注入方式
本文博客地址:http://www.52php.cn/article/p-ephiyxdb-bps.html (转载请注明出处) 平常应用Spring开发中,用得最多的是容器。spring容器帮我们实例化对象并且注入到需要该对象的类中。 一、Setter注入set注入是最简单常用的注入方式,《【Spring学习03】Spring简单入门实例 》中就是用的set注入。 代码如下: public class Order {
/*要注入的对象*/
private NotifyService notifyservice;
/*notifyservice不是在内部new()出来的, 而是通过指定方法传进来的,也就是我们说的注入。这里是setter方法注入*/
public void setNotifyservice(NotifyService notifyservice) {
this.notifyservice = notifyservice;
}
/*订单支付完成后,系统通知老板*/
public void PaySuccess(){
notifyservice.sendMessage("客户张铁蛋完成订单2017079657付款,共人民币:97.5元");
}
}
然后编写spring的xml文件: <bean id="notify" class="twm.spring.start.NotifyServiceByCellPhoneImpl" />
<bean id="order" class="twm.spring.start.Order" >
<!-- 配置要注入的对象 -->
<property name="notifyservice" ref="notify"/>
</bean>
另外要注意的是:property name只和注入方法名相关,和内部属性名没有半毛关系。所以order类改成下面这样也是可以的: public class Order {
private NotifyService notifyservice_suibian;
public void setnotifyservice(NotifyService notifyservice) {
this.notifyservice_suibian = notifyservice;
}
/*订单支付完成后,系统通知老板*/
public void PaySuccess(){
notifyservice_suibian.sendMessage("客户张铁蛋完成订单2017079657付款,共人民币:97.5元");
}
}
二、构造器注入构造函数也是一个注入点。现在我们把上例的setter注入改为构造注入。 /*构造函数注入*/
public Order(NotifyService notifyservice1) {
this.notifyservice1 = notifyservice1;
}
相应的beans.xml修改:
public class Order {
private String username;
private String orderno;
private NotifyService notifyservice1;
/*构造函数注入*/
public Order(String username,String orderno,NotifyService notifyservice1) {
this.username = username;
this.orderno = orderno;
this.notifyservice1 = notifyservice1;
}
/*订单支付完成后,系统通知老板*/
public void PaySuccess(){
notifyservice1.sendMessage("客户"+username+"完成订单"+orderno+"付款,共人民币:97.5元");
}
}
这时配置beans.xml就有讲究了,spring提供了几种方法: <bean id="order" class="twm.spring.start.Order" >
<constructor-arg value="张老三"></constructor-arg>
<constructor-arg ref="notify"></constructor-arg>
<constructor-arg value="1234567"></constructor-arg>
</bean>
2、指明参数类型。 <bean id="order" class="twm.spring.start.Order" >
<constructor-arg type="String" value="张老三"></constructor-arg>
<constructor-arg type="twm.spring.start.NotifyService" ref="notify"></constructor-arg>
<constructor-arg type="String" value="1234567"></constructor-arg>
</bean>
和1智能识别一样,Spring会先按类型区分,同类型的按先后顺序向构造函数参数赋值。 3、指定参数名 <bean id="order" class="twm.spring.start.Order" >
<constructor-arg name="username" value="张老三"></constructor-arg>
<constructor-arg name="notifyservice1" ref="notify"></constructor-arg>
<constructor-arg name="orderno" value="1234567"></constructor-arg>
</bean>
这样Spring会完全按照构造函数的形参名字匹配。 4、指定索引index <bean id="order" class="twm.spring.start.Order" >
<constructor-arg index="0" value="张老三"></constructor-arg>
<constructor-arg index="2" ref="notify"></constructor-arg>
<constructor-arg index="1" value="2017877997"></constructor-arg>
</bean>
指定的索引顺序一定要和类型匹配,不然会报错。构造函数有2个相同类型的参数,指定索引可以解决此种情况。 构造注入对比setter注入
三、静态工厂的方法注入静态工厂,就是通过调用静态工厂的方法来获取自己需要的对象,为了让spring管理所有对象,我们不能直接通过”工程类.静态方法()”来获取对象,而是依然通过spring注入的形式获取。 NotifyFactory.java: public class NotifyFactory {
/*静态工厂方法*/
public static NotifyService getNotifyService(){
return new NotifyServiceByWeixinImpl();
}
}
其它类不变,但xml会发现有很大差别: <bean id="order" class="twm.spring.start.Order">
<constructor-arg name="username" value="张老三"></constructor-arg>
<constructor-arg name="notifyservice1" ref="notify2"></constructor-arg>
<constructor-arg name="orderno" value="1234567"></constructor-arg>
</bean>
<bean id="notify2" class="twm.spring.start.NotifyFactory" factory-method="getNotifyService" />
注意看指向的class并不是NotifyService的实现类,而是指向静态工厂NotifyFactory,并且配置 静态工厂方法需要参数,怎么传递? 四、实例工厂的方法注入实例工厂的意思是获取对象实例的方法不是静态的,所以需要首先new工厂类,再调用普通的实例方法:: public class NotifyFactory {
/*普通工厂方法*/
public NotifyService getNotifyService(){
return new NotifyServiceByWeixinImpl();
}
}
实例工厂方法(非静态)和静态工厂方法本质相同(除了使用facory-bean属性替代class属性,其他都相同),因此细节就不讨论了。 <bean id="order" class="twm.spring.start.Order">
<constructor-arg name="username" value="张老三"></constructor-arg>
<constructor-arg name="notifyservice1" ref="notify2"></constructor-arg>
<constructor-arg name="orderno" value="1234567"></constructor-arg>
</bean>
<bean id="notifyfactory" class="twm.spring.start.NotifyFactory" />
<bean id="notify2" factory-bean="notifyfactory" factory-method="getNotifyService" />
Spring IOC注入方式用得最多的是1、2两种。 现在常用的注解注入不属于xml配置注入方式,故后面开篇单讲 附录: <bean class="ExampleBean">
<property name="email" value=""/>
</bean>
上面的配置相当于 2、给属性设置null值 <bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
上面配置相当于 恭喜你,看完这里,基本可以应付日常开发了。 本文博客地址:http://www.52php.cn/article/p-ephiyxdb-bps.html (转载请注明出处) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |