4.Spring DI(依赖注入)
来源:互联网 发布:手机后期ps软件 编辑:程序博客网 时间:2024/05/01 11:06
注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装
配会产生未知情况,开发人员无法预见最终的装配结果。
一.手工装配:
1.使用构造器注入
2.使用属性setter方法注入
注意:setter注入的属性必须要有setter方法。
两点一起演示:
package com.zyy.service;/** * Created by CaMnter on 2014/8/27. */public interface ISpring_5 { public void message();}
package com.zyy.service.impl;import com.zyy.service.ISpring_5;import com.zyy.service.PersonDao;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;/** * Created by CaMnter on 2014/8/27. */public class ISpring_5Bean implements ISpring_5 { private PersonDao personDao; private String info; private String name; private int id; private Set<String> set; private List<String> list; private Map<String, String> map; private Properties properties; public PersonDao getPersonDao() { return personDao; } public ISpring_5Bean(String info) { this.info = info; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public ISpring_5Bean() { System.out.println("***** 各种的注入属性 *****"); } public void message() { this.personDao.add(); System.out.println("***** " + this.getName() + " *****"); System.out.println("***** " + this.getId() + " *****"); System.out.println("***** " + this.getInfo() + " *****"); for (String set_part : this.getSet()) { System.out.println("***** " + set_part + " *****"); } for (String list_part : this.getList()) { System.out.println("***** " + list_part + " *****"); } for (Object key : this.getProperties().keySet()) { System.out.println("***** " + key + " = " + this.getProperties().getProperty((String) key) + " *****"); } for (Object key : this.getMap().keySet()) { System.out.println("***** " + key + " = " + this.getMap().get(key) + " *****"); } }}
package com.zyy.service;/** * Created by CaMnter on 2014/8/20. */public interface PersonDao { void add();}
package com.zyy.service.impl;import com.zyy.service.PersonDao;/** * Created by CaMnter on 2014/8/20. */public class PersonDaoBean implements PersonDao { public void add() { System.out.println("***** PersonDaoBean-add()方法 *****"); }}
beans.xml:
<bean id="personDao" class="com.zyy.service.impl.PersonDaoBean"></bean> <bean name="iSpring_5" class="com.zyy.service.impl.ISpring_5Bean" > <!-- setter注入对象属性 --> <property name="personDao" ref="personDao"></property> <!-- setter注入普通属性 --> <property name="name" value="CaMnter"></property> <property name="id" value="07"></property> <property name="set"> <set> <value>set_part_1</value> <value>set_part_2</value> <value>set_part_3</value> </set> </property> <!-- setter注入List类型属性 --> <property name="list"> <list> <value>list_part_1</value> <value>list_part_2</value> <value>list_part_3</value> </list> </property> <!-- setter注入ProPerties类型属性 --> <property name="properties"> <props> <prop key="properties_part_1"> properties_value_1 </prop> <prop key="properties_part_2"> properties_value_2 </prop> </props> </property> <!-- setter注入Map类型属性 --> <property name="map"> <map> <entry key="map_key-1" value="map_value_1"/> <entry key="map_key-2" value="map_value_2"/> <entry key="map_key-3" value="map_value_3"/> </map> </property> <!-- 构造器注入 --> <constructor-arg index="0" value="构造器注入"></constructor-arg> <!-- 内部bean的方式 只为这个bean使用 –> <property name="personDao" > <bean class="com.zyy.service.impl.PersonDaoBean"></bean> </property> --> </bean>
junit4.4测试代码:
@Test public void test_3() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); ISpring_5 iSpring_5 = (ISpring_5) applicationContext.getBean("iSpring_5"); iSpring_5.message(); }
3.使用Field注入(用于注解方式):
在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:
@Autowired默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean
才会按类型装配。
@Autowired
private PersonDao personDao;//用于字段上
@Autowired
public void setOrderDao(OrderDao orderDao){//用于属性的setter方法上
this.orderDao = orderDao;
}
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null
值可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注
解一起使用。如下:
@Autowired @Qualifier("personDaoBean")
private PersonDao personDao;
@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按
名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在
字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上
即默认取属性名作为bean名称寻找依赖对象。
@Resource(name=“personDaoBean”)
private PersonDao personDao;//用于字段上
注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时,@Resource注解会
回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。
beans.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!--注解配置--> <context:annotation-config></context:annotation-config> <bean id="personDao" class="com.zyy.service.impl.PersonDaoBean"></bean> <bean name="iSpring_6" class="com.zyy.service.impl.ISpring_6Bean"></bean></beans>
package com.zyy.service;/** * Created by CaMnter on 2014/8/27. */public interface ISpring_6 { public void message();}
package com.zyy.service.impl;import com.zyy.service.ISpring_6;import com.zyy.service.PersonDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;/** * Created by CaMnter on 2014/8/27. */public class ISpring_6Bean implements ISpring_6 { @Autowired @Qualifier("personDao") private PersonDao personDao; public ISpring_6Bean() { } public void message() { this.personDao.add(); }}
junit4.4测试代码:
@Test public void test_4() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); ISpring_6 iSpring_6 = (ISpring_6) applicationContext.getBean("iSpring_6"); iSpring_6.message(); }
提示:@Autowired是Spring提供的注解,@Resource是java提供的注解。还是尽量使用@Autowired
比较好,因为是Spring项目。
二.自动装配:
对于自动装配,大家了解一下就可以了,实在不推荐使用。例子:
<beanid="..." class="..." autowire="byType"/>
autowire属性取值如下:
byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。如果发现
多个,那么将会抛出异常。如果没有找到,即属性值为null。
byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没
有找到,即属性值为null。
constructor与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有
找到与构造器参数类型一致的bean,那么将会抛出异常。
autodetect:通过bean类的自省机制(introspection)来决定是使用constructor还
是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。
- 4.Spring DI(依赖注入)
- spring依赖注入(DI)
- Spring 依赖注入(DI)
- spring-DI(依赖注入)
- Spring依赖注入(DI)
- Spring依赖注入(DI)
- Spring依赖注入(DI)
- Spring 依赖注入(DI)
- Spring依赖注入(DI)
- spring(依赖注入-DI)
- Spring DI 依赖注入
- Spring-DI(依赖注入)
- Spring依赖注入DI
- Spring 依赖注入(DI)
- Spring DI[依赖注入]
- Spring DI(依赖注入)
- Spring依赖注入(DI)
- Spring依赖注入DI
- 理解SQL SERVER中的分区表
- OpenCV基础篇之像素操作对比度调节
- Win7删除文件问题
- Java RMI之HelloWorld篇
- 阿里校招笔试题
- 4.Spring DI(依赖注入)
- NandFlash的读写操作
- UVa 11076 - Add Again (排列之和 组合数学)
- hnu 1242 Rescue
- PYTHON基础(转)
- DSP/BIOS使用之初窥门径——滴答时钟及烧写Flash
- HDU 1171
- Mac下使用Eclipse编译C/C++文件出现 launch failed, binary not found 错误的解决办法
- Android事件处理