读取类路径下配置文件的方式

574 篇文章 4 订阅
272 篇文章 1 订阅

方法1:

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);

如:
在这里插入图片描述

方法2:


	/**
	 * 初始方法的执行:
	 *  1) SomeService service = new SomeService();
	 *  2) service.setUp();
	 *  
	 *  单例对象调用销毁方法
	 *  service.tearDown();
	 */
	@Test
	public void test01(){
		String configLocation="com/bjpowernode/ba03/applicationContext.xml"; 
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);

		SomeService service = (SomeService) ctx.getBean("someService");
		service.doSome();
		
		/**
		 * 销毁方法的执行:
		 * 1.关闭容器,关闭容器时会通知容器中的单例对象,调用对象自己的销毁方法
		 * 2.对象必须是单例的
		 */
		((ClassPathXmlApplicationContext)ctx).close();
	
	}

方法3: 加载资源文件到项目代码中,然后再读取文件

package com.bjpowernode.test;

import java.io.File;
import java.io.IOException;

import com.bjpowernode.Container;
import com.bjpowernode.service.UserService;

public class MyTest {

	public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException {
		//创建Service对象
//		UserService service = new UserServiceImpl();
//		service.addUser();
		
		
		//使用配置文件表示对象的信息, 使用Contianer完成对象创建,属性赋值
//E:\JAVA课程内容\07Spring\本阶段代码\第一天代码\01-miniSpring\bin
		String path = MyTest.class.getResource("/").getPath();
		System.out.println("path:"+path);
		File file = new File(path,"myobject.txt");
		Container cc  = new Container(file);
		
		
		//从Contianer中获取对象
		UserService service = (UserService) cc.getBean("service");
		
		service.addUser();

	}

}

package com.bjpowernode;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 读取myobject.txt文件
 * 1.使用反射创建对象
 * 2.给对象的属性(依赖对象)赋值
 *
 */
public class Container {
	//使用集合保存创建好的对象
	private Map<String,Object> map = new HashMap<>();
	
	public Container(File file) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
		init(file);
	}
	
	//从Map中获取程序中使用对象
	public Object getBean(String name){
		Object object = null;
		if( map.containsKey(name)){
			object = map.get(name);
		}
		return object;
	}
	
	/**
	 * 读取文件myobject
	 * @param file
	 * @throws IOException 
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public void init(File file) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
		FileReader fr = new FileReader(file);
	//缓冲流读取文件。
		BufferedReader br = new BufferedReader(fr);
		String line = null;
		
		while( (line = br.readLine()) != null ){
			/*
			 *  service=com.bjpowernode.service.UserServiceImpl
				myUserDao=com.bjpowernode.dao.UserDaoMySQLImpl
				service#userDao=myUserDao
			 */
			String [] data = line.split("=");
			String left = data[0];//service#userDao
			String right = data[1];//myUserDao
			
			//区分是第三行数据
			if( left.indexOf("#") > 0 ){
				//第三行数据
				String ref []= left.split("#");
				String refLeft = ref[0];//service
				String refRight = ref[1];//userDao 属性名
				
				//从Map中取对象
				Object bean = map.get(refLeft);
				//反射机制获取Class
				Class clazz  = bean.getClass();
				//获取要修改的属性 Field
				Field field = clazz.getDeclaredField(refRight); //userDao属性
				
				//允许属性赋值
				field.setAccessible(true);
				
				//给属性赋值
				field.set(bean, map.get(right));
				
				
			} else {
				//使用反射创建对象
				Object obj  = makeObject(right);
				//创建好的对象放入Map
				map.put(left, obj);
			}
			
		}
			
		
		
		
	}
	
	
	//创建对象
	private Object makeObject(String className){
		Object object = null;
		try {
			//调用类的无参数构造方法创建对象
			object = Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值