DI依赖注入_手动装配_非注解
基本数据类型的注入(将以下代码放到对象的bean中即可)
可以通过构造器注入 <constructor-arg index="0" type="java.lang.String" value="xxx" />
可以通过set方法注入? <property name="name" value="xx" />
可以通过field方式注入(注解)——待续
其它bean的注入
一、通过接口实现注入
这里只关心dao层和service层
思路:在service层声明一个dao层对象,并set该对象;在xml文件中配置dao层和service层的bean,并在service层的bean中使用property属性ref dao层bean
1、编写service层
??? Service.java
package com.qh.service;public interface Service {public abstract void say();}??? ServiceImp.java实现Service接口
package com.qh.serviceImp;import com.qh.dao.Dao;import com.qh.service.Service;public class ServiceImp implements Service {private Dao dao; //声明的dao层对象public Dao getDao() {return dao;}public void setDao(Dao dao) { //通过set方法注入,因为该对象不是基本对象this.dao = dao;}public void say(){dao.add();}}2、编写dao层
??? Dao.java
package com.qh.dao;public interface Dao {public abstract void add();}??? DaoImp.java实现Dao接口
package com.qh.daoImp;import com.qh.dao.Dao;public class daoImp implements Dao {public void add(){System.out.println("我是一个add方法");}}3、编写spring.xml(放在src根目录下)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="daoImp" ref="daoImp"></property></bean></beans>
??? 注:<property name="dao" ref="daoImp"></property>中的name属性对应的是ServiceImp中的dao对象,ref属性对应的是id为daoImp的bean
4、编写测试类
package com.qh.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.qh.serviceImp.ServiceImp;public class SpringTest {@Testpublic void test(){ApplicationContext ax=new ClassPathXmlApplicationContext("spring.xml");ServiceImp s1=(ServiceImp) ax.getBean("serviceImp");s1.say();}}测试结果:输出 ”我是一个add方法“ 这句话
此实例需要 spring.jar 和commons-logging.jar 两个架包
二、采用内部bean的方式注入(该方法使注入的bean不能被其它bean使用)
???? 将上述实例中的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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="serviceImp" name="code">import com.qh.dao.Dao;import com.qh.service.Service;public class ServiceImp implements Service {private Dao dao;private String name;public ServiceImp(Dao dao, String name) {this.dao = dao;this.name = name;}public void say(){System.out.println(name);dao.add();}}??? 修改xml文件
<bean id="daoImp" type="com.qh.dao.Dao" ref="daoImp"></constructor-arg><constructor-arg index="1" type="java.lang.String" value="xiong"></constructor-arg> </bean>
???注:index属性为构造函数参数索引,从0开始;type为参数类型,基本类型可省略
?? 其它文件不变即可
集合的注入
set:private Set<String> set=new HashSet<String>();略set和get方法
???? xml文件中在对应的bean中加入如下:
<property name="set"> //name属性对应set方法的对象<set><value>第一个</value><value>第二个</value><value>第三个</value></set> </property>
???测试
for(String temp: s1.getSet()){System.out.println(temp); }?list:private List<String> list=new ArrayList<String>();和set类似
?properties:private Properties properties=new Properties();和set类似
<property name="properties"><props><prop key="key1">一点</prop><prop key="key2">二点</prop><prop key="key3">三点</prop></props> </property>
???测试
for(Object temp: s1.getProperties().keySet()){System.out.println(s1.getProperties().getProperty((String) temp));}?? map:private Map<String, String> map=new HashMap<String, String>();和set类似
<property name="map"><map><entry key="key1" value="map1"></entry><entry key="key2" value="map2"></entry><entry key="key3" value="map3"></entry></map></property>
??? 测试
for(Object temp: s1.getMap().keySet()){System.out.println(s1.getMap().get(temp));}总结:通过以上方式进行注入,会使xml文件中的bean的代码很庞大,特别是在大型系统的开发中,为了解决这种情况,spring 2.5 提供了注解