目前我知道的注入方式有五种:
1.Setter注入(应用广泛):set方法
这种方式比较容易理解的,使用Setter方法进行依赖注入,可以注入简单值,集合,其他依赖的bean等,举个例子会更加直观:
业务类:
(1)OtherBean(空类)
package lm.practice.di; /** * Created by Administrator on 2017/4/28. */ public class OtherBean { }
(2)SomeBean
public class SomeBean { private String name; private String pwd; private long maxConnection; private URL url; private List list; private Set set; private Map map; private Properties properties; private OtherBean otherBean; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public long getMaxConnection() { return maxConnection; } public void setMaxConnection(long maxConnection) { this.maxConnection = maxConnection; } public URL getUrl() { return url; } public void setUrl(URL url) { this.url = url; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Set getSet() { return set; } public void setSet(Set set) { this.set = set; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public OtherBean getOtherBean() { return otherBean; } public void setOtherBean(OtherBean otherBean) { this.otherBean = otherBean; } @Override public String toString() { return "{ "+"name="+name+",pwd="+pwd+",maxConnection="+maxConnection+",url="+url+" ,list="+list+",set=" +set+",map="+map+",properties="+properties+",otherBean="+otherBean+" }"; } }
(3)spring配置文件
1.注入简单值:
<bean id="otherBean" class="lm.practice.di.OtherBean"/> <bean id="someBean" class="lm.practice.di.SomeBean"><property name="name" value="somebean"/> <property name="maxConnection" value="11"/> <property name="pwd" value="1234"/> <property name="url" value="http://localhost:8080/hello.html"/></bean>
注:set方法注入简单值,只需要通过bean元素的property的value属性赋值就可以给对应的name赋值,name中的值是对象里对应属性的值,在注入的时候是以字符串的形式进行的,spring内部会自动转换成要求的类型
2.注入其他依赖的bean
<property name="otherBean" ref="otherBean"/>注:通过set方法注入依赖的其他bean,要保证两点,第一,ref的值其依赖bean的id,第二,依赖的bean要在spring中进行了配置
3.注入list集合
<property name="list"> <list> <value>haha</value> <value>1111</value> <ref bean="otherBean"/> <list> <value>jihe</value> </list> </list> </property>
注:list集合是无序可重复的,在注入list集合的时候,通过value赋值,集合中可以嵌套集合或者其他类型。如果是简单的List(没有指定泛型),那么value值可以随意指定,spring内部会转换成对应的类型,如果指定了泛型,例如List<Integer>,那么spring内部会转换成整形数据
4.注入map集合
<property name="map"> <map> <entry key="1" value="壹"/> <entry key="2" value-ref="otherBean"/> <entry key-ref="otherBean" value="1"/> <entry key="3"> <map> <entry key="4" value="si"/> </map> </entry> </map> </property>
注:Map是键值对,要指定key和value,其中可以通过key-ref和value-ref赋值其他的依赖
5.注入set集合
<property name="set"> <set> <value>111</value> <value>111</value> <set> <value>111</value> <value>111</value> </set> <list> <value>111</value> <value>111</value> </list> </set> </property>
注:set集合是无序,不可重复的,如果value值赋值重复,会自动过滤掉重复的数据
6.注入Properties
有两种方式:
第一、通过prop注入
<property name="properties"> <props> <prop key="show_sql">true</prop> <prop key="hdgg.auto">create</prop> <prop key="中文">中文</prop> </props> </property>
注:这种方式的特点是,能够正常识别中文,正常装配Properties对象
第二、通过value注入
<property name="properties"> <value> show_sql=true hdgg.auto=create 中文=中文 </value> </property>注:这种方式要注意的是,每个Properties对象占一行,形式为key=value,另外,中文不能正常识别
小结:
Setter注入的优缺点:
1,如果依赖对象过多,代码不会不好维护;
2,如果依赖对象过多,可能造成某些属性注入丢失(忘记注入了~);
解决办法:@Required注解配置必要的setter方法
2.注解注入(简单):@Resource(“要注入的bean”)
3.构造器注入(应用也广泛):<constructor-arg ref="要注入的对象">
(1)OtherBean同上
(2)SomeBean
public class SomeBean { private String name; private long maxConnection; private OtherBean otherBean; public SomeBean(SomeBean someBean) { } public SomeBean(String name, long maxConnection, OtherBean otherBean) { this.name = name; this.maxConnection = maxConnection; this.otherBean = otherBean; } @Override public String toString() { return "{ "+"name="+name+",maxConnection="+maxConnection+",otherBean="+otherBean+" }"; } }
(3)配置文件
<bean id="someBean" class="lm.practice.conDi.SomeBean"> <!--注意:这里要按照构造器参数的顺序赋值--> <constructor-arg value="name"/> <constructor-arg value="1111"/> <constructor-arg ref="otherBean"/> </bean>
注:构造器注入时,默认是按照参数的顺序赋值,如果不按顺序赋值的话,Spring提供了其他额外的配置来方便spring找到对应的构造器的对应参数。
1.index:通过构造方法中参数位置来指定,从0开始
2.type:通过构造方法中参数的类型来设置
3.name:通过构造方法中参数的名字来设置
这里还要指出的一点是,一旦使用构造器注入,就不能简单的配置bean元素,必须<bean></bean>这样配置
4.静态工厂注入
5.实例工厂注入