Spring_01反射代理创建对象

代理创建对象:把创建对象的任务交给BeanFactory工厂,调用该类静态方法,传递类名即可创建对象。(BeanFactory通过在配置文件bean.properties中查找类路径,通过反射的方式创建对象。)

1、持久层

持久层接口

/**
 * 账户的持久层接口
 */
public interface IAccountDao {
	/**
	 * 模拟保存账户
	 */
	void saveAccount();
}

持久层实现类

public class AccountDaoImpl implements IAccountDao{
/**
 * 账户的持久层实现类
 */
	public void saveAccount() {
		System.out.println("AccountDaoImpl保存成功!");
	}
}

2、业务层

业务层接口

/**
 * 账户业务层的接口
 */
public interface IAccountService {
	/**
	 * 模拟保存账户
	 */
	void saveAccount();
}

业务层实现类

/**
 * 账户的业务层实现类
 * @author 
 *	我们的业务层持久层也好,很少包含可以修改的类成员。
 */

public class AccountServiceImpl implements IAccountService{
	
	//业务层调用持久层
	//private IAccountDao accountDao=new AccountDaoImpl();
	private IAccountDao accountDao= (IAccountDao) BeanFactory.getBean("accountDao");
	
	public void saveAccount() {
		accountDao.saveAccount();
		System.out.println("AccountServiceImpl");
	}
}

3、展示层

/**
 * 模拟一个表现层调用业务层
 * 模拟Service
 * @author 宋强
 *
 */
public class Client {
	public static void main(String[] args) {
		//IAccountService as = new AccountServiceImpl();
		/**
		 * BeanFactory.getBean("accountService")
		 * getBean:是BeanFactory的一个静态方法,传递参数,返回对象。
		 */
		System.out.println();
		IAccountService as =(IAccountService) BeanFactory.getBean("accountService"); 
		System.out.println("Client"+as);
		as.saveAccount();
	}
}

4、工厂工具类

package com.itheima.factory;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 创建bean对象的工厂
 * @author 
 *	Bean:在计算机英语中,有可重用组件的含义。
 *		service可以被Servlet重用
 *		dao可以被service重用
 *	Javabean:用java语言编写的可重用组件。
 *		javabean!==实体类(javabean范围远大于实体类)
 *		他就是创建我们service和dao对象的。
 *
 *		第一个:需要一个配置文件来配置service和dao
 *				配置的内容:唯一标识==全限定类名(key=value)
 *		第二个:通过读取配置文件中的内容,反射来创建对象
 *
 *	我们配置文件可以是xml也可以是properties(容易)
 */

/**
 * 	Properties类表示一组持久的属性。
 *  Properties可以保存到流中或从流中加载。
 *  属性列表中的每个键及其对应的值都是一个字符串。 
 * @author 
 *
 */
public class BeanFactory {
	//定义一个Properties 对象
	private static Properties props;
	//定义一个map,用于存放我们要创建的对象。我们把它称之为容器
	private static Map<String,Object> beans;
	
	//使用静态代码块为Properties对象赋值
	static {
		try {
			//实例化对象
			props = new Properties();
			//获取配置文件properties
			InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
			props.load(in);
			
			//实例化容器
			beans = new HashMap<String ,Object>();
			//取出配置文件中所有的key
			Enumeration keys=props.keys();
			//遍历枚举
			while(keys.hasMoreElements()) {
				//取出每个key
				String key=keys.nextElement().toString();
				//根据key获取value
				String beanPath=props.getProperty(key);
				
				//反射创建对象
				Object value = Class.forName(beanPath).newInstance();
				//把key和value存入容器中
				beans.put(key, value);
				
			}
			
		} catch (Exception e) {
			throw new ExceptionInInitializerError("初始化properties失败。");
		}   
	}
	
	
	/**
	 * 根据bean的名称获取对象		单例的
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName) {
		return beans.get(beanName);
	}
	
	
	/**
	 * 根据Bean的名称获取Bean对象	多例的
	 * @param beanName
	 * @return
	 
	 
	public static Object getBean(String beanName) {
		Object bean=null;
		
		try {
			String beanPath = props.getProperty(beanName);
			//System.out.println(beanPath);
			//每次都会调用默认构造函数创建对象
			bean = Class.forName(beanPath).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bean;
		
	}
	*/	
}

配置文件bean.properties

accountService=com.itheima.service.impl.AccountServiceImpl
accountDao=com.itheima.dao.impl.AccountDaoImpl
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值