一只倔强的笨猪

Spring通过不同的工厂创建对象

结构图:
《Spring通过不同的工厂创建对象》

创建工程并导入Spring框架。代码如下:

package com.biz;

import com.dao.GoodsDao;

public class GoodsBIZ {
	private GoodsDao goodsDao;

	public GoodsDao getGoodsDao() {
		return goodsDao;
	}

	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}
	public void save(){
		System.out.println("GoodsBIZ.save() is called...");
		this.goodsDao.save();
	}
}
package com.controller;

import com.biz.GoodsBIZ;

public class GoodsController {
	private GoodsBIZ goodsBIZ;
	public GoodsController(GoodsBIZ goodsBIZ){
		System.out.println("GoodsController的有参构造被调用");
		this.goodsBIZ = goodsBIZ;
	}
	
	public void save(){
		System.out.println("GoodsController.save() is called...");
		goodsBIZ.save();
	}
}
package com.dao;

public interface GoodsDao {
	public void save();
}
package com.dao.impl;

import com.dao.GoodsDao;

public class GoodsDaoImpl implements GoodsDao {
	
	@Override
	public void save() {
		System.out.println("GoodsDaoImpl.save() is called...");
	}
}
package com.factory;

import com.dao.GoodsDao;
import com.dao.impl.GoodsDaoImpl;

public class DaoFactory {
	public static GoodsDao createDao(){
		return new GoodsDaoImpl();
	}
	public GoodsDao getDao(){
		return new GoodsDaoImpl();
	}
}

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"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

	<bean name="goodsDao" class="com.dao.impl.GoodsDaoImpl"/>
	
	<bean name="goodsBIZ" class="com.biz.GoodsBIZ">
		<property name="goodsDao"> 	<!-- GoodsBIZA有个属性叫goodsDAO -->
		<ref bean="goodsDao"/>	<!--依赖。引用了上面那个bean(1) -->
		</property>
	</bean>
	
	<bean name="goodsController" class="com.controller.GoodsController">
	<constructor-arg name="goodsBIZ" ref="goodsBIZ"/>
	</bean>
	
	<bean name="dao1" class="com.factory.DaoFactory" factory-method="createDao"/>
	
	<bean name="daoFactory" class="com.factory.DaoFactory"/>
	<bean name="dao2" factory-bean="daoFactory" factory-method="getDao" scope="prototype"/> 
	<!-- scope属性默认为”singleton”,表示单例;取prototype代表多例 -->
</beans>

测试类:

package com.test;

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

import com.biz.GoodsBIZ;
import com.controller.GoodsController;
import com.dao.GoodsDao;


public class SpringTest {

	public static void main(String[] args) {
		
		@SuppressWarnings("resource")
		ApplicationContext context = 
				new ClassPathXmlApplicationContext("/applicationContext.xml");
				
		//注释掉的是不同的方法创建出的对象
		
		/*GoodsDao goodsDao = (GoodsDao) context.getBean("goodsDao");
		goodsDao.save();*/
		
		/*GoodsBIZ goodsBIZ = (GoodsBIZ) context.getBean("goodsBIZ");
		goodsBIZ.save();*/
		
		/*GoodsController goodsController = (GoodsController) context.getBean("goodsController");
		goodsController.save();*/
		
		/*GoodsDao dao1 = (GoodsDao) context.getBean("dao1");
		System.out.println(dao1);*/
		
		GoodsDao dao21 = (GoodsDao) context.getBean("dao2");
		System.out.println(dao21);
		GoodsDao dao22 = (GoodsDao) context.getBean("dao2");
		System.out.println(dao22);
		System.out.println(dao21 == dao22);
	}

}

项目源代码: 本地下载

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注