这一节我们介绍一下Spring框架的相关常用配置
1. Spring依赖注入方式
package com.gxa.spring.day02; public class PetServiceImpl { private PetDaoImpl petDao; //依赖对象 public PetServiceImpl(PetDaoImpl petDao) { //构造方法的DI this.petDao = petDao; } public void selectPet() { petDao.selectPet(); } }
package com.gxa.spring.day02; public class PetDaoImpl { public void selectPet() { /** * 完成宠物数据查询 */ System.out.println("==宠物数据查询=="); } }
<?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.xsd"> <bean id="petService" class="com.gxa.spring.day02.PetServiceImpl"> <constructor-arg name="petDao" ref="petDao"></constructor-arg> </bean> <bean id="petDao" class="com.gxa.spring.day02.PetDaoImpl"></bean> </beans>
package com.gxa.spring.day01; public class PetServiceImpl { private PetDaoImpl petDao; //依赖对象 private ItemDaoImpl itemDao; //依赖对象 public void setPetDao(PetDaoImpl petDao) { this.petDao = petDao; } public void setItemDao(ItemDaoImpl itemDao) { this.itemDao = itemDao; } public void selectPet() { petDao.selectPet(); itemDao.selectItem(); } }
package com.gxa.spring.day01; public class PetDaoImpl { public void selectPet() { /** * 完成宠物数据查询 */ System.out.println("==宠物数据查询=="); } }
package com.gxa.spring.day01; public class ItemDaoImpl { public void selectItem() { /** * 完成宠物分类数据查询 */ System.out.println("==宠物分类的数据查询=="); } }
<?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.xsd"> <bean id="person" class="com.gxa.spring.day01.Person"></bean> <bean id="petService" class="com.gxa.spring.day01.PetServiceImpl"> <property name="petDao" ref="petDao"></property> <property name="itemDao" ref="itemDao"></property> </bean> <bean id="petDao" class="com.gxa.spring.day01.PetDaoImpl"></bean> <bean id="itemDao" class="com.gxa.spring.day01.ItemDaoImpl"></bean> </beans>
原文:http://www.cnblogs.com/liuyangjava/p/6667260.html