Spring依赖注入的方式

Spring依赖注入的方式:

1、构造函数注入

顾名思义,构造函数注入,就是我们依靠类的构造函数来达到DI(依赖注入)的目的,每个参数代表着一个依赖,如下所示:

package org.spring.service;

public interface PersonService {
	public void showMessage();
}
package org.spring.service.impl;

import org.spring.dao.PersonDAO;
import org.spring.service.PersonService;

public class PersonServiceBean implements PersonService {
	private PersonDAO personDAO;

	private String message;

	public PersonServiceBean(PersonDAO personDAO, String message) {
		this.personDAO = personDAO;
		this.message = message;
	}

	public void showMessage() {
		System.out.println(message);
		personDAO.add();
	}
}

上面的PersonDAO接口如下

package org.spring.dao;

public interface PersonDAO {
	public void add();
}

PersonDAO接口的实现类

package org.spring.dao.impl;

import org.spring.dao.PersonDAO;

public class PersonDAOBean implements PersonDAO {
	public void add() {
		System.out.println("执行PersonDAOBean中的增加方法!");
	}
}

配置文件

<?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="personDAOBean" class="org.spring.dao.impl.PersonDAOBean" />

	<bean id="personServiceBean" class="org.spring.service.impl.PersonServiceBean"
		scope="prototype">
		<constructor-arg index="0" ref="personDAOBean" />
		<constructor-arg index="1" value="message" />
	</bean>

</beans>  

index属性用来显示指定构造构造参数的索引,通过使用索引属性不但可以解决多个简单属性的混淆问题,还可以解决有可能有相同类型的2个构造参数的混淆问题了,注意index是从0开始。但是当使用简单类型时,Spring将无法知道该值的类型,不借助其他帮助,它将无法仅仅根据参数类型进行匹配,比如下面的这个例子:

public class ExampleBean {   
    private int age;   
    private String name;   
  
    public ExampleBean(int age, String name) {   
        this.age= age;   
        this.name= name;   
    }   
}

  

<bean id="exampleBean" class="examples.ExampleBean">  
  <constructor-arg type="int" value="18"/>  
  <constructor-arg type="java.lang.String" value="zhangsan"/>  
</bean>

测试类

package org.spring;

import org.junit.Test;
import org.spring.service.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
	@Test
	public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"spring.xml");

		PersonService personService = (PersonService) ctx
				.getBean("personServiceBean");
		
		personService.showMessage();

	}
}

控制台结果

 

2、Setter注入:

通过调用无参构造器或无参static工厂方法实例化bean之后,调用该bean的setter方法,即可实现基于setter的DI

此时PersonServiceBean类修改如下

package org.spring.service.impl;

import org.spring.dao.PersonDAO;
import org.spring.service.PersonService;

public class PersonServiceBean implements PersonService {
	private PersonDAO personDAO;
	private String message;

	public void showMessage() {
		System.out.println(message);
		personDAO.add();
	}

	public PersonDAO getPersonDAO() {
		return personDAO;
	}

	public void setPersonDAO(PersonDAO personDAO) {
		this.personDAO = personDAO;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}

PersonServiceBean中省略了默认的构造器,如果还有其他带参数的构造器,则需要显示的定义默认的构造器,否则Spring容器无法初始化该类

此时的配置文件如下

<?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="personDAOBean" class="org.spring.dao.impl.PersonDAOBean" />

	<bean id="personServiceBean" class="org.spring.service.impl.PersonServiceBean"
		scope="prototype">
		<property name="personDAO" ref="personDAOBean" />
		<property name="message" value="message" />
	</bean>

</beans>  

测试类不变

控制台打印结果

可以得知,通过Setter注入方式成功

猜你喜欢

转载自huangminwen.iteye.com/blog/1041743