天天看點

Spring_3_Spring的依賴注入_setter方法和構造函數方法

《一》setter方實作依賴注入:

1)PersonDao接口類:

public interface PersonDao {
	public void add();
}
           

2)PersonDaoBean實作類:

public class PersonDaoBean implements PersonDao {

	@Override
	public void add() {
		System.out.println("執行personDao中的add()方法!");
	}
}
           

3)PersonService 接口類:

public interface PersonService {

	public void save();

	public Set<String> getSet();
	
	public List<String> getList();
	
	public Properties getProperties();
	
	public Map<String, String> getMap();
}
           

4)PersonServiceBean 實作類:

public class PersonServiceBean implements PersonService {
	
	private PersonDao personDao;
	private Integer id;
	private String name;
	private Set<String> set = new HashSet<String>();
	private List<String> list = new ArrayList<String>();
	private Properties properties = new Properties();
	private Map<String, String> map = new HashMap<String, String>();

	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 List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

	public Set<String> getSet() {
		return set;
	}

	public void setSet(Set<String> set) {
		this.set = set;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public void init() {
		System.out.println("init()執行個體化!");
	}

	public PersonServiceBean() {
		System.out.println("初始化構造函數!");
	}

	@Override
	public void save() {
		System.out.println("name= " + name + ", id= " + id);
		personDao.add();
	}
}
           

5)XML檔案的配置:

<pre name="code" class="html"><?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的方法:(推薦使用)
	  優點:該Bean可以為多個bean服務
	  當執行personService即PersonServiceBean時,
	  會調用ref對應的personDao_即PersonDaoBean,
	  然後利用反射技術将PersonDaoBean中的方法賦給property對應的屬性personDao,<span style="white-space:pre">							</span>  這樣personServiceBean中的personDao就相當于		  
     <span style="white-space:pre">	</span>  PersonDaoBean的對象.
-->	
			   
	<bean id="personDao_" class="springDao.PersonDaoBean" /> 
<bean id="personService" class="springDao.PersonServiceBean" >
		<property name="personDao" ref="personDao_" />
		<!-- 在此也可以給其它屬性注入值 -->
		<property name="name" value="ItCast"></property>
		<property name="id" value="33"></property>
		<!-- 此處的name對應PersonServiceBean中Set集合對象的名字 -->
		<property name="set">
			<set>
				<value>First_set</value>
				<value>Second_set</value>
				<value>Third_set</value>
			</set>
		</property>
		<property name="list">
			<list>
				<value>First_list</value>
				<value>Second_list</value>
				<value>Third_list</value>
			</list>
		</property>
		<property name="properties">
			<props>
				<prop key="key1">value1</prop>
				<prop key="key2">value2</prop>
				<prop key="key3">value3</prop>
			</props>
		</property>
		<property name="map">
			<map>
				<entry key="key1" value="value1"></entry>
				<entry key="key2" value="value2"></entry>
				<entry key="key3" value="value3"></entry>
			</map>
		</property>
	</bean>

<!-- 第二種注入Bean的方法:(不推薦使用)
	  缺點:該内部Bean隻能為一個bean服務,具有局限性
	 
<bean id="personService" class="springDao.PersonServiceBean" >
		<property name="personDao" >
			<bean class="springDao.PersonDaoBean" ></bean>
		</property>
</bean>
-->
</bean>
           

6)測試類springTest:

public class springTest {
	@Test
	public void instanceSpring() {
		AbstractApplicationContext ctx = new 
         ClassPathXmlApplicationContext("springXml/beans.xml");
	 PersonService personService = (PersonService) 			
ctx.getBean("personService");
		 personService.save();

		 System.out.println("========輸出注入的set集合的值=====");
		 for (String value : personService.getSet()) {
		 System.out.println(value);
		 }

		 System.out.println("======輸出注入的list集合的值======");
		 for (String value : personService.getList()) {
		 System.out.println(value);
		 }

		 System.out.println("=輸出注入的properties集合的鍵值對=");
	for (Object key : personService.getProperties().keySet()) {
		 System.out.println(key + "=" +  
      personService.getProperties().getProperty((String) key));
	}

		 System.out.println("=====輸出注入的map集合的鍵值對====");
		 for (String key : personService.getMap().keySet()) {
		 	System.out.println(key + "=" + 
personService.getMap().get(key));
		 }
	}
}
           

《二》構造函數方法依賴注入:

接口類PersonDao和PersonService與《一》中相同。

1)PersonServiceBean類:

public class PersonServiceBean implements PersonService {
	private PersonDao personDao;
	private String name;

	public PersonServiceBean(PersonDao personDao, String name) {
		this.personDao = personDao;
		this.name = name;
	}

	public void save() {
		personDao.add();
		System.out.println("name= " + name);

	}
}
           

2)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="personDao_" class="springDaoBean.PersonDaoBean" />
	<bean id="personService" 	
class="springDaoBean.PersonServiceBean">
		<constructor-arg index="0" ref="personDao_" />
		<constructor-arg index="1" value="Hello World" />
	</bean>
</beans>
           

3)測試檔案代碼:

public class springTest {
	@Test
	public void instanceSpring() {
		AbstractApplicationContext ctx = new 
      ClassPathXmlApplicationContext("springXml/beans.xml");

		// 使用類構造器執行個體化Bean
		PersonService personService = (PersonService) 
  ctx.getBean("personService");
		personService.save();
	}
}
           
<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></strong>