3、依赖注入

编程入门 行业动态 更新时间:2024-10-08 08:27:36

3、依赖注入

3、依赖注入

概述:

setter注入:

引用类型用ref引入其他bean,简单类型用value赋值注入。

构造器注入:

public BookServiceImpl(BookDao bookDao) {this.bookDao = bookDao;
}
<bean id="bookService" class="service.Impl.BookServiceImpl"><constructor-arg name="bookDao" ref="bookDao"></constructor-arg>
</bean>

两者的方法比较:

自己开发的模块推荐使用setter注入。

强制依赖使用构造器进行,使用setteri注入有概率不进行注入导致null对象出现

可选依赖使用setter注入进行,灵活性强

Spring框架倡导使用构造器,第三方框架内部大多数采用构造器注入的形式进行数据初始化,相对严谨。

构造注入优先级比set注入高

依赖自动装配:

IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配。

setter方法不变,bean配置变成

<bean id="bookDao" class="dao.impl.BookDaoImpl"/>
<bean id="bookService" class="service.Impl.BookServiceImpl" autowire="byType"/>

ATT:自动装配用于引用类型依赖注入,不能对简单类型进行操作

自动装配是不能省略要注入的其他类对象的bean的,譬如这里的bookDao

按类型装配的话,这里的bookDao的bean的名字,即id都可以省略。

使用按类型装配时(byType)必须保障容器中相同类型的bean唯一,推荐使用

使用按名称装配时(byName)必须保障容器中具有指定名称的bean,因变量名与配置耦合,不推荐使用

自动装配优先级低于setter注入与构造器注入,同时出现时自动装配配置失效

集合注入:

package dao.impl;import dao.BookDao;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class BookDaoImpl implements BookDao, InitializingBean, DisposableBean {private int[] array;private List<String> list;private Set<String> set;private Map<String,String> map;private Properties properties;public void setArray(int[] array) {this.array = array;}public void setList(List<String> list) {this.list = list;}public void setSet(Set<String> set) {this.set = set;}public void setMap(Map<String, String> map) {this.map = map;}public void setProperties(Properties properties) {this.properties = properties;}public void save(){System.out.println("array:  ");for(int x:array){System.out.print(x+" ");}System.out.println();System.out.println("list:   ");for(String x:list){System.out.print(x+" ");}System.out.println();System.out.println("Set:   ");for(String x:set){System.out.print(x+' ');}System.out.println();System.out.println("Map:   ");for(Map.Entry<String,String>x: map.entrySet()){System.out.print(x+" ");}System.out.println();System.out.println("Propertities:   ");for(String key:properties.stringPropertyNames()){System.out.print(key+" = "+properties.getProperty(key)+"  ");}}@Overridepublic void destroy() throws Exception{System.out.println("system destroy...");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("system init");}
}
<bean id="bookDao" class="dao.impl.BookDaoImpl">
<property name="array" ><array><value>100</value><value>200</value><value>300</value></array>
</property><property name="list"><list><value>itheima</value><value>itcast</value><value>lalala</value></list>
</property><property name="map"><map><entry key="wow" value="ao"></entry><entry key="biubiu" value="err"></entry><entry key="sth" value="wrong"></entry></map>
</property>
<property name="set"><set><value>heiheihei</value><value>hahahaha</value><value>aowuaowu</value></set>
</property>
<property name="properties"><props><prop key="ohayo">konijiwa</prop><prop key="minasang">wagada</prop><prop key="aligado">daijiobu</prop></props>
</property>
</bean>

更多推荐

3、依赖注入

本文发布于:2024-02-13 19:22:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!