spring学习实例之装手动装配自动装配

来学习下装配相关:
手动装配:手动的将bean中所关联的其他bean装配进去。使用以下方法:
<property><ref bean=""></property>
自动装配:使用autowire关键字声明bean的自动装配方式。其可选值为byName、byType、constructor等。

包结构:
spring学习实例之装手动装配自动装配

源代码:
ItemDao.java
package dao;

public interface ItemDao {
	void update();
}

ItemDaoImpl.java
package dao;

public class ItemDaoImpl implements ItemDao{
	public void update(){
		System.out.println("update item .");
	}
}


OrderDao.java
package dao;

public interface OrderDao {
	void save();
}

OrderDaoImpl.java
package dao;

public class OrderDaoImpl implements OrderDao{
	public void save(){
		System.out.println("save order .");
	}
}


StoreService.java
package service;

public interface StoreService {
	void submitOrder();
}

StoreServiceImpl.java
package service;

import dao.ItemDao;
import dao.OrderDao;

public class StoreServiceImpl implements StoreService {
	public ItemDao itemDao;
	public OrderDao orderDao;

	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public void submitOrder() {
		orderDao.save();
		itemDao.update();
	}
}


手动装配配置文件:
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
	<bean id="orderDao" class="dao.OrderDaoImpl"></bean>
	<bean id="itemDao" class="dao.ItemDaoImpl"></bean>

	<bean id="storeService" class="service.StoreServiceImpl">
		<property name="orderDao">
			<ref bean="orderDao"/>
		</property>
		<property name="itemDao">
			<ref bean="itemDao"/>
		</property>
	</bean>
</beans>

自动装配配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byType"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
	<bean class="dao.OrderDaoImpl" autowire="byName"></bean>
	<bean class="dao.ItemDaoImpl"></bean>
	<bean id="storeService" class="service.StoreServiceImpl"></bean>
	
</beans>

以上配置文件中default-autowire="byType"指明默认装配形式为byType(根据类型)。
在具体的bean中也可以单独定制自动装配方式。<bean class="dao.OrderDaoImpl" autowire="byName"></bean>这行则指明了该bean的自动中配为byName(根据名字)。

测试类Test.java
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.StoreService;

public class Test {
	public static void main(String[]args){
		//加载手动装配配置文件
		ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
		StoreService storeService=(StoreService)ac.getBean("storeService");
		storeService.submitOrder();
		
		//加载自动装配配置文件
		ApplicationContext ac_autowire=new ClassPathXmlApplicationContext("ApplicationContext_autowire.xml");
		StoreService storeService_autowire=(StoreService)ac.getBean("storeService");
		storeService_autowire.submitOrder();
	}
}

你可能感兴趣的:(java,DAO,spring,bean,xml)