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、依赖注入
发布评论